-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1260.cpp
48 lines (44 loc) · 1022 Bytes
/
1260.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
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<queue>
using namespace std;
#define MAX_VALUE 1001
int N, M, V;
int mat[MAX_VALUE][MAX_VALUE];
int visit[MAX_VALUE];
void dfs(int v) {
cout << v << ' ' ;
visit[v] = 1;
for(int i=1; i<=N; i++) {
if(visit[i] == 1 || mat[v][i] == 0)
continue;
dfs(i);
}
}
void bfs(int v) {
queue<int> q;
q.push(v);
visit[v] = 0;
while(!q.empty()) {
v = q.front();
cout << q.front() << ' ';
q.pop();
for(int i=1; i<=N; i++) {
if(visit[i] == 0 || mat[v][i] == 0)
continue;
q.push(i);
visit[i] = 0;
}
}
}
int main() {
int x, y;
cin >> N >> M >> V;
for(int i=0; i<M; i++) {
cin >> x >> y;
mat[x][y] = mat[y][x] = 1;
}
dfs(V);
cout << '\n';
bfs(V);
return 0;
}