forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlight Routes Check.cpp
More file actions
42 lines (37 loc) · 862 Bytes
/
Flight Routes Check.cpp
File metadata and controls
42 lines (37 loc) · 862 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5+1;
int N, M, a, b;
bool vis1[maxN], vis2[maxN];
vector<int> G1[maxN], G2[maxN];
void dfs1(int u = 1, int p = 0){
vis1[u] = true;
for(int v : G1[u])
if(v != p && !vis1[v])
dfs1(v, u);
}
void dfs2(int u = 1, int p = 0){
vis2[u] = true;
for(int v : G2[u])
if(v != p && !vis2[v])
dfs2(v, u);
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0; i < M; i++){
scanf("%d %d", &a, &b);
G1[a].push_back(b);
G2[b].push_back(a);
}
dfs1();
dfs2();
for(int i = 1; i <= N; i++){
if(!vis1[i] || !vis2[i]){
printf("NO\n");
if(!vis1[i]) printf("1 %d\n", i);
else printf("%d 1\n", i);
return 0;
}
}
printf("YES\n");
}