rt,会T 4个点
#include<bits/stdc++.h>
using namespace std;
const int N=8005,M=25005;
int n,m,T;
string a,b;
map<string,int> to;
int head[N],cnt_e,low[N],dfn[N],idx,num[N],cnt_scc,x[N],y[N];
bool vis[N];
stack<int> st;
struct E{
int to,pre;
}e[M];
inline void add(int from,int to)
{
e[++cnt_e].to=to;
e[cnt_e].pre=head[from];
head[from]=cnt_e;
return;
}
void dfs(int u)
{
vis[u]=true;
low[u]=dfn[u]=++idx;
st.push(u);
for(int i=head[u];i;i=e[i].pre)
{
int v=e[i].to;
if(!vis[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
int t=-1;
++cnt_scc;
do{
t=st.top();st.pop();
vis[t]=false;
num[t]=cnt_scc;
}while(t!=u);
}
return;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
cin>>a>>b;
x[i]=to[a]=++T;y[i]=to[b]=++T;
add(to[a],to[b]);
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
cin >> a>>b;
add(to[b],to[a]);
}
for(int i=1;i<=T;i++)
if(!dfn[i])
dfs(i);
for(int i=1;i<=n;i++)
{
if(num[x[i]]==num[y[i]])
puts("Unsafe");
else
puts("Safe");
}
return 0;
}