AC了,但有个问题
查看原帖
AC了,但有个问题
794701
wo_hen_la楼主2024/7/24 22:44

为什么tarjan函数中的sum,不能被siz[u]-1代替(20pts),不都是一样吗?

#include <bits/stdc++.h>
using namespace std;

#define space putchar(' ');
#define enter putchar('\n');
#define pb push_back
#define int long long
#define P 998244353
#define HP 1000000000000002097
#define N (int)1e5+5

int read(){ char ch=getchar();int x=0,f=1;while(ch>'9' || ch<'0'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return x*f; }
void print(int x){ if(x<0) putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+'0'); }

int n,m;
vector<int> e[N];
int dfn[N],low[N],siz[N],ti,ans[N];
bool vis[N];
void tarjan(int u,int fa)
{
	dfn[u]=low[u]=++ti;
	siz[u]=1;
	int son=0,sum=0;
	for(auto v:e[u]){
		if(v==fa) continue;
		Union(u,v);
		if(!dfn[v]){
			tarjan(v,u);
			siz[u]+=siz[v];
			low[u]=min(low[u],low[v]);
			son++;
			if(low[v]>=dfn[u]){
				ans[u]+=(n-siz[v])*siz[v];
				sum+=siz[v];
				if(u!=1) vis[u]=1;
			}
		}
		else low[u]=min(low[u],dfn[v]);
	}
	if(fa==0 && son>=2) vis[u]=1;
	if(!vis[u]) ans[u]=2*(n-1);
	else{
		ans[u]+=(n-sum-1)*(sum+1)+n-1;
	}
}
main()
{
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
	cin>>n>>m;
	int u,v;
	while(m--){
		cin>>u>>v;
		e[u].pb(v);e[v].pb(u);
	}
	for(int i=1;i<=n;i++){
		if(!dfn[i]){
			tarjan(i,0);
		}
	}
	for(int i=1;i<=n;i++) cout<<ans[i]<<"\n";
	return 0;
}

2024/7/24 22:44
加载中...