-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260(재시작).cpp
More file actions
81 lines (61 loc) · 994 Bytes
/
Copy path1260(재시작).cpp
File metadata and controls
81 lines (61 loc) · 994 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
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int N, M, V;
vector<int> vc[1001];
int visit[1001];
void dfs(int n) {
printf("%d ", n);
visit[n] = 1;
for (int i = 0; i < vc[n].size(); i++) {
int nx = vc[n][i];
if (visit[nx] == 0) {
dfs(nx);
}
}
}
void bfs(int n) {
queue<int> q;
q.push(n);
visit[n] = 1;
while (1) {
int x = q.front();
q.pop();
printf("%d ", x);
for (int i = 0; i < vc[x].size(); i++) {
int nx = vc[x][i];
if (visit[nx] == 0) {
visit[nx] = 1;
q.push(nx);
}
}
if (q.empty()) {
break;
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M >> V;
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
vc[a].push_back(b);
vc[b].push_back(a);
}
for (int i = 1; i <= N; i++) {
sort(vc[i].begin(), vc[i].end());
}
dfs(V);
printf("\n");
for (int i = 1; i <= N; i++) {
visit[i] = 0;
}
bfs(V);
return 0;
}