fork download
  1. #include<vector>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstdio>
  5.  
  6. using namespace std;
  7.  
  8. typedef vector<int> vi;
  9. typedef vector<vi> vvi;
  10. typedef pair<int,int> ii;
  11. typedef vector< vector<pair<int,int> > > vii;// Storing vertex and weight;
  12.  
  13. int u,v,w,n;
  14. vector<bool> visited;
  15. vector<int> edgeTo;
  16. vii AdjList;
  17. void DFS(int);
  18.  
  19. int main()
  20. {
  21. //freopen("input.txt","r",stdin);
  22. cout<<"Vertices---> ";
  23. cin>>n;
  24. visited=vector<bool> (n,0);
  25. AdjList=vii (n);
  26. edgeTo=vi (n,-1);
  27. cin>>u>>v>>w;
  28. while(u!=-1&&v!=-1&&w!=-1)
  29. {
  30. AdjList[u].push_back(ii(v,w));
  31. AdjList[v].push_back(ii(u,w));
  32. cin>>u>>v>>w;
  33. }
  34. for(int i=0;i<n;i++)
  35. {
  36. for(vector<pair<int,int> >::iterator itr=AdjList[i].begin();itr!=AdjList[i].end();itr++)
  37. {
  38. cout<<"Vertex "<<i<<" is connected to "<<itr->first<<" and has weight "<<itr->second<<endl;
  39. }
  40. }
  41. cout<<"-------\n";
  42. for(int i=0;i<n;i++)
  43. {
  44. if(!visited[i])
  45. DFS(i);
  46. }
  47. int i=0;
  48. for(vector<int>::iterator itr=edgeTo.begin();itr!=edgeTo.end();itr++)
  49. {
  50. cout<<"Vertex "<<i<<" can be reached from "<<*itr<<endl;
  51. i++;
  52. }
  53.  
  54.  
  55. return 0;
  56. }
  57.  
  58. void DFS(int s)
  59. {
  60. visited[s] = true;
  61. cout<<s<<" \n";
  62. for(vector<pair<int,int> >::iterator itr=AdjList[s].begin();itr!=AdjList[s].end();itr++)
  63. {
  64. if(!visited[itr->first])
  65. {
  66. //cout<<itr->first<<endl;
  67. DFS(itr->first);
  68. edgeTo[itr->first]=s;
  69. }
  70. }
  71. }
  72.  
Success #stdin #stdout 0s 2828KB
stdin
13
0 1 1
0 2 1
0 5 1
0 6 1
3 4 1
3 5 1
4 5 1
4 6 1
7 8 1
9 10 1
9 11 1
9 12 1
11 12 1
-1 -1 -1
stdout
Vertices---> Vertex 0 is connected to 1 and has weight 1
Vertex 0 is connected to 2 and has weight 1
Vertex 0 is connected to 5 and has weight 1
Vertex 0 is connected to 6 and has weight 1
Vertex 1 is connected to 0 and has weight 1
Vertex 2 is connected to 0 and has weight 1
Vertex 3 is connected to 4 and has weight 1
Vertex 3 is connected to 5 and has weight 1
Vertex 4 is connected to 3 and has weight 1
Vertex 4 is connected to 5 and has weight 1
Vertex 4 is connected to 6 and has weight 1
Vertex 5 is connected to 0 and has weight 1
Vertex 5 is connected to 3 and has weight 1
Vertex 5 is connected to 4 and has weight 1
Vertex 6 is connected to 0 and has weight 1
Vertex 6 is connected to 4 and has weight 1
Vertex 7 is connected to 8 and has weight 1
Vertex 8 is connected to 7 and has weight 1
Vertex 9 is connected to 10 and has weight 1
Vertex 9 is connected to 11 and has weight 1
Vertex 9 is connected to 12 and has weight 1
Vertex 10 is connected to 9 and has weight 1
Vertex 11 is connected to 9 and has weight 1
Vertex 11 is connected to 12 and has weight 1
Vertex 12 is connected to 9 and has weight 1
Vertex 12 is connected to 11 and has weight 1
-------
0  
1  
2  
5  
3  
4  
6  
7  
8  
9  
10  
11  
12  
Vertex 0 can be reached from -1
Vertex 1 can be reached from 0
Vertex 2 can be reached from 0
Vertex 3 can be reached from 5
Vertex 4 can be reached from 3
Vertex 5 can be reached from 0
Vertex 6 can be reached from 4
Vertex 7 can be reached from -1
Vertex 8 can be reached from 7
Vertex 9 can be reached from -1
Vertex 10 can be reached from 9
Vertex 11 can be reached from 9
Vertex 12 can be reached from 11