#include<bits/stdc++.h>
using namespace std;
const int N=10000;
stack<int>s;
map<string,int>mp;
vector<int>a[N];
bool c[N];
int t,ind,dfn[N],low[N],kind[N];
struct node{
int hus,wif;
}mar[N];
void tarjan(int x){
ind++;
dfn[x]=ind;
low[x]=ind;
s.push(x);c[x]=1;
int i,y,len=a[x].size();
for(i=0;i<len;i++){
y=a[x][i];
if(dfn[y]==0){
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(c[y])
low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x]){
t++;
do{
y=s.top();
s.pop();
kind[y]=t;
c[y]=0;
}while(x!=y);
}
}
int main(){
int i,q,p,n,m;
string s1,s2;
cin>>n;
for(i=1;i<=n;i++){
cin>>s1>>s2;
q=i*2-1;
p=i*2;
mp[s1]=q;
mp[s2]=p;
a[q].push_back(p);
mar[i]=(node){q,p};
}
cin>>m;
for(i=1;i<=m;i++){
cin>>s1>>s2;
q=mp[s1];
p=mp[s2];
a[p].push_back(q);
}
for(i=1;i<=2*n;i++)
if(!dfn[i])
tarjan(i);
for(i=1;i<=n;i++)
if(kind[mar[i].hus]==kind[mar[i].wif])
puts("Unsafe");
else puts("Safe");
return 0;
}
BZOJ 2140 稳定婚姻
最新推荐文章于 2018-10-16 21:12:42 发布