P1407-[国家集训队]稳定婚姻【tarjan,强连通分量】
生活随笔
收集整理的這篇文章主要介紹了
P1407-[国家集训队]稳定婚姻【tarjan,强连通分量】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
題目鏈接:https://www.luogu.org/problemnew/show/P1407
題目大意
若干對夫妻,和若干對綠色關系,求每對夫妻離婚后,綠色關系是否可以重新讓每個人兩兩配對。
解題思路
我們可以讓
夫妻女的連男的
交往男的連女的
然后跑tarjantarjantarjan,若一對夫妻在同一個聯通分量里,那么一點有辦法使得男的可以到女的,而這條路徑一定是偶數個點,只要讓這條路徑兩兩配對就可以重新構成關系。
codecodecode
#include<cstdio> #include<string> #include<algorithm> #include<iostream> #include<map> #include<stack> using namespace std; const int N=9000,M=70000; struct edge{int to,next; }a[M]; map<string,int> num; stack<int> s; int tot,h,l[N],dfn[N],n,lt,inlt[N]; int low[N],ls[N],cnt; bool v[N]; void addl(int x,int y) {a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot; } void tarjan(int x) {dfn[x]=low[x]=++cnt;s.push(x);v[x]=1;for(int i=ls[x];i;i=a[i].next){int y=a[i].to;if(!dfn[y]){tarjan(y);low[x]=min(low[x],low[y]);}else if(v[y])low[x]=min(low[x],dfn[y]);}if(low[x]==dfn[x]){++lt;s.push(1);do{s.pop();inlt[s.top()]=lt;v[s.top()]=0;}while(s.top()!=x);s.pop();} } int main() {tot=1;scanf("%d",&h);for(int i=1;i<=h;i++){string a,b;cin>>a>>b;num[a]=++n;num[b]=++n;addl(n-1,n);}scanf("%d",&h);for(int i=1;i<=h;i++){string a,b;cin>>a>>b;addl(num[b],num[a]);}for(int i=1;i<=n;i++)if(!dfn[i]) tarjan(i);for(int i=1;i<=n;i+=2)if(inlt[i]!=inlt[i+1]) printf("Safe\n");else printf("Unsafe\n"); }總結
以上是生活随笔為你收集整理的P1407-[国家集训队]稳定婚姻【tarjan,强连通分量】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无线机械键盘颜值与实力的碰撞无线机械键盘
- 下一篇: jzoj4671-World Tour【