-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.cpp
More file actions
52 lines (47 loc) · 860 Bytes
/
1260.cpp
File metadata and controls
52 lines (47 loc) · 860 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
#include <iostream>
#include <queue>
#define MAX 1000
using namespace std;
int N, M, V;
int graph[MAX + 1][MAX + 1];
bool visited[MAX + 1]; // DFS
queue<int> q; // BFS
void DFS(int curr) {
cout << curr << " ";
visited[curr] = true;
for (int n = 1; n <= N; n++) {
if (graph[curr][n] == 0 || visited[n]) {
continue;
}
DFS(n);
}
}
void BFS(int curr) {
q.push(curr);
visited[curr] = true;
while (!q.empty()) {
curr = q.front();
cout << curr << " ";
q.pop();
for (int n = 1; n <= N; n++) {
if (graph[curr][n] == 0 || visited[n]) {
continue;
}
q.push(n);
visited[n] = true;
}
}
}
int main() {
cin >> N >> M >> V;
for (int m = 1; m <= M; m++) {
int x, y; cin >> x >> y;
graph[x][y] = graph[y][x] = 1;
}
DFS(V); cout << endl;
for (int i = 1; i <= N; i++) { // init
visited[i] = false;
}
BFS(V);
return 0;
}