poj1904

来源:互联网 发布:欧几里得算法 编辑:程序博客网 时间:2024/06/05 23:05

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

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

Sample Output

2 1 22 1 21 3

1 4

每个王子在能保证每个王子都有伴的情况下,可以随机选择的公主有哪些。

如果王子a1选择的不是自己本来对应的那个b1,而是选择了b2,那么a2一定是可以选择a1的。

这样才能符合要求(也可能选择b3啥的,这里简化一下。)

这说明a1指向b1,b2.

a2指向b1,b2.

又因为b1指向a1,b2指向a2.这个是大臣给的推荐结婚。

这样我们会发现这是一个强连通分量,也就是说只有强连通分量中的王子才可以互相随便换公主,而保证被抢了公主的那个王子还

可以选择其他的公主。

所以我们把整个图进行缩点,对于每个强连通分量内部,就是对应的王子可以选择的。

  1. #include <iostream>  
  2. #include <vector>  
  3. #include <stack>  
  4. using namespace std;  
  5. #define MAXV 4020  
  6. #define min(a,b) (a>b?b:a)  
  7.   
  8. vector <int>ans;  
  9. stack <int>stap;  
  10.   
  11. int map[MAXV][MAXV];  
  12. int dfn[MAXV],low[MAXV],belong[MAXV];  
  13. int count,n,cnt;  
  14. bool instack[MAXV];  
  15.   
  16. void init(){  
  17.     count=cnt=0;  
  18.     memset(dfn,0,sizeof(dfn));  
  19.     memset(low,0,sizeof(low));  
  20.     memset(instack,0,sizeof(instack));  
  21. }  
  22.   
  23. void tarjan(int x){  
  24.     int i;  
  25.     dfn[x]=low[x]=++cnt;  
  26.     stap.push(x);  
  27.     instack[x]=true;  
  28.     for(i=1;i<=n<<1;i++){  
  29.         if(!map[x][i]) continue;  
  30.         if(!dfn[i]){  
  31.             tarjan(i);  
  32.             low[x]=min(low[i],low[x]);  
  33.         }else if(instack[i])  
  34.             low[x]=min(dfn[i],low[x]);  
  35.     }  
  36.       
  37.     if(low[x]==dfn[x]){  
  38.         count++;  
  39.         while(1){  
  40.             int tmp=stap.top();  
  41.             stap.pop();  
  42.             belong[tmp]=count;  
  43.             instack[tmp]=false;  
  44.             if(tmp==x) break;  
  45.         }  
  46.     }  
  47. }  
  48.   
  49. void work(){  
  50.     init();  
  51.     for(int i=1;i<=2*n;i++)  
  52.         if(!dfn[i]) tarjan(i);  
  53. }  
  54.   
  55. int main(){  
  56.     int i,j,num;  
  57.     scanf("%d",&n);  
  58.     for(i=1;i<=n;i++){  
  59.         scanf("%d",&num);  
  60.         while(num--){  
  61.             scanf("%d",&j);  
  62.             map[i][j+n]=1;  
  63.         }  
  64.     }  
  65.     for(i=1;i<=n;i++){  
  66.         scanf("%d",&j);  
  67.         map[j+n][i]=1;  
  68.     }  
  69.     work();  
  70.     for(i=1;i<=n;i++){  
  71.         ans.clear();  
  72.         for(j=n+1;j<=n<<1;j++){  
  73.             if(map[i][j] && belong[i]==belong[j]){  
  74.                 ans.push_back(j-n);  
  75.             }  
  76.         }  
  77.         printf("%d",ans.size());  
  78.         for(j=0;j<ans.size();j++) printf(" %d",ans[j]);  
  79.         printf("\n");  
  80.     }  
  81.     return 0;  
  82. }  

原创粉丝点击