模拟链表

· · 个人记录

// 模拟链表
#include <iostream>
#include <vector>
using namespace std;
#define MAXN 5002

vector<int> adj[MAXN];

int main() {
  int n, m; cin >> n >> m;
  while(m--){
    int x, y; cin >> x >> y;
    adj[x].push_back(y); adj[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    cout << adj[i].size() << ' ';
    for (auto x : adj[i]) {
      cout << x << ' ';
    }
    cout << endl;
  }
  return 0;
}