蒟蒻求教才40pts
查看原帖
蒟蒻求教才40pts
224927
lei_yu楼主2020/6/29 21:01
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#define int long long
using namespace std;
int n,m,times,cnt,head[1000001],dfn[1000001],low[1000001],gd[1000001],gd_num[1000001],son[1000001];
struct node
{
	int to,next;
}a[5000001];
void add_edge(int from,int to)
{
	a[++cnt].to=to;
	a[cnt].next=head[from];
	head[from]=cnt;
}
void tarjan(int u,int root)
{
	int yhsb=n-1;
	int cs=0;
	dfn[u]=low[u]=++times;
	for(int i=head[u];i;i=a[i].next)
	{
		int v=a[i].to;
		if(!dfn[v])
		{
			cs++;
			tarjan(v,root);
			son[u]+=son[v];
			low[u]=min(low[u],dfn[v]);
			if(u!=root&&dfn[u]<=low[v])
			{
				gd[u]=1;
				gd_num[u]+=son[v]*(yhsb-son[v]);
				yhsb-=son[v];
				//cout<<u<<" "<<v<<" "<<gd_num[u]<<" "<<endl;
			}
		}
		else low[u]=min(low[u],dfn[v]);
	}		
	if(u==root&&cs>1)
	{
		gd[u]=1;
		//cout<<"根居然是割点!!   ";
		for(int i=head[u];i;i=a[i].next)
		{

			int v=a[i].to;
			//cout<<v<<" "<<son[v]<<" ";
			gd_num[u]+=son[v]*(yhsb-son[v]);
			yhsb-=son[v];
			//cout<<gd_num[u]<<"           ";
		}
	}
}
signed main()
{
	//freopen("P3469_1.in","r",stdin);
	//freopen("myans.txt","w",stdout);
	int x,y;
	cin>>n>>m;	
	for(int i=1;i<=n;i++)son[i]=1;
	for(int i=1;i<=m;i++)
	{
		cin>>x>>y;
		add_edge(x,y);
		add_edge(y,x);
	}
	tarjan(1,1);
	//for(int i=1;i<=n;i++)cout<<son[i]<<" ";
	//cout<<endl;
	for(int i=1;i<=n;i++)
	{
		if(!gd[i])cout<<(n-1)*2<<endl;
		else cout<<(n-1)*2+2*gd_num[i]<<endl;
	}
}

因为是自己想的好像和题解有亿点点不同诶。。。

不过这样哪里错了?求hack qwqqwq

2020/6/29 21:01
加载中...