Description
求出如果把每个点割去, 有序对$(x, y)$且 $x,y$不连通的对数
Solution
做一遍Tarjan割点,一个点$x$ 不是割点, 那么有序对数为$ 2 * (n - 1)$
如果$x$为割点, 那么割去$x$ 所构成的联通块有3类:
1: $x$
2: $x$的搜索树中的点$y$, 且$low[y] >= dfn[x]$
3:其他节点
对于联通块$i$,大小为$size_i$, 那么最后的答案就是$sum (n - size_i)*(size_i)$。
Code
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define ll long long 5 #define rep(i,a,b) for(register int i = (a) ; i <= (b); ++i) 6 #define per(i,a,b) for(register int i = (a); i >= (b); --i) 7 #define rd read() 8 #define R register 9 using namespace std; 10 11 const int N = 1e5 + 1e4, M = 5e5 + 1e4; 12 13 int dfn[N], low[N], cnt; 14 int n, m, head[N], tot, size[N], cut[N]; 15 ll ans[N]; 16 17 struct edge { 18 int nxt, to; 19 }e[M << 1]; 20 21 int read() { 22 int X = 0, p = 1; char c = getchar(); 23 for(; c > '9' || c < '0'; c = getchar()) if(c == '-') p = -1; 24 for(; c >= '0' && c <= '9'; c = getchar()) X = X * 10 + c - '0'; 25 return X * p; 26 } 27 28 void added(int u, int v) { 29 e[++tot].to = v; 30 e[tot].nxt = head[u]; 31 head[u] = tot; 32 } 33 34 void add(int u, int v) { 35 added(u, v); added(v, u); 36 } 37 38 int ch(int x) { 39 return ((x + 1) & 1) - 1; 40 } 41 42 void dfs(int x, int pre) { 43 dfn[x] = low[x] = ++cnt; 44 size[x] = 1; 45 int flag = 0, sum = 0; 46 for(R int i = head[x]; i; i = e[i].nxt) { 47 if(i == ch(pre)) continue; 48 int nt = e[i].to; 49 if(dfn[nt]) low[x] = min(low[x], dfn[nt]); 50 else { 51 dfs(nt, i); 52 low[x] = min(low[x], low[nt]); 53 size[x] += size[nt]; 54 if(low[nt] >= dfn[x]) { 55 flag++; 56 ans[x] += 1LL * size[nt] * (n - size[nt]); 57 sum += size[nt]; 58 if(x != 1 || flag > 1) cut[x] = 1; 59 } 60 } 61 } 62 if(cut[x]) 63 ans[x] += n - 1 + 1LL*(n - sum - 1) * (sum + 1); 64 else ans[x] = 2 * (n - 1); 65 } 66 67 int main() 68 { 69 n = rd; m = rd; 70 rep(i, 1, m) add(rd, rd); 71 rep(i, 1, n) if(!dfn[i]) dfs(i, -1); 72 rep(i, 1, n) printf("%lld ", ans[i]); 73 }