POJ - 1904 King's Quest (tarjan)

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个皇子找喜欢的女孩,每个皇子必须要跟自己喜欢的女孩在一起。每个皇子有能选那几个女孩,使得皇子选完其中之一后其他皇子依然可以找到自己喜欢的女孩。

思路:tarjan缩点,皇子所能选的女孩必须和他在同一个强连通中。

#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stack>
#include<vector>
#define M 20100
using namespace std;
struct path
{
    int to,nextt;
}A[2000100];
stack<int>q;
int head[M],DFN[M],LOW[M],book[M],re[M],s[M];
int n,m,x,y,indox,carry,tot;
void init()
{
    tot=indox=carry=0;
    memset(head,-1,sizeof(head));
    memset(LOW,-1,sizeof(LOW));
    memset(DFN,-1,sizeof(DFN));
    memset(book,0,sizeof(book));
    return ;
}
void add(int u,int v)
{
    A[tot].to=v;
    A[tot].nextt=head[u];
    head[u]=tot++;
    return ;
}
void tarjan(int u)
{
    int tem;
    book[u]=1;
    q.push(u);
    DFN[u]=LOW[u]=++indox;
    for(int i=head[u];i!=-1;i=A[i].nextt)
    {
        tem=A[i].to;
        if(DFN[tem]==-1)
        {
            tarjan(tem);
            LOW[u]=min(LOW[u],LOW[tem]);
        }
        else if(book[tem])
        {
            LOW[u]=min(LOW[u],DFN[tem]);
        }
    }
    if(DFN[u]==LOW[u])
    {
        ++carry;
        do
        {
            tem=q.top();
            q.pop();
            book[tem]=0;
            re[tem]=carry;
        }while(tem!=u);
    }
    return ;
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&m);
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&y);
                add(i,y+n);
            }
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            add(x+n,i);
        }
        for(int i=1;i<=n;i++)
        {
            if(DFN[i]==-1)
                tarjan(i);
        }
        int tem,k;
        for(int i=1;i<=n;i++)
        {
            k=0;
            for(int j=head[i];j!=-1;j=A[j].nextt)
            {
                tem=A[j].to;
                if(re[i]==re[tem])
                {
                    s[k++]=tem-n;
                }
            }
            printf("%d",k);
            sort(s,s+k);
            for(int i=0;i<k;i++)
                printf(" %d",s[i]);
            printf("\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/84110270