"Tarjan" bzoj2140-stable marriage

Source: Internet
Author: User

Aka NTR's story.

"The main topic"

n Pairs of couples bi and GI. If a male bi and a female GJ once had intercourse, they have the possibility of elope. You may want to set up bi and GJ old flame, and then BJ will contact his first love, GK, to recursion. If the bi and GI divorce under the premise of the 2n individuals will eventually still be able to combine into a couple of N, then we call marriage I is unsafe, or marriage i is safe. Is it safe to ask about the marriage of n couples?

Ideas

The first reaction was the Hungarian algorithm, but it was too violent and too much.

We put the husband and wife to the male, the old man even to the woman. It can be concluded that if the strongly connected components of the graph are in the same strong connected component, then their marriage is unsafe or their marriage is safe.

Why is it? If in the same strong connected component, obviously can be connected to an augmented path, equivalent to the Hungarian algorithm can run, then must be able to form a new n pairs of couples.

If it is not in a strong connected component, it can be understood that the Hungarian algorithm cannot be adjusted (see the Hungarian algorithm for specific reasons), then must not form a new n couples.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <map>6#include <vector>7#include <stack>8 using namespacestd;9map<string,int>Name;Ten Const intmaxn=4000+ -; One intn,m; A intcnt,col,dfn[maxn*2],low[maxn*2],instack[maxn*2],tar[maxn*2]; -vector<int> e[maxn*2]; -stack<int>S; the  - voidAddedge (intUintv) {E[u].push_back (v);} -  - voidTarjan (intu) + { -low[u]=dfn[u]=++CNT; + s.push (u); Ainstack[u]=1;//Don't forget these two sentences. at      for(intI=0; I<e[u].size (); i++) -     { -         intv=E[u][i]; -         if(!Instack[v]) -         { - Tarjan (v); inlow[u]=min (low[u],low[v]); -         } to         Else if(instack[v]==1) low[u]=min (low[u],dfn[v]); +     } -      the     if(low[u]==Dfn[u]) *     { $++Col;Panax Notoginseng          while(S.top ()! =u) -         { theTar[s.top ()]=col,instack[s.top ()]=2; + S.pop (); A         } thetar[u]=col,instack[u]=2; + S.pop (); -     } $ } $  - voidInit () - { thescanf"%d",&n); -      for(intI=1; i<=n;i++)Wuyi     { the         Charwife[ the],husband[ the]; -scanf"%s%s", Wife,husband); Wuname[wife]=i; -name[husband]=i+N; AboutAddedge (i,i+n); $     } -scanf"%d",&m); -      for(intI=1; i<=m;i++) -     { A         Charexgf[ the],exbf[ the]; +scanf"%s%s", EXGF,EXBF); the         intexgf=name[exgf],exbf=NAME[EXBF]; - Addedge (EXBF,EXGF); $     } the } the  the voidSolve () the { -Cnt=col=0; in      while(!S.empty ()) S.pop (); thememset (Instack,0,sizeof(Instack)); the      for(intj=1; j<=2*n;j++)if(!Instack[j]) Tarjan (j); About      for(intI=1; i<=n;i++) the         if(Tar[i]==tar[i+n]) puts ("Unsafe"); the             ElsePuts"Safe"); the } +  - intMain () the {Bayi init (); the solve (); the     return 0; -}

"Tarjan" bzoj2140-stable marriage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.