[Connected graph | bipartite matching + strongly connected component] POJ-1904 King & #39; s Quest, poj-1904quest

Source: Internet
Author: User
Tags integer numbers

[Connected graph | bipartite matching + strongly connected component] POJ-1904 King's Quest, poj-1904quest

King's Quest
Time Limit: 15000 MS Memory Limit: 65536 K
Case Time Limit: 2000 MS

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 about each of his sons which of those girls he did like. the sons of the king were young and light-headed, so it was possible for one 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 coshould marry her. and the king's wizard did it-for each son the girl that he coshould marry was chosen, so that he liked this girl and, of course, 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 you have made, but I am not completely satisfied. for each son I wowould 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 be able to choose the girl he likes to marry."

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 king's sons contain the list of the girls he likes: first Ki-the number of those girls, and then Ki different integer numbers, ranging from 1 to N denoting the girls. the sum of all Ki does 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 wowould marry in compliance with this list. it is guaranteed that the list is correct, that 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 that after his marriage it is possible to marry each of the other king's sons. after that print Li different integer numbers denoting those girls, in ascending order.

Sample Input

4
2 1 2
2 1 2
2 2 3
2 3 4
1 2 3 4

Sample Output

2 1 2
2 1 2
1 3
1 4

Hint
This problem has huge input and output data, use scanf () and printf () instead of cin and cout to read data to avoid time limit exceed.

Source
Northeastern Europe 2003.

Question:A bipartite graph and a complete match are given to find all Y points that can be matched for each X point, so that a complete match can still be obtained.
Ideas:
For more information, see Polla.
As soon as I saw this question, I would like to look at the problem of stable marriage in the bipartite graph. (although I am going to look at it directly, this question has nothing to do with stable marriage ). If you ignore the given complete match, you can enumerate all the Y points to match X, and then judge the binary match. However, the complexity is too high, so it is bound to TLE.
The provided complete match must contain some useful information. Since all the information of X is provided in the input data, the complete match must contain some information about Y. Some X points cannot be combined with Y. For example, in the example, the No. 3 Prince and the No. 2 girl cannot be together, prince Wang and PRINCE 2 are bound to have a single person. Then the information provided by the complete match is "X points that every Y point can like ". Although not all, It is enough.
X points are connected to the desired Y point, and then the Y point in the complete match is connected to the X point. Assume that the complete match is all Xi and Yi matches, then if Xi and Yj match, Yi and Xj are bound to find another partner, imagine that Xi and Yj are in the same strongly connected component, and the strongly connected component must contain a ring, so Xj and Yi must be able to find another partner.
Does it mean that if a set of complete matches are provided, all other complete matches are in the same strongly connected component?
I think there is nothing wrong with this. All complete matching schemes should have some common information. That is, it must be a ring.
P.S. If the output plug-in is used, the G ++ evaluation can be 500 + ms. However, I/O plug-ins are ineffective in C ++ evaluation.
The Code is as follows:

/* * ID: j.sure.1 * PROG: * LANG: C++ */#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define PB push_back#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;const double eps = 1e-8;/****************************************/const int N = 4444, M = 222222;int n, tot, scc_cnt, deep;int head[N], dfn[N], scc_id[N];struct Edge {    int v, next;    Edge(){}    Edge(int _v, int _next):        v(_v), next(_next){}}e[M];stack <int> s;vector<int> ans;void init(){    tot = deep = scc_cnt = 0;    memset(head, -1, sizeof(head));    memset(dfn, 0, sizeof(dfn));    memset(scc_id, 0, sizeof(scc_id));}void add(int u, int v){    e[tot] = Edge(v, head[u]);    head[u] = tot++;}int dfs(int u){    int lowu = dfn[u] = ++deep;    s.push(u);    for(int i = head[u]; ~i; i = e[i].next) {        int v = e[i].v;        if(!dfn[v]) {            int lowv = dfs(v);            lowu = min(lowu, lowv);        }        else if(!scc_id[v]) {            lowu = min(lowu, dfn[v]);        }    }    if(lowu == dfn[u]) {        scc_cnt++;        while(1) {            int x= s.top(); s.pop();            scc_id[x] = scc_cnt;            if(x == u) break;        }    }    return lowu;}void tarjan(){    for(int i = 1; i <= 2*n; i++) {        if(!dfn[i]) dfs(i);    }}int main(){#ifdef J_Sure    freopen("000.in", "r", stdin);    //freopen("999.out", "w", stdout);#endif    while(~scanf("%d", &n)) {        int m, j;        init();        for(int i = 1; i <= n; i++) {            scanf("%d", &m);            while(m--) {                scanf("%d", &j);                add(i, j+n);            }        }        for(int i = 1; i <= n; i++) {            scanf("%d", &j);            add(j+n, i);        }        tarjan();        for(int u = 1; u <= n; u++) {            ans.clear();            for(int i = head[u]; ~i; i = e[i].next) {                int v = e[i].v;                if(scc_id[u] == scc_id[v]) ans.PB(v-n);            }            size_t len = ans.size();            printf("%d", (int)len);            sort(ans.begin(), ans.end());            for(size_t k = 0; k < len; k++) {                printf(" %d", ans[k]);            }            puts("");        }    }    return 0;}

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.