fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int node, edge;
  7. cin>>node>>edge;
  8. vector<int>graph[node+1];
  9. int u,v;
  10. for(int i = 1; i <= edge; i++)
  11. {
  12. cin>>u>>v;
  13. graph[u].push_back(v);
  14. graph[v].push_back(u);
  15. }
  16.  
  17. ///i = node, graph[i][j] = node, j = column number
  18. for(int i = 1; i <= node; i++)
  19. {
  20. cout<<" Node "<<i<<" -> ";
  21. for(int j = 0; j < graph[i].size(); j++)
  22. {
  23. cout<<graph[i][j]<<" ";
  24. }
  25. cout<<endl;
  26. }
  27. }
  28.  
Success #stdin #stdout 0.01s 5324KB
stdin
4 4
1 2
2 3
3 4
4 1
stdout
 Node 1 -> 2 4 
 Node 2 -> 1 3 
 Node 3 -> 2 4 
 Node 4 -> 3 1