-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage_Route.cpp
More file actions
77 lines (66 loc) · 1.33 KB
/
Copy pathMessage_Route.cpp
File metadata and controls
77 lines (66 loc) · 1.33 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
74
75
76
#include<bits/stdc++.h>
using namespace std;
using ll= long long;
bool bfs(vector<bool> &vis, vector<ll>adj[],vector<ll> &par,ll n, vector<ll> &path)
{
vis[1] =true;
queue<ll>q;
q.push(1);
par[1]=0;
while(!q.empty())
{
ll c = q.front();
q.pop();
if(c == n)
{
ll cur=n;
while(cur!=0)
{
path.push_back(cur);
cur=par[cur];
}
return true;
}
for(auto v : adj[c])
{
if(!vis[v])
{
par[v]=c;
q.push(v);
vis[v]=true;
}
}
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,m;
cin>>n>>m;
vector<ll>adj[n+1];
for(ll i =0; i<m; i++ )
{
ll u,v;
cin>> u>> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<bool>vis(n+1);
vector<ll>par(n+1);
vector<ll>path;
if(bfs(vis,adj,par,n,path))
{
cout<<path.size()<<endl;
reverse(path.begin(),path.end());
for(auto v : path)
{
cout<<v<<" ";
}
}
else{
cout<<"IMPOSSIBLE";
}
}