-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Flight_Route.cpp
More file actions
50 lines (50 loc) · 1.18 KB
/
Copy pathLongest_Flight_Route.cpp
File metadata and controls
50 lines (50 loc) · 1.18 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
#include "bits/stdc++.h"
// #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
list<ll> dfs(ll node, vector<vector<ll>> &adj, vector<bool> &visited, vector<list<ll>> &arr){
if(node==adj.size()-1){
list<ll> l;
l.push_back(node);
arr[node]=l;
return l;
}
if(visited[node]){
return arr[node];
}
visited[node] = true;
list<ll> ans;
for(auto it: adj[node]){
list<ll> a = dfs(it, adj, visited, arr);
if (a.size()!=0&&a.size() > ans.size()&& a.back() == adj.size()-1) {
ans = std::move(a);
}
}
ans.push_front(node);
return arr[node]=ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
cin>>n>>m;
vector<vector<ll>> adj(n);
vector<list<ll>> arr(n);
vector<bool> visited(n, false);
for(int i=0;i<m;i++){
ll x,y;
cin>>x>>y;
adj[x-1].push_back(y-1);
}
list<ll> res=dfs(0, adj, visited, arr);
if(res.size()==0||res.back()!=n-1){
cout<<"IMPOSSIBLE"<<endl;
return 0;
}
cout<<res.size()<<endl;
for(auto it: res){
cout<<it+1<<" ";
}
cout<<endl;
return 0;
}