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
| #include <bits/stdc++.h> using namespace std; namespace Legendgod { namespace Read {
#ifdef Fread const int Siz = (1 << 21) + 5; char *iS, *iT, buf[Siz]; #define gc() ( iS == iT ? (iT = (iS = buf) + fread(buf, 1, Siz, stdin), iS == iT ? EOF : *iS ++) : *iS ++ ) #define getchar gc #endif template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; !isdigit(c); c = getchar()) if(c == '-') f = -1; for(; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= f; } template <typename T, typename...Args> void r1(T &x, Args&...arg) { r1(x), r1(arg...); } #undef getchar }
using namespace Read;
const int maxn = 3e5 + 5; int n, m; int head[maxn], cnt; struct Edge { int to, next; }edg[maxn << 1]; int dg[maxn]; void add(int u,int v) { edg[++ cnt] = (Edge) {v, head[u]}, head[u] = cnt; ++ dg[v]; }
queue<int> q; int f[maxn], vis[maxn];
void Solve(int x,int y,int c) { int fl(0); for(int i = head[y];i;i = edg[i].next) { int to = edg[i].to; if(dg[to] == 1) { fl = 1; break; } } if(fl) vis[x] = 1; else f[x] += c; }
void topu() { int i, j; int sum(0); while(!q.empty()) q.pop(); for(i = 1; i <= n; ++ i) if(dg[i] == 0) q.push(i), ++ sum; while(!q.empty()) { int sz = q.size(), u = q.front(); q.pop(); if(sz == 1) f[u] += n - sum; else if(sz == 2) Solve(u, q.front(), n - sum); for(i = head[u];i;i = edg[i].next) { int to = edg[i].to; if(!(-- dg[to])) q.push(to), ++ sum; } } }
vector<pair<int, int>> E;
signed main() { int i, j; r1(n, m); for(i = 1; i <= m; ++ i) { int u, v; r1(u, v), add(u, v), E.push_back({v, u}); } topu();
memset(head, 0, sizeof(head)), memset(dg, 0, sizeof(dg)), cnt = 1; for(const auto& v: E) add(v.first, v.second); topu(); int ans(0); for(i = 1; i <= n; ++ i) if(!vis[i] && f[i] >= n - 2) ++ ans; printf("%d\n", ans); return 0; }
}
signed main() { return Legendgod::main(), 0; }
|