返回顶部

矿场搭建(tarjan)

# 矿场搭建(tarjan)
Mining Your Own Business
矿场搭建测试点太水了,唉

红圈为割点,我们需要找点双连通分量,然后如果一个点双连通分量中有至少1个割点,如果割点大于1则不必建通道
所以只用分一个点双连通分量中割点数为1还是0
因为有序数对(1,2)和(2,1)为两种情况
所以割点为0时
\(ans*=size*(size-1)/2\)
如果割点为1时(去掉割点,即为size-1)
\(ans*=(size-1)\)

点击查看代码
#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int N=50005;
int low[N],dfn[N],num,head[N],cnt;bool vis[N],cut[N];
int t,n,root;
vector <int> dcc[N];
struct Edge
{
	int to,next,from,id;
}edge[N*2];
int ge;
void add(int u,int v)
{
	edge[++cnt].to=v;
	edge[cnt].next=head[u];
	edge[cnt].from=u;
	head[u]=cnt;
}
stack <int> stk;
void tarjan(int now)
{
	low[now]=dfn[now]=++num;
	stk.push(now);
	if(now==root&&!head[now])
	{
		dcc[++ge].push_back(now);
		return;
	}
	int son=0;
	for(int i=head[now];i;i=edge[i].next)
	{
		int to=edge[i].to;
		if(!dfn[to])
		{
			tarjan(to);
			low[now]=min(low[now],low[to]);
			if(dfn[now]<=low[to])
			{
				son++;
				if(now!=root||son>1)cut[now]=true;
				ge++;
				int x;
				do
				{
					x=stk.top();
					stk.pop();
					dcc[ge].push_back(x);
				}while(x!=to);
				dcc[ge].push_back(now);
			}
		}else
		{
			low[now]=min(low[now],dfn[to]);
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
//	cin>>n;
//freopen("a.in","r",stdin);
	int T=0;
	while(cin>>n)
	{
		if(!n)break;
		T++;
		memset(head,0,sizeof(head));
		memset(edge,0,sizeof(edge));
		memset(dfn,0,sizeof(dfn));
		memset(low,0,sizeof(low));
		memset(dcc,0,sizeof(dcc));
		memset(cut,0,sizeof(cut));
		int x,y;
		ge=0;num=0;
		cnt=0;
		int st=1,en=1;
		for(int i=1;i<=n;i++)
		{
			cin>>x>>y;
			en=max({en,x,y});
			if(x==y)continue;//重要
			add(x,y);add(y,x);
			
		}
		for(int i=1;i<=en;i++)
		if(!dfn[i])
		{
			root=i,tarjan(i);
		}
		ll chukou=0,fangan=1,gedian=0;
		if(ge==1)
		{
			cout<<"Case "<<T<<": "<<2<<" "<<(ll)dcc[1].size()*(dcc[1].size()-1)/2<<endl;//重要
			continue;
		}
		for(int i=1;i<=ge;i++)
		{
			gedian=0;
			for(int j=0;j<dcc[i].size();j++)
			{
				if(cut[dcc[i][j]])gedian++;
			}
			if(gedian==1)
				chukou++,fangan*=dcc[i].size()-1;
			else if(gedian==0)chukou+=2,fangan*=(dcc[i].size()*(dcc[i].size()-1))/2; 
		}
		cout<<"Case "<<T<<": "<<chukou<<" "<<fangan<<endl;
	}
	return 0;
}
posted @ 2024-03-14 21:14  wlesq  阅读(12)  评论(0编辑  收藏  举报