POJ-1904 King's Quest (strong connected component + binary graph matching)

Source: Internet
Author: User

The main topic: There are N handsome men and n beauty, now give each handsome girl's favorite number, and a handsome and beautiful match the perfect
Ask each handsome man can marry how many beauty, and when he married this beautiful woman, the rest of the people can also be a perfect match

Problem-solving ideas: A question, a detailed answer to a great God
Specifically, first build the edge, the handsome man and can marry the beauty of the side, and then the perfect match of beauty and handsome man even edge, so that formed a map of the
Then, to find out that all the strong connected components of the graph, in the strong connected components of the handsome men can marry their favorite beauty, and after marrying this beauty, will not affect other people
Why is it?
Suppose Xi is handsome, Yi and yj for beauties, assuming the perfect match given in Xi and Yi match.
Now, we want XI and YJ to match, according to the binary graph match, first to find an augmented chain
Because to perfect match, so this augmented chain will definitely point to Yi's, that if the Yi is connected to Xi, is not formed a ring, then this ring is definitely a strong connected component.
So, if Xi can choose YJ, then XI and YJ must be in the same connected component.

#include <cstdio>#include <vector>#include <cstring>using namespace STD;#define N 4010#define M 1000010#define S#define MIN (a) < (b)? (a): (b))#define MAX (a) > (b)? (a): (b))structedge{intFrom, to, next;} E[M];intPre[n], Sccno[n],Stack[N], lowlink[n], head[n];intN, M, Dfs_clock, scc_cnt, top, tot;intLove[s][s];voidDfsintu) {pre[u] = lowlink[u] = ++dfs_clock;Stack[++top] = u;intV for(inti = Head[u]; I! =-1; i = e[i].next) {v = e[i].to;if(!pre[v])            {DFS (v);        Lowlink[u] = min (Lowlink[u], lowlink[v]); }Else if(!sccno[v])        {Lowlink[u] = min (Lowlink[u], pre[v]); }    }if(Lowlink[u] = = Pre[u]) {scc_cnt++; while(1) {v =Stack[top--]; SCCNO[V] = scc_cnt;if(v = = u) Break; }    }} vector<int>V[n];voidSolve () {memset(Pre,0,sizeof(pre));memset(Sccno,0,sizeof(SCCNO)); Dfs_clock = scc_cnt = top =0; for(inti =0; I <2N i++)if(!pre[i]) DFS (i); for(inti =0; I < n; i++) v[i].clear (); for(inti =0; I < n; i++) { for(intj =0; J < N; J + +) {if(Sccno[i] = = Sccno[j + N] && love[i][j]) V[i].push_back (j +1); }printf("%d", V[i].size ()); for(intj =0; J < V[i].size (); J + +)printf("%d", V[i][j]);printf("\ n"); }}voidAddedge (intUintV) {e[tot].from = u;    E[tot].to =v;    E[tot].next = Head[u]; Head[u] = tot++;}voidInit () {memset(Head,-1,sizeof(head));memset(Love,0,sizeof(Love)); tot =0;intT, u, v; for(U =0; U < n; u++) {scanf("%d", &t); while(t--) {scanf("%d", &v); Love[u][v-1] =true; Addedge (U, v + N-1); }    } for(inti =0; I < n; i++) {scanf("%d", &v); Addedge (V-1+ N, i); }}intMain () { while(scanf("%d", &n)! = EOF) {init ();    Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ-1904 King's Quest (strong connected component + binary graph matching)

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.