-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf80789_8a.cpp
More file actions
56 lines (46 loc) · 704 Bytes
/
f80789_8a.cpp
File metadata and controls
56 lines (46 loc) · 704 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
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int N = 1 << 10;
vector <int> graph[N];
bool visited[N];
int m;
void input()
{
scanf("%d\n", &m);
for (int i = 0; i < m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
graph[u].push_back(v);
}
}
void bfs(int u)
{
queue <int> q;
q.push(u);
visited[u] = 1;
while (q.size() > 0)
{
int front = q.front();
q.pop();
printf("%d ", front);
for (int i = 0; i < graph[front].size(); i++)
{
int v = graph[front][i];
if (!visited[v])
{
q.push(v);
visited[v] = 1;
}
}
}
}
int main()
{
input();
bfs(1);
printf("\n");
return 0;
}