fork download
  1. #include<bits/stdc++.h>
  2. #define endl '\n'
  3. using namespace std;
  4. int main()
  5. {
  6. int n;
  7. cin>>n;
  8. int e;
  9. cin>>e;
  10. vector<pair<int,int>> g[n+1];
  11. for(int i=0;i<e;i++)
  12. {
  13. int a,b,wt;
  14. cin>>a>>b>>wt;
  15. g[a].push_back(make_pair(b,wt));
  16. g[b].push_back(make_pair(a,wt));
  17. }
  18. for(int i=0;i<=n;++i)
  19. {
  20. if(g[i].size()==0)
  21. cout<<i<<" is not connected to any vertex"<<endl;
  22. else
  23. {
  24. cout<<i<<" is connected to following vertices with following weights"<<endl;
  25. for(int j=0;j<g[i].size();++j)
  26. {
  27. cout<<g[i][j].first<<" "<<g[i][j].second<<endl;
  28. }
  29. }
  30. }
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 16064KB
stdin
5 4
1 2 1
2 3 2
3 4 3
4 5 4
stdout
0 is not connected to any vertex
1 is connected to following vertices with following weights
2 1
2 is connected to following vertices with following weights
1 1
3 2
3 is connected to following vertices with following weights
2 2
4 3
4 is connected to following vertices with following weights
3 3
5 4
5 is connected to following vertices with following weights
4 4