36分求调
查看原帖
36分求调
695154
Astraios楼主2025/7/2 07:50
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1000000+10;
struct Edge
{
	int next,to;
} e[maxn*2],E[maxn*2];
int head[maxn],k,head2[maxn],k2;
void add(int u,int v)
{
	e[++k].next=head[u];
	e[k].to=v;
	head[u]=k;
}
void add2(int u,int v)
{
	E[++k2].next=head2[u];
	E[k2].to=v;
	head2[u]=k2;
}
int m,n,ans;
int tot,cnt,subn;
int st[maxn],top;
int w[maxn];
int dfn[maxn],low[maxn],tim;
void tarjan(int u,int fa)
{
	subn++;
	dfn[u]=low[u]=++tim;
	st[++top]=u; w[st[top]]=-1;
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].to;
		if(!dfn[v])
		{
			tarjan(v,u);
			low[u]=min(low[u],low[v]);
			if(dfn[u]<=low[v])
			{
				cnt++; w[cnt]=1;
				while(st[top]!=v)
				{
					add2(st[top],cnt); add2(cnt,st[top]);
					w[cnt]++;
					top--;
				}
				add2(st[top],cnt); add2(cnt,st[top]); w[cnt]++; top--;
				add2(u,cnt); add2(cnt,u); w[cnt]++;
			}
		}
		else if(v!=fa) low[u]=min(low[u],dfn[v]);
	}
}
int siz[maxn];
void dfs(int u,int fa)
{
	siz[u]=(u<=n);
	for(int i=head2[u];i;i=E[i].next)
	{
		int v=E[i].to;
		if(v==fa) continue;
		dfs(v,u);
		ans+=2ll*siz[u]*siz[v]*w[u];
		siz[u]+=siz[v];
	}
	ans+=siz[u]*(subn-siz[u])*w[u];
}
signed main()
{
	cin>>n>>m; cnt=n;
	for(int i=1;i<=m;i++)
	{
		int u,v;
		cin>>u>>v;
		if(u==v) continue;
		add(u,v); add(v,u);
	}
	for(int i=1;i<=n;i++)
	{
		if(!dfn[i])
		{
			subn=0;
			tarjan(i,i);
			dfs(i,i);
		}
	}
	cout<<ans;
	return 0;
}
2025/7/2 07:50
加载中...