Skip to content

Commit 548c282

Browse files
authored
Create 1680 longestFlightRoute(optimised).cpp
1 parent d249eb8 commit 548c282

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// JAI BAJARANG BALI
2+
3+
// manitianajay45
4+
5+
// give me some sunshine, give me some rain, give me another chance to grow up once again....
6+
7+
// sab moh maya hai....
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
#define ll int
13+
14+
ll n, m;
15+
16+
vector<ll> graph[100005];
17+
vector<bool> visited(100005,false);
18+
bool flag=false;
19+
vector<ll> par(100005,-1);
20+
vector<ll> d(100005,-1);
21+
22+
23+
void dfs(ll v){
24+
25+
if(v==n){
26+
flag=true;
27+
d[v]=0;
28+
return ;
29+
}
30+
31+
32+
visited[v]=true;
33+
for(auto u:graph[v]){
34+
35+
36+
37+
if(!visited[u]){
38+
dfs(u);
39+
}
40+
if(d[u]==-1){
41+
continue;
42+
}
43+
if(d[v]< d[u]+1){
44+
d[v]=d[u]+1;
45+
par[v]=u;
46+
}
47+
48+
49+
50+
51+
}
52+
53+
54+
}
55+
56+
57+
58+
59+
int main()
60+
{
61+
ios_base::sync_with_stdio(false);
62+
cin.tie(NULL);
63+
cin >> n >> m;
64+
for(ll i=0;i<m;i++){
65+
ll u,v;
66+
cin>>u>>v;
67+
graph[u].push_back(v);
68+
}
69+
d[1]=0;
70+
71+
72+
dfs(1);
73+
// cout<<"-1"<<endl;
74+
75+
if(!flag){
76+
cout<<"IMPOSSIBLE"<<endl;
77+
return 0;
78+
}
79+
80+
vector<ll> ans;
81+
ll nd=1;
82+
while(nd!=-1){
83+
ans.push_back(nd);
84+
nd=par[nd];
85+
}
86+
// reverse(ans.begin(),ans.end());
87+
cout<<ans.size()<<endl;
88+
for(auto i:ans){
89+
cout<<i<<" ";
90+
}
91+
cout<<endl;
92+
93+
94+
95+
return 0;
96+
}

0 commit comments

Comments
 (0)