luogu ac,spoj tle
查看原帖
luogu ac,spoj tle
852295
Mr_Vatican楼主2022/11/14 18:54
#include <bits/stdc++.h>
using namespace std;
int n,size1,size2,tot,idx,ans1,t,head[50005];
long long ans2;
int iscut[50005],low[50005],dfn[50005];
struct edge
{
	int next,to;
}e[100005];
void add_edge(int u,int v)
{
	e[++tot].next=head[u];
	e[tot].to=v;
	head[u]=tot;
}
stack<int>s;
vector<int>dcc[1005];
void init()
{
	s=stack<int>();
	for(int i=1;i<=size2;i++)
	{
		dcc[i].clear();
	}
	size1=size2=tot=idx=ans1=0;
	ans2=1;
	memset(iscut,0,sizeof iscut);
	memset(low,0,sizeof low);
	memset(dfn,0,sizeof dfn);
	memset(head,0,sizeof head);
}
void tarjan(int u)
{
	int fa=0;
	s.push(u);
	low[u]=dfn[u]=++idx;
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].to;
		if(!dfn[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
			if(low[v]>=dfn[u])
			{
				fa++;
				if(u!=1||fa>1)
					iscut[u]=1;
				dcc[++size2].push_back(u);
				int tmp=0;
				do
				{
					tmp=s.top();
					s.pop();
					dcc[size2].push_back(tmp);
				}while(tmp!=v);
			}
		}
		else
			low[u]=min(low[u],dfn[v]);
	}
}
int main()
{
	while(++t)
	{
		init();
		scanf("%d",&n);
		if(!n)
		    break;
		for(int i=1;i<=n;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			add_edge(x,y);
			add_edge(y,x);
			size1=max(size1,max(x,y));
		}
		tarjan(1);
		for(int i=1;i<=size2;i++)
		{
			int size=dcc[i].size(),cnt=0;
			for(int j=0;j<size;j++)
			{
				cnt+=iscut[dcc[i][j]];
			}
			if(cnt==1)
			{
				ans1++;
				ans2*=size-1;
			}
			if(!cnt)
			{
				ans1+=2;
				ans2*=size*(size-1)/2;
			}
		}
		printf("Case %d: %d %lld\n",t,ans1,ans2);
	}
	return 0;
}
2022/11/14 18:54
加载中...