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.  
  10. int u, v;
  11. for(int i = 1; i <= edge; i++)
  12. {
  13. cin>>u>>v;
  14. graph[u].push_back(v);
  15. graph[v].push_back(u);
  16. }
  17.  
  18. for(int i = 1; i <= node; i++)
  19. {
  20. cout<<i<<" -> ";
  21. for(int j = 0; j < graph[i].size(); j++)
  22. {
  23. cout<<graph[i][j]<<" ";
  24. }
  25. cout<<endl;
  26. }
  27.  
  28. }
  29.  
Success #stdin #stdout 0.01s 5316KB
stdin
4 5
1 2
2 3
3 4
4 1
2 4
stdout
1 -> 2 4 
2 -> 1 3 4 
3 -> 2 4 
4 -> 3 1 2