poj1904 binary graph matching + strong connected components

Source: Internet
Author: User
Tags integer numbers

http://poj.org/problem?id=1904

Description

Once upon a time there lived a king and he had N sons. And there were N beautiful girls in the kingdom and the king knew on each of his sons which of those girls he does like. The sons of the king were young and light-headed, so it is possible for a son to like several girls.

So the King asked his wizard to find for each of his sons the girl he liked, so that he could marry her. And the King ' s wizard did it--for each son the girl that he could marry is chosen, so that he liked this girl and, of C Ourse, each beautiful girl had to marry only one of the king's sons.

However, the king looked at the list and said: "I like the list that you had made, but I am not completely satisfied. For each son I would like to know all the girls that he can marry. Of course, after he marries any of those girls, for each other son you must still is able to choose the girl he likes to M Arry. "

The problem the King wanted the wizard to solve had become too hard for him. You must save Wizard's head by solving this problem.

Input

The first line of the input contains n-the number of King ' s sons (1 <= N <= 2000). Next N lines for each of the king's sons contain the list of the girls he likes:first Ki-the number of those girls, and th En Ki different integer numbers, ranging from 1 to N denoting the girls. The sum of all Ki does is not exceed 200000.

The last line of the case contains the original list the wizard had made--N different integer numbers:for each son the Number of the girl he would marry in compliance with this list. It is guaranteed, and the list is correct, which is, each son likes the girl he must marry according to this list.

Output

Output N lines. For each king's son first print Li-the number of different girls he likes and can marry so, after his marriage it I s possible to marry each of the other King ' s sons. After the print Li different integer numbers denoting those girls, in ascending order.

Sample Input

42 1 22 1 22 2 32 3 41 2 3 4

Sample Output

2 1 22 1 21 31 4

Hint

This problem have huge input and output data,use scanf () and printf () instead of CIN and cout to read data to avoid time Li MIT Exceed.

/**poj1904 binary map matching + strong connected component topic: There are n princes and N princesses, each prince can only marry the princess he likes, and the Princess may marry any prince, now know that each Prince's wedding object (all have the wedding object), and each prince's favorite princess, ask each prince can and those The princess marries and guarantees that every prince has to have a marriage. The idea of solving a problem: each prince to his favorite princess even a pair of side, each princess to her wedding object with a forward edge, to find each prince's strong connected components, the Prince can and he in the same strong connected component of the Princess married, meet the conditions. Why is it? Because every prince can only marry his favorite sister, the original perfect match between the husband and wife has two different sides can reach each other, then the same strong connected component of the number of princes and sisters must be equal, if Prince X can and another sister a married,           Sister A's original Prince Y can certainly find another sister B married, because if not found, then x and a must not be in the same strong connected component. So a prince can marry all the girls who have the same strong connection with him, and this will not cause the other princes in the same strong connecting component to find a sister to marry. */#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <    stack>using namespace Std;const int maxn=5005;int head[maxn],ip;void init () {memset (head,-1,sizeof (head)); Ip=0;} struct note{int v,next;} edge[400005];void addedge (int u,int v) {edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;} int N,cnt[maxn];int dfn[maxn],low[maxn],belong[maxn],index,cnt_tar,cont[maxn],instack[maxn*2];stack<int>q;    void Tarjan (int u) {dfn[u]=low[u]=++index;    Q.push (U);    Instack[u]=1; for (iNT I=head[u]; I!=-1;        I=edge[i].next) {int v=edge[i].v;            if (!dfn[v]) {Tarjan (v);        Low[u]=min (Low[u],low[v]);        } else if (Instack[v]) {low[u]=min (low[u],dfn[v]);        }} if (Dfn[u]==low[u]) {cnt_tar++;        Int J;            do {j=q.top ();            Q.pop ();            Belong[j]=cnt_tar;            instack[j]=0;        cont[cnt_tar]++;    } while (J!=u);    }}void solve () {index=0,cnt_tar=0;    memset (dfn,0,sizeof (DFN));    memset (low,0,sizeof (Low));    memset (instack,0,sizeof (instack));    memset (Cont,0,sizeof (cont));    for (int i=1; i<=n; i++) {if (!dfn[i]) Tarjan (i);        }}int Main () {while (~SCANF ("%d", &n)) {init ();            for (int i=1; i<=n; i++) {int x, y;            scanf ("%d", &x);                for (int j=0; j<x; J + +) {scanf ("%d", &y); Addedge (i, y+n);            }} for (int i=1; i<=n; i++) {int x;            scanf ("%d", &x);        Addedge (X+n,i);        } solve ();            for (int u=1; u<=n; u++) {int k=0;                for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;                if (Belong[u]==belong[v]) {cnt[k++]=v-n;            }} sort (cnt,cnt+k);            printf ("%d", k);            for (int i=0; i<k; i++) {printf ("%d", cnt[i]);        } printf ("\ n"); }} return 0;}


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

poj1904 binary graph matching + strong connected components

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.