poj 1904 King's Quest 【图论-强连通分量-二分图匹配】

来源:互联网 发布:网络在生活中的应用 编辑:程序博客网 时间:2024/05/18 18:42
                                    King's Quest                Time Limit: 15000MS     Memory Limit: 65536K                            Case Time Limit: 2000MS

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 could marry her. And the king’s wizard did it – for each son the girl that he could 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 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 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 would 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.

题目大意:一个国王有n个王子,同时有n个女孩。每个王子都有自己喜欢的若干个女孩,现给定一个合法的完备匹配(也就是一个王子娶其中一个自己喜欢女孩),求每个王子可以选择哪些女孩可以让剩下的每个王子依旧能够选择到自己喜欢的一个女孩。

知识点:强连通分量、二分图匹配

AC代码:

# include <cstdio># include <cstring># include <algorithm>using namespace std;# define MAXN 4005struct EDGE{    int v;    int next;}edge[MAXN * 100]; //刚开始由于给边集开的数组太小,wa了好多次,感觉无语^-_-^int tot;int top;int index;int scc;int stack[MAXN];int instack[MAXN];int head[MAXN];int low[MAXN];int dfn[MAXN];int belong[MAXN];int result[MAXN];void Init(){    tot = 0;    top = 0;    index = 0;    scc = 0;    memset(head, -1, sizeof(head));    memset(dfn, 0, sizeof(dfn));    memset(low, 0, sizeof(low));    memset(belong, 0, sizeof(belong));    memset(instack, 0, sizeof(instack));    memset(stack, 0, sizeof(stack));}void Addedge(int u, int v){    edge[tot].v = v;    edge[tot].next = head[u];    head[u] = tot++;}void Tarjan(int u){    int v;    low[u] = dfn[u] = ++index;    stack[top++] = u;    instack[u] = 1;    for (int i = head[u]; i != -1; i = edge[i].next)    {        v = edge[i].v;        if (!dfn[v])        {            Tarjan(v);            if (low[v] < low[u])            {                low[u] = low[v];            }        }           else if (instack[v] && dfn[v] < low[u])        {            low[u] = dfn[v];        }    }    if (dfn[u] == low[u])    {        scc++;        do        {            v = stack[--top];            instack[v] = 0;            belong[v] = scc;        } while (v != u);    }}void Solve(int n){    int i, j;    for (i = 1; i <= n; i++)    {        if (!dfn[i])        {            Tarjan(i);        }    }    int k;    for (int u = 1; u <= n; u++)    {        k = 0;        for (j = head[u]; j != -1; j = edge[j].next)        {            int v = edge[j].v;            if (belong[u] == belong[v])            {                result[k++] = v;            }        }        sort(result, result + k);        printf("%d", k);        for (j = 0; j < k; j++)        {            printf(" %d", result[j] - n);        }        printf("\n");    }}int main(void){    int n;    while (~scanf("%d", &n))    {        int i, j, k, ps;        Init();        for (i = 1; i <= n; i++)        {            scanf("%d", &k);            for (j = 1; j <= k; j++)            {                scanf("%d", &ps);                Addedge(i, ps + n);            }        }        for (i = 1; i <= n; i++)        {            scanf("%d", &ps);            Addedge(ps + n, i);        }        Solve(n);    }    return 0;}
0 0
原创粉丝点击