fork(2) download
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct edge{
  6. int from;
  7. int to;
  8. int cap;
  9. };
  10.  
  11. int n,m;
  12. vector<edge> list;
  13. vector<vector<edge*> > adj;
  14. int a,b,c;
  15. edge t;
  16. int main(){
  17. cin>>n>>m;
  18. int g=0;
  19. adj.resize(n+3);
  20. for(int i=0;i<m;i++){
  21. cin>>a>>b>>c;
  22.  
  23. t.from=a;
  24. t.to=b;
  25. t.cap=c;
  26. list.push_back(t);
  27. g++;
  28. adj[a].push_back(&list[g-1]);
  29. adj[b].push_back(&list[g-1]);
  30. }
  31. for(int i=1;i<=n;i++){
  32. cout<<"adjacent nodes of the node "<<i<<" are :"<<endl;
  33. for(int j=0;j<adj[i].size();j++){
  34. if(adj[i][j]->from==i){
  35. cout<<adj[i][j]->to<<" ";
  36. } else {
  37. cout<<adj[i][j]->from<<" ";
  38. }
  39. }
  40. cout<<endl<<endl;
  41. }
  42. }
Success #stdin #stdout 0s 3284KB
stdin
4 6
1 2 3
2 3 4
3 1 2
2 2 5
3 4 3
4 3 3
stdout
adjacent nodes of the node 1 are :
165904704 3 

adjacent nodes of the node 2 are :
165904704 3 2 2 

adjacent nodes of the node 3 are :
2 1 4 4 

adjacent nodes of the node 4 are :
3 3