題解 [JSOI2015]最小表示
阿新 • • 發佈:2021-10-21
Solution
md,智障了。
首先看出一個結論,就是說一條邊 \((u,v)\) 可以刪當且僅當在原圖中刪掉改變該邊 \(u\) 仍能到達 \(v\)。
因為這是一個 DAG,所以我們可以拓排之後從後往前掃,然後用 bitset 維護連通性。
Code
#include <bits/stdc++.h> using namespace std; #define Int register int #define MAXM 100005 #define MAXN 30005 template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;} template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);} template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');} template <typename T> inline void chkmax (T &a,T b){a = max (a,b);} template <typename T> inline void chkmin (T &a,T b){a = min (a,b);} vector <int> g[MAXN],h[MAXN],sta[MAXN]; int n,m,px[MAXM],py[MAXM],dep[MAXN],deg[MAXN]; void topo (){ queue <int> q; for (Int i = 1;i <= n;++ i){ deg[i] = h[i].size(); if (!deg[i]) dep[i] = 1,q.push (i); } while (!q.empty()){ int u = q.front(); q.pop (),sta[dep[u]].push_back (u); for (Int v : g[u]) if (!-- deg[v]) dep[v] = dep[u] + 1,q.push (v); } } bitset <MAXN> S[MAXN]; signed main(){ read (n,m); for (Int i = 1;i <= m;++ i) read (px[i],py[i]),g[px[i]].push_back (py[i]),h[py[i]].push_back (px[i]); topo ();int ans = 0; for (Int i = n;i >= 1;-- i) for (Int u : sta[i]){ sort (g[u].begin(),g[u].end(),[](int x,int y){return dep[x] < dep[y];}); S[u][u] = 1; for (Int v : g[u]) if (S[u][v]) ans ++; else S[u] |= S[v]; } write (ans),putchar ('\n'); return 0; }