-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_printing.cpp
More file actions
73 lines (60 loc) · 1.15 KB
/
path_printing.cpp
File metadata and controls
73 lines (60 loc) · 1.15 KB
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
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1005];
bool vis[1005];
int level[1005];
int parent[1005];
void bfs(int src)
{
queue<int> q;
q.push(src);
vis[src] = true;
level[src] = 0;
while (!q.empty())
{
int par = q.front();
q.pop();
// cout << par << endl;
for (int child : v[par])
{
if (!vis[child])
{
q.push(child);
vis[child] = true;
level[child] = level[par] + 1;
parent[child] = par;
}
}
}
}
int main()
{
int n, e;
cin >> n >> e;
while (e--)
{
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
memset(vis, false, sizeof(vis));
memset(level, -1, sizeof(level));
memset(parent, -1, sizeof(parent));
int src, des;
cin >> src >> des;
bfs(src);
int x = des;
vector<int> path;
while (x != -1)
{
path.push_back(x);
x = parent[x];
}
reverse(path.begin(), path.end());
for (int val : path)
{
cout << val << " ";
}
return 0;
}