-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathLongest Flight Route.cpp
58 lines (50 loc) · 1.14 KB
/
Longest Flight Route.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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5+1;
bool vis[maxN];
int N, M, K, a, b, in[maxN], p[maxN], l[maxN], ans[maxN];
vector<int> G[maxN];
queue<int> Q;
void dfs(int u, int par = 0){
vis[u] = true;
for(int v : G[u])
if(v != par && !vis[v])
dfs(v, u);
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0; i < M; i++){
scanf("%d %d", &a, &b);
G[a].push_back(b);
in[b]++;
}
dfs(1);
if(!vis[N]){
printf("IMPOSSIBLE\n");
return 0;
}
fill(l+2, l+maxN, -1);
for(int i = 1; i <= N; i++)
if(in[i] == 0)
Q.push(i);
while(!Q.empty()){
int u = Q.front(); Q.pop();
for(int v : G[u]){
if(l[u] != -1 && l[v] < l[u]+1){
l[v] = l[u] + 1;
p[v] = u;
}
in[v]--;
if(in[v] == 0)
Q.push(v);
}
}
K = l[N] - l[1];
printf("%d\n", K+1);
for(int i = K, u = N; i >= 0; i--){
ans[i] = u;
u = p[u];
}
for(int i = 0; i <= K; i++)
printf("%d%c", ans[i], (" \n")[i==K]);
}