-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbfs.cpp
49 lines (45 loc) · 809 Bytes
/
bfs.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
#include <iostream>
#include <fstream>
#include <list>
#include <queue>
std::ifstream in ("bfs.in");
std::ofstream out ("bfs.out");
struct ndL
{
unsigned int nd;
unsigned int len;
};
std::list<unsigned int> adL[100001];
std::vector<int> lens(100001,-1);
unsigned int n,m,st;
int main()
{
in >> n >> m >> st;
for(int i = 0 ; i < m; i++)
{
unsigned int src,dest;
in >> src >> dest;
adL[src].push_back(dest);
}
std::queue<ndL> bf;
ndL stN = (ndL){st,0};
lens[st]=0;
bf.push(stN);
for(;!bf.empty();bf.pop())
{
for(auto it=adL[bf.front().nd].begin(); it!=adL[bf.front().nd].end(); it++)
{
if(lens[*it]==-1)
{
bf.push((ndL){*it,bf.front().len+1});
lens[*it]=bf.front().len+1;
}
}
}
for(int i = 1; i <= n; i++)
{
out << lens[i] << ' ';
}
out << '\n';
return 0;
}