15分求助
查看原帖
15分求助
300166
Zikl楼主2023/7/3 20:45
#include<iostream>
#include<map> 
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 300005
using namespace std;
int n,m,a[N];
map<string,int>mp;
int head[N],Next[N],ver[N],tot=1;
void add(int x,int y){
	ver[++tot]=y,Next[tot]=head[x],head[x]=tot;
}
int dfn[N],low[N],cnt,init[N],stack_[N],top,cntt;
void tarjan(int x){
	dfn[x]=low[x]=++cnt;
	stack_[++top]=x;
	init[x]=1;
	for(int i=head[x];i;i=Next[i]){
		int y=ver[i];
		if(!dfn[i]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		if(init[y])
		low[x]=min(low[x],dfn[y]);
	}
	if(low[x]==dfn[x]){
		cntt++;
		while(1){
			a[stack_[top]]=cntt;
			init[stack_[top]]=0;
			top--;
			if(x==stack_[top+1])break;
		}
	}
}
int main(){
    cin>>n;
    string G,B;
    for(int i=1;i<=n;i++){
		cin>>G>>B;
		mp[G]=i;
		mp[B]=i+n;
		add(i,i+n);
    }
	cin>>m;
	for(int i=1;i<=m;i++){
		cin>>G>>B;
		add(mp[B],mp[G]);
    }
    for(int i=1;i<=n*2;i++)
    if(!dfn[i])
    tarjan(i);
    for(int i=1;i<=n;i++){
		 if(a[i]==a[i+n]) cout<<"Unsafe"<<endl; 
		 else cout<<"Safe"<<endl;
    }
    return 0;
}

找不出问题。。。

2023/7/3 20:45
加载中...