bzoj 2140: 稳定婚姻 强连通分量

       这道题目建图还是很奇妙的。

       把一对夫妻抽象为一个点i。那么如果夫妻对i的男方和夫妻对j的女方有关♂(jian)♀系♂(qing),那么就连一条边i->j,那么如果存在一个环就说明不(hui)安(chu)全(gui)。

       Tarjan强连通分量跑跑就好了。

AC代码如下:

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#define N 40005
using namespace std;

int n,m,cnt,tot,dfsclk,fst[N],pnt[N],nxt[N],pos[N],low[N],stk[N],tp,scc[N],sum[N];
struct stg{
	char ch[10];
	void read(){ scanf("%s",ch); }
}s;
bool operator <(stg a,stg b){
	int len1=strlen(a.ch),len2=strlen(b.ch),i;
	for (i=0; i<len1 && i<len2; i++){
		if (a.ch[i]<b.ch[i]) return 1; if (a.ch[i]>b.ch[i]) return 0;
	}
	return len1<len2;
}
map<stg,int> mp1,mp2;
void add(int x,int y){
	pnt[++tot]=y; nxt[tot]=fst[x]; fst[x]=tot;
}
void dfs(int x){
	pos[x]=low[x]=++dfsclk; stk[++tp]=x; int p;
	for (p=fst[x]; p; p=nxt[p]){
		int y=pnt[p];
		if (!pos[y]){
			dfs(y); low[x]=min(low[x],low[y]);
		} else if (!scc[y]) low[x]=min(low[x],pos[y]);
	}
	if (low[x]==pos[x])
		for (cnt++; ; ){
			scc[stk[tp]]=cnt; sum[cnt]++;
			if (stk[tp--]==x) break;
		}
}
int main(){
	scanf("%d",&n); int i,x,y;
	for (i=1; i<=n; i++){
		s.read(); mp1[s]=i;
		s.read(); mp2[s]=i;
	}
	scanf("%d",&m);
	while (m--){
		s.read(); x=mp1[s];
		s.read(); y=mp2[s]; add(x,y);
	}
	for (i=1; i<=n; i++){
		if (!pos[i]) dfs(i);
		puts((sum[scc[i]]>1)?"Unsafe":"Safe");	
	}
	return 0;
}


by lych

2016.3.26

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值