题解 P4630 【[APIO2018] Duathlon 铁人两项】
那么一个圆点作为
看起来是
这个
那么就可以
再思考方点的贡献。
对于
那么方点计算的就是以这个点双中的某个点作为
求出这个点双中的圆点数,即方点的度数
如果
但是
如果
所以方点的答案为
这个也可以用类似上面的
所以这道题的复杂度为
注意图不联通。
代码:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
inline LL read(){
LL x = 0,f = 1; char c = getchar();
while (c != EOF && !isdigit(c)) {if (c == '-') f = -1;c = getchar();}
while (isdigit(c)) {x = x * 10 + c - '0';c = getchar();}
return x * f;
}
inline void write(LL x){
if (x < 0) putchar('-'),x = -x;
if (x > 9) write(x/10); putchar(x%10+'0');
}
inline void writeln(LL x){ write(x),putchar('\n'); }
const int N = 100005,M = 200005,E = M*4;
int To[E],Ne[E],He1[N],He2[N<<1],k;
inline void add(int *He,int x,int y){
++k; To[k] = y,Ne[k] = He[x],He[x] = k;
++k; To[k] = x,Ne[k] = He[y],He[y] = k;
}
int n,m,tot,deg[N<<1];
int dfn[N],low[N],st[N],top,Tim;
void tarjan(int x){
dfn[x] = low[x] = ++Tim,st[++top] = x;
for (int y,p = He1[x]; p ; p = Ne[p]){
y = To[p];
if (!dfn[y]){
tarjan(y);
low[x] = min(low[x],low[y]);
if (low[y] >= dfn[x]){
++tot; add(He2,tot,x); add(He2,tot,y);
++deg[x],++deg[y],deg[tot] += 2;
while (st[top] ^ y) add(He2,st[top],tot),++deg[tot],++deg[st[top]],--top;
--top;
}
}
else low[x] = min(low[x],dfn[y]);
}
}
int size1[N<<1],size2[N<<1],fa[N<<1];
void dfs1(int x){
size1[x] = x <= n ? 1 : 0;
for (int p = He2[x]; p ; p = Ne[p]) if (To[p] ^ fa[x]) fa[To[p]] = x,dfs1(To[p]),size1[x] += size1[To[p]];
}
LL ans,ret;
void dfs2(int x){
if (fa[x]) size2[x] = size2[fa[x]] + size1[fa[x]] - size1[x];
for (int y,p = He2[x]; p ; p = Ne[p]) if ((y=To[p])^fa[x]) dfs2(To[p]);
int sum = size2[x] + size1[x];
ret = 0;
if (x <= n){
for (int y,p = He2[x]; p ; p = Ne[p]) if ((y=To[p])^fa[x]){
ans += (LL)(sum - size1[y] - 1) * size1[y];
}
ans += (LL)(sum - size2[x] - 1) * size2[x];
}
else{
for (int y,p = He2[x]; p ; p = Ne[p]) if ((y=To[p])^fa[x]){
ans += (LL)(sum - size1[y]) * size1[y] * (deg[x] - 2);
}
ans += (LL)(sum - size2[x]) * size2[x] * (deg[x] - 2);
}
}
int main(){
int i,u,v;
tot = n = read(),m = read();
while (m--) u = read(),v = read(),add(He1,u,v);
for (i = 1; i <= n; ++i) if (!dfn[i]) top = 0,tarjan(i);
for (i = 1; i <= tot; ++i) if (!size1[i]) dfs1(i);
for (i = 1; i <= tot; ++i) if (!size2[i]) dfs2(i);
writeln(ans);
return 0;
}