-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.cpp
39 lines (32 loc) · 765 Bytes
/
DFS.cpp
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
#include<bits/stdc++.h>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
using namespace std;
vector<int> v[1000005];
int visit[100005];
void dfs(int node){
//cout<< "\nNode = "<< node << endl;
visit[node] = 1;
cout<< "Node size = "<< v[node].size()<< endl;
cout<< node << " -> ";
for(int i=0; i<v[node].size(); i++){
int child = v[node][i];
cout<<"Child = "<< child << " ";
cout<< "I = "<< i << endl;
if(visit[child]==0) dfs(child);
}
}
int main()
{
sync;
int n,e;
cin>>n>>e;
int x,y;
while(e--){
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1);
return 0;
}