-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph-depth first traversal
46 lines (44 loc) · 1.01 KB
/
Graph-depth first traversal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// depth first traversal in graph representing it with adjacency list
#include <vector>
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
int n;
void dfs(int s,int v,vector<int>a,vector<vector<pair<int,int>>>arr);
int main() {
int m,u,v,w,s;
pair<int,int>p;
cin>>n>>m;
vector<pair<int,int>> temp;
vector<vector<pair<int,int>>>arr(n,temp);
for(int i=0;i<m;i++)
{
cin>>u>>v>>w;
p=make_pair(v,w);
arr[u].push_back(p);
}
cin>>s;
vector<int>a(n,0);
dfs(s,-1,a,arr);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
void dfs(int s,int v,vector<int>a,vector<vector<pair<int,int>>>arr)
{
a[s]=1;
pair<int,int>t;
if(a[s])
{
// p=arr[s][];
// cout<<p.first<<" "<<p.second<<endl;
cout<<s<< " "<<v<<endl;
}
for(int i=0;i<arr[s].size();i++)
{
t=arr[s][i];
if(!a[t.first]){
dfs(t.first,t.second,a,arr);
}
}
}