题目
题解
最后树的形态我描述一下,我刚开始就是因为想象中树的形态是错的才理解了好久
每个方点下面连着一堆圆点,两棵树之间的公共点为这两个点双的割点
再补充一下题解中的(3), u到 v的路径上的点双是包括 u和 v的
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100002,M=200002;
struct node{
int to,ne;
}e[N<<2],e2[M<<2];
int cnt,tim,dfn[N],low[N],h[N],h2[M],sz[M],sum[M],tot,tot2,st[N],top,n,m,i,x,y;
ll f[M],ans,g[M];
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
int x=0,fl=1;char ch=gc();
for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return x*fl;
}
void add(int x,int y){
e[++tot]=(node){y,h[x]},h[x]=tot;
e[++tot]=(node){x,h[y]},h[y]=tot;
}
void add2(int x,int y){
e2[++tot2]=(node){y,h2[x]},h2[x]=tot2;
e2[++tot2]=(node){x,h2[y]},h2[y]=tot2;
}
void tarjan(int u){
low[u]=dfn[u]=++tim,st[top++]=u;
for (int i=h[u],v;i;i=e[i].ne)
if (!dfn[v=e[i].to]){
tarjan(v);
low[u]=min(low[u],low[v]);
if (low[v]>=dfn[u]){
int x;cnt++;
do{
add2(cnt,x=st[--top]);
sz[cnt]++;
}while (x!=v);
add2(cnt,u),sz[cnt]++;
}
}else low[u]=min(low[u],dfn[v]);
}
void dfs(int u,int fa){
sum[u]=u<=n,g[u]=u<=n?-1:0;
for (int i=h2[u],v;i;i=e2[i].ne)
if ((v=e2[i].to)!=fa){
dfs(v,u);
f[u]+=f[v]+g[u]*sum[v]+g[v]*sum[u];
g[u]+=g[v]+1ll*sz[u]*sum[v];
sum[u]+=sum[v];
}
}
int main(){
cnt=n=rd(),m=rd();
memset(sz,-1,n+1<<2);
for (i=1;i<=m;i++) x=rd(),y=rd(),add(x,y);
for (i=1;i<=n;i++)
if (!dfn[i]) tarjan(i),dfs(i,0),ans+=f[i]<<1;
printf("%lld",ans);
}