-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepth_First_Search.cpp
More file actions
68 lines (62 loc) · 804 Bytes
/
Copy pathDepth_First_Search.cpp
File metadata and controls
68 lines (62 loc) · 804 Bytes
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
using namespace std;
const int MaxN = 1e3+100;
vector<int> adj [MaxN];
int parent [MaxN];
bool mark[MaxN];
bool leaves [MaxN];
int n,m,com;
void input()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int u,v;
cin>>v>>u;
adj[v].push_back(u);
adj[u].push_back(v);
}
}
void DFS(int v)
{
mark[v]=1;
for (int i=0;i<adj[v].size();i++)
if(mark[adj[v][i]]==0)
{
parent[adj[v][i]]=v;
leaves[v]=1;
DFS(adj[v][i]);
}
}
int main()
{
input();
for(int i=1;i<=n;i++)
if(!mark[i])
{
DFS(i);
com++;
}
int t;
cin>>t;
for (int i=1;i<=t;i++)
{
int Q;
int v;
cin>>Q;
if (Q==1)
{
cin>>v;
cout<<parent[v]<<endl;
}
if(Q==2)
{
cin>>v;
cout<<!leaves[v]<<endl;
}
if(Q==3)
cout<<com<<endl;
}
return 0;
}