-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1260.cpp
72 lines (58 loc) · 857 Bytes
/
p1260.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <queue>
using namespace std;
bool arr[1001][1001];
bool visit[1001];
int n, m, v, x, y;
void dfs(int);
void bfs(int);
queue<int> bq;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n>>m>>v;
for (int i = 0; i < m; i++)
{
cin >> x >> y;
arr[x][y] = 1;
arr[y][x] = 1;
}
dfs(v);
cout << '\n';
for (int i = 0; i <= n; i++)
visit[i] = false;
bfs(v);
return 0;
}
void dfs(int nn)
{
visit[nn] = true;
cout << nn << ' ';
for (int i = 1; i <= n; i++) {
if (arr[i][nn] && !visit[i])
{
dfs(i);
}
}
}
void bfs(int nn)
{
visit[nn] = true;
bq.push(nn);
while (!bq.empty())
{
int temp = bq.front();
bq.pop();
cout << temp << ' ';
for (int i = 1; i <= n; i++)
{
if (arr[temp][i] && !visit[i])
{
visit[i] = true;
bq.push(i);
}
}
}
}