Problem Statement
Longest Flight Route
Implementation
void solve() {
ll n, m;
cin >> n >> m;
vvll v(n);
rep(i, m) {
ll x, y;
cin >> x >> y;
--x, --y;
v[x].pb(y);
}
vll topo, vis(n, 0);
function <void(ll, ll)> dfs = [&](ll node, ll pr) {
vis[node] = 1;
for (auto& child : v[node]) {
if (!vis[child]) dfs(child, node);
}
topo.pb(node);
};
dfs(0, 0);
reverse(all(topo));
vll dp(n, -inf), save(n, -1);
dp[0] = 0;
for (auto& node : topo) {
for (auto& child : v[node]) {
dp[child] = max(dp[child], dp[node] + 1);
if (dp[child] == dp[node] + 1) save[child] = node;
}
}
if (dp[n - 1] == -inf) {
cout << "IMPOSSIBLE\n";
return;
}
vll path;
for (ll i = n - 1; ; i = save[i]) {
path.pb(i);
if (i == 0) break;
}
reverse(all(path));
cout << sz(path) << "\n";
for (auto& x : path) cout << x + 1 << " ";
cout << "\n";
}