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.

Source

Northeastern Europe 2003 又想吐槽一下欧洲题简单了==


分类里面说是tarjan+并查集,我就傻乎乎的想怎么去即用tarjan又用并查集==然而其实感觉就是长得像并查集,强连通分量不也长得像并查集嘛╮(╯_╰)╭

为什么要给你一组正确结果呢?为了让你反向建边啊!要不只有连接从王子到女孩这么一个方向的边啊~这样的话 ,很明显看出来是有强连通分量的图,然而……女孩只会嫁给爱她的王子!并不是强连通分量中的每一对都能输出==解决这个问题的时候,脑残了一下,为什么非要用数组导一下呢?vector自带size()啊 (⊙﹏⊙)b。还有,有必要总结一下自己犯过的没长脑子的问题了→_→判断exist[i][a+2000]=true;不能都不管+的2000! exist[i][j]能一样么???

不过挺开心自己算是改明白了^_^

/***********
poj1904
2015.11.10
18028K	11047MS	G++
***********/
#include <iostream>
#include<cstdio>
#include<stack>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 2005
vector<int>G[maxn<<1];
vector<int>ans;
int pre[maxn<<1],lowlink[maxn<<1],sccno[maxn<<1],dfs_clock,scc_cnt;
stack<int>S;
int ccount[maxn<<1];
bool exist[maxn<<1][maxn<<1];
void dfs(int u)
{
    pre[u]=lowlink[u]=++dfs_clock;
    S.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        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++;
       // printf("序号是%d  ,里面有",scc_cnt);
        for(;;)
        {
            int x=S.top();S.pop();
            sccno[x]=scc_cnt;
           // ccount[scc_cnt]++;
           // printf("%d   ",x+1);
            if(x==u) break;
        }
    }
}
void find_scc(int n)
{
    dfs_clock=scc_cnt=0;
    memset(sccno,0,sizeof(sccno));
    memset(pre,0,sizeof(pre));
    for(int i=0;i<n;i++) if(!pre[i]) dfs(i);
}
int main()
{
 //   freopen("cin.txt","r",stdin);
    int n,k,a;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++) G[i].clear();
       // memset(ccount,0,sizeof(ccount));
        memset(exist,false,sizeof(exist));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&a);
                a--;
                G[i].push_back(a+2000);
                exist[i][a+2000]=true;
            }
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            a--;
            G[a+2000].push_back(i);
            exist[a+2000][i]=true;
        }
        find_scc(n);
        /**********
        for(int i=0;i<n;i++)
        {
            printf("count=%d",ccount[sccno[i]]/2);
            for(int j=2000;j<2000+n;j++)
            {
                if(sccno[i]==sccno[j]) printf(" %d",j-2000+1);
            }
            printf("\n");
        }
        **********/
        for(int i=0;i<n;i++)
        {
            ans.clear();
            for(int j=2000;j<2000+n;j++)
            {
                if(exist[i][j]&&sccno[i]==sccno[j])
                    ans.push_back(j-2000);
            }
            printf("%d",ans.size());
            for(int j=0;j<ans.size();j++) printf(" %d",ans[j]+1);
            printf("\n");
        }
    }
    return 0;
}