-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF.cpp
83 lines (80 loc) · 1.32 KB
/
F.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
#include "bits/stdc++.h"
using namespace std;
int l[300010], r[300010];
vector <int> g[300010];
int in[300010];
int n, m;
int cnt[2][300010];
const int inf = 1e8;
void solve(int c) {
for(int i = 1; i <= n; i++) {
g[i].clear();
in[i] = 0;
}
for(int i = 1; i <= m; i++) {
if(c == 0) {
g[l[i]].push_back(r[i]);
++in[r[i]];
} else {
g[r[i]].push_back(l[i]);
++in[l[i]];
}
}
int alive = n;
vector <int> Q;
for(int i = 1; i <= n; i++) {
if(in[i] == 0) {
Q.push_back(i);
--alive;
}
}
while(!Q.empty()) {
int x = Q.back();
Q.pop_back();
if(Q.size() == 0) {
cnt[c][x] = alive;
} else if (Q.size() == 1) {
int y = Q.back();
bool bad = false;
for(auto j : g[y]) {
if(in[j] == 1) {
bad = true;
break;
}
}
if(!bad) {
cnt[c][x] = alive;
} else {
cnt[c][x] = -inf;
}
} else {
cnt[c][x] = -inf;
}
for(auto j : g[x]) {
--in[j];
if(in[j] == 0) {
Q.emplace_back(j);
--alive;
}
}
}
}
int main(int argc, char const *argv[])
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i++) {
scanf("%d %d", &l[i], &r[i]);
}
solve(0);
solve(1);
int ans = 0;
for(int i = 1; i <= n; i++) {
assert(cnt[0][i] + cnt[1][i] < n);
if(cnt[0][i] + cnt[1][i] >= n-2) {
// cout << i << endl;
++ans;
}
}
printf("%d\n", ans);
return 0;
}