题解:
题意就是求二分图的必须边。
我们有结论:
在残量网络上跑tarjan,对于一条边(u,v)
如果该边满流||scc[u]==scc[v],那么该边是可行边。
因为如果scc[u]==scc[v],那么说明v到u有通路,我们把v-u的路以及u-v这条边全部反色,也就是匹配->非匹配,非匹配->匹配。同样还是最大匹配。这说明该边是可行的。
如果该边满流&&scc[u]!=scc[v],那么该边是必须边。
因为如果去掉这条边,最大匹配会减少1(想想看),所以该边必须出现在最大匹配中。
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 100000+5 14 #define maxm 100000+5 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 23 #define mod 1000000007 24 using namespace std; 25 inline int read() 26 { 27 int x=0,f=1;char ch=getchar(); 28 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 29 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 30 return x*f; 31 } 32 int n,m,s,t,maxflow,cnt,ti,top,tot=1,a[maxn],sta[maxn],low[maxn],dfn[maxn],scc[maxn],head[maxn],cur[maxn],h[maxn]; 33 queue<int>q; 34 struct edge{int go,next,v;}e[maxm]; 35 void add(int x,int y,int v) 36 { 37 e[++tot]=(edge){y,head[x],v};head[x]=tot; 38 e[++tot]=(edge){x,head[y],0};head[y]=tot; 39 } 40 bool bfs() 41 { 42 for(int i=s;i<=t;i++)h[i]=-1; 43 q.push(s);h[s]=0; 44 while(!q.empty()) 45 { 46 int x=q.front();q.pop(); 47 for(int i=head[x];i;i=e[i].next) 48 if(e[i].v&&h[e[i].go]==-1) 49 { 50 h[e[i].go]=h[x]+1;q.push(e[i].go); 51 } 52 } 53 return h[t]!=-1; 54 } 55 int dfs(int x,int f) 56 { 57 if(x==t) return f; 58 int tmp,used=0; 59 for(int i=cur[x];i;i=e[i].next) 60 if(e[i].v&&h[e[i].go]==h[x]+1) 61 { 62 tmp=dfs(e[i].go,min(e[i].v,f-used)); 63 e[i].v-=tmp;if(e[i].v)cur[x]=i; 64 e[i^1].v+=tmp;used+=tmp; 65 if(used==f)return f; 66 } 67 if(!used) h[x]=-1; 68 return used; 69 } 70 void dinic() 71 { 72 maxflow=0; 73 while(bfs()) 74 { 75 for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf); 76 } 77 } 78 void tarjan(int x) 79 { 80 low[x]=dfn[x]=++ti;sta[++top]=x; 81 for4(i,x)if(e[i].v) 82 { 83 if (!dfn[y]) 84 { 85 tarjan(y); 86 low[x]=min(low[x],low[y]); 87 } 88 else if(!scc[y])low[x]=min(low[x],dfn[y]); 89 } 90 if(low[x]==dfn[x]) 91 { 92 cnt++; 93 for(int y=-1;y!=x;scc[y=sta[top--]]=cnt); 94 } 95 } 96 char ch[10]; 97 string st; 98 map<string,int>mp; 99 int main() 100 { 101 freopen("input.txt","r",stdin); 102 freopen("output.txt","w",stdout); 103 n=read();s=0;t=2*n+1; 104 for1(i,n) 105 for0(j,1) 106 { 107 memset(ch,0,sizeof(ch));scanf("%s",ch);st=ch; 108 mp[st]=2*i-1+j; 109 } 110 for1(i,n)add(s,2*i-1,1),add(2*i,t,1),add(2*i-1,2*i,1),a[i]=tot-1; 111 m=read(); 112 while(m--) 113 { 114 memset(ch,0,sizeof(ch));scanf("%s",ch);st=ch; 115 int x=mp[st]; 116 memset(ch,0,sizeof(ch));scanf("%s",ch);st=ch; 117 int y=mp[st]; 118 add(x,y,1); 119 } 120 dinic(); 121 for2(i,s,t)if(!dfn[i])tarjan(i); 122 //for2(i,s,t)cout<<i<<' '<<scc[i]<<endl; 123 for1(i,n)printf("%s\n",(!e[a[i]].v&&scc[e[a[i]].go]!=scc[e[a[i]^1].go])?"Safe":"Unsafe"); 124 return 0; 125 }