-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy path1682.cc
56 lines (50 loc) · 868 Bytes
/
1682.cc
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
// https://cses.fi/problemset/task/1682
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
vvi g, h;
vi a, s, t;
void dfs1(int i) {
s[i] = 1;
for (int j : g[i])
if (!s[j])
dfs1(j);
a.push_back(i);
}
void dfs2(int i) {
t[i] = 1;
for (int j : h[i])
if (!(t[j]))
dfs2(j);
}
int main() {
int n, m, u, v;
cin >> n >> m;
g = vvi(n);
h = vvi(n);
for (int i = 0; i < m; i++) {
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
h[v].push_back(u);
}
s = vi(n);
for (int i = 0; i < n; i++)
if (!s[i])
dfs1(i);
reverse(a.begin(), a.end());
t = vi(n);
int c = 0;
for (int i : a)
if (!t[i]) {
c++;
if (c > 1) {
cout << "NO\n" << i + 1 << ' ' << a[0] + 1 << '\n';
return 0;
}
dfs2(i);
}
cout << "YES\n";
}