-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestFlight.cpp
More file actions
75 lines (58 loc) · 1.13 KB
/
longestFlight.cpp
File metadata and controls
75 lines (58 loc) · 1.13 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
#include <iostream>
#include <vector>
using namespace std;
const int N = 1000;
int state[N];
vector<int> fin;
vector <int> adjList[N];
int childArr[N];
int dp[N];
void topSort (int node)
{
if (state[node] == 1 || state[node] == 2) return;
state[node] = 1;
for (auto i: adjList[node]) {
topSort(i);
}
state[node] = 2;
fin.push_back(node);
}
void dfs (int node)
{
for (auto i: adjList[node]) {
dp[node] = max(dp[i]+1, dp[node]);
if (dp[node] == dp[i]+1) {
childArr[node] = i;
}
}
}
int main ()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
x--;
y--;
adjList[x].push_back(y);
}
for (int i = 0; i < n; i++) {
state[i] = 0;
}
topSort(0);
bool found = false;
for (int i = 0; i < fin.size(); i++) {
if (found) dfs(fin[i]);
if (fin[i] == n-1) {
found = true;
dp[n-1] = 1;
}
}
cout << dp[n-1] << endl;
int i = 1;
while (i != n-1) {
cout << i << " ";
i = childArr[i];
}
}