P3469 [POI2008]BLO-Blockade

https://www.luogu.org/problemnew/show/P3469

#include 
#include 
#include 
#include 
#include 
using namespace std;

const int MAXN=100005;
const int MAXM=1000005;
int n,m,p[MAXN],ec,ans;
struct cyq{
	int v,next;
}edge[MAXM];

void add(int u, int v){	 
	edge[++ec].v = v;
	edge[ec].next = p[u];
	p[u] = ec;
}
int dfn[MAXN],cTime,low[MAXN];	
int gid[MAXN],gc,size[MAXN];
bool flag,belong[MAXN];

void tarjan(int u,int fa){
	dfn[u]=low[u]=++cTime;size[u]=1;flag=1;
	for(int i=p[u];i;i=edge[i].next){
		int v=edge[i].v;if(v==fa && flag){flag=0;continue;}
		if(!dfn[v]){
			tarjan(v,u);
			size[u]+=size[v];
			if(dfn[u]<=low[v]) belong[u]=1;
			low[u]=min(low[u],low[v]);
		}
		else low[u]=min(low[u],dfn[v]);
	}
}

void work(int u){
	long long sum=2ll*(n-1);
	for(int i=p[u];i;i=edge[i].next){
		int v=edge[i].v;
		sum+=size[v]*1ll*(n-size[v]-1);
	}
	printf("%lld\n",sum);
}

int main(){	
	scanf("%d%d",&n,&m);int x,y;
	for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);add(x,y);add(y,x);}
	for(int i=1;i<=n;i++)
		if(!dfn[i]) tarjan(i,0);
	for(int i=1;i<=n;i++){
		if(!belong[i]) printf("%lld\n",2ll*(n-1));
		else work(i);
	}
	return 0;
}

你可能感兴趣的:(Tarjan)