UVA 247 Calling Circles

点双连通分量裸题。每找到一个双连通分量,就将其输出。

要注意对字符串的处理,将其转化为图中的节点。

代码:

#include <bits/stdc++.h>
using namespace std;
map<int,string> l;
map<string,int> h;
int n,m;
int tot=0;
bool g[101][101];
int dfn[101];
int low[101];
bool vis[101];
int top=0;
int cnt=0;
int s[101];
int cur=0;
vector<int> ans[101];
void tarjan(int u)
{
	s[++top]=u;
	low[u]=dfn[u]=++cur;
	vis[u]=1;
	for(int i=1;i<=n;i++)
	{
		if(g[u][i])
		{
			if(dfn[i]==0)tarjan(i);
			if(vis[i]&&low[i]<low[u])low[u]=low[i];
		}
	}
	if(dfn[u]==low[u])
	{
		cnt++;
		bool ok=0;
		do{
			ans[cnt].push_back(s[top]);
			if(!ok){cout<<l[s[top]];ok=1;}
			else cout<<", "<<l[s[top]];
			vis[s[top]]=0;
			top--;
		}while(s[top+1]!=u);
		cout<<endl;
	}
}
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m;
	int sce=0;
	while(n)
	{
		sce++;
		h.clear();l.clear();
		memset(g,0,sizeof(g));
		memset(dfn,0,sizeof(dfn));
		memset(low,0,sizeof(low));
		memset(vis,0,sizeof(vis));
		memset(s,0,sizeof(s));
		cur=0;
		for(int i=1;i<=tot;i++)ans[i].clear();
		cnt=tot=0;
		string s1,s2;
		int x,y;
		for(int i=1;i<=m;i++)
		{
			cin>>s1>>s2;
			if(h[s1])x=h[s1];
			else
			{
				h[s1]=++tot;
				l[tot]=s1;
				x=tot;
			}
			if(h[s2])y=h[s2];
			else
			{
				h[s2]=++tot;
				l[tot]=s2;
				y=tot;
			}
			g[x][y]=1;
		}
		cout<<"Calling circles for data set "<<sce<<":"<<endl;
		for(int i=1;i<=tot;i++)
		{
			if(dfn[i]==0)
			{
				top=0;tarjan(i);
			}
		}
		cin>>n>>m;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值