上的题解都太神仙了,我不太懂怎么容斥。。。。。
首先这道题要建出圆方树。
考虑枚举中转点,统计满足条件的的个数。
如果为圆点:
对于的每一个儿子,子树内的所有点作为都能和其他子树中的节点组成合法的,对的贡献为
那么一个圆点作为的就是
看起来是的,因为要枚举根来计算,但是我们可以通过一个,求出表示当为根时,(dfs树上的)的.
这个是的。
那么就可以统计所有圆点的了。
再思考方点的贡献。
对于和都不在该点双内的,它们到达这个点双内部之前,已经被这个点双里的一些点计算过了,所以我们不考虑。
那么方点计算的就是以这个点双中的某个点作为,中至少有一个点在点双内,且没有被圆点的ans统计到的答案。
求出这个点双中的圆点数,即方点的度数.
如果中有一个点在点双内部,那么可以有个点双内的点作为.
但是在割点处被计算了一次,所以应该是
如果都在点双内部,那么可行的显然有种。
所以方点的答案为
这个也可以用类似上面的方法做到.
所以这道题的复杂度为
注意图不联通。
代码:
#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;
}