-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1682-FlightRouteCheck.cpp
95 lines (67 loc) · 1.43 KB
/
1682-FlightRouteCheck.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// JAI BAJARANG BALI
// manitianajay45
// give me some sunshine, give me some rain , give me another chance to grow up once again....
// sab moh maya hai....
/*
kosaraju algorithm.
step:-
1 perform topo sort.
2 reverse all link of directed graph.
3. run dfs over graph from topo sort first node.
and the node which is not visited will not connect with first node of topo sort.
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
ll n,m;
vector<bool> visited(200005,false);
vector<ll> graph[200005];
vector<ll> order;
vector<ll> graphr[200005];
void topo(ll v){
visited[v]=true;
for(auto u:graph[v]){
if(!visited[u]){
topo(u);
}
}
order.push_back(v);
}
void dfs(ll v){
visited[v]=true;
for(auto u:graphr[v]){
if(!visited[u]){
dfs(u);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ll n;
cin >> n>>m;
for (ll i = 0; i <m; i++)
{
ll u,v;
cin>>u>>v;
graph[u].push_back(v);
graphr[v].push_back(u);
}
topo(1);
// cout<<"YES"<<endl;
visited.assign(n+1,false);
ll nd=order[n-1];
// cout<<nd<<endl;
dfs(nd);
for(ll i=1;i<=n;i++){
if(!visited[i]){
cout<<"NO"<<endl;
cout<<i<<" "<<nd<<endl;
return 0;
}
}
cout<<"YES"<<endl;
return 0;
}