-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.cpp
39 lines (33 loc) · 912 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 mx 1000000
using namespace std;
/***************************************************************************************/
vector<int> graph[mx];
bool vis[mx];
int dis[mx];
void dfs(int vertex) {
vis[vertex] = true;
for (int i = 0; i < graph[vertex].size(); i++) {
int next = graph[vertex][i];
if (!vis[next]) { //if not visited //
dis[next] = dis[vertex] + 1; //child distance is +1 more than parent //
dfs(next);
}
}
}
void solve() {
int edge, source;
cin >> edge;
for (int i = 0; i < edge; i++) {
int v1, v2;
cin >> v1 >> v2;
graph[v1].push_back(v2);
graph[v2].push_back(v1);
}
cin >> source;
dis[source] = 0;
dfs(source);
}
int main() {
solve();
}