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.
 
这题第一眼看过去是二分图,看题解才知道是强连通。
喜欢就建立一条边,如果互相喜欢那么就是强连通的
男女能够结婚他们肯定在一个强连通分量里面,

这题加和不加输入输出挂差别是超级大的

下面是放的是加输入输出挂的代码

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <string>
  4 #include <algorithm>
  5 #include <queue>
  6 using namespace std;
  7 
  8 const int maxn = 1e6 + 10;
  9 int n, m, u, v, tot, top, cnt, flag;
 10 int Scan()
 11 {
 12     int res=0,ch,flag=0;
 13     if((ch=getchar())=='-')
 14         flag=1;
 15     else if(ch>='0'&&ch<='9')
 16         res=ch-'0';
 17     while((ch=getchar())>='0'&&ch<='9')
 18         res=res*10+ch-'0';
 19     return flag?-res:res;
 20 }
 21 void Out(int a)
 22 {
 23     if(a>9)
 24         Out(a/10);
 25     putchar(a%10+'0');
 26 }
 27 struct node {
 28     int v, next;
 29 } edge[maxn];
 30 int head[maxn], instack[maxn], s[maxn];
 31 int dfn[maxn], low[maxn], belong[maxn];
 32 void init() {
 33     tot = cnt = top = flag = 0;
 34     memset(s, 0, sizeof(s));
 35     memset(head, -1, sizeof(head));
 36     memset(dfn, 0, sizeof(dfn));
 37     memset(instack, 0, sizeof(instack));
 38 }
 39 void add(int u, int v) {
 40     edge[tot].v = v;
 41     edge[tot].next = head[u];
 42     head[u] = tot++;
 43 }
 44 void tarjin(int v) {
 45     dfn[v] = low[v] = ++flag;
 46     instack[v] = 1;
 47     s[top++] = v;
 48     for (int i = head[v] ; i != -1 ; i = edge[i].next) {
 49         int j = edge[i].v;
 50         if (!dfn[j]) {
 51             tarjin(j);
 52             low[v] = min(low[v], low[j]);
 53         } else if (instack[j]) low[v] = min(low[v], dfn[j]);
 54     }
 55     if (dfn[v] == low[v]) {
 56         cnt++;
 57         int t;
 58         do {
 59             t = s[--top];
 60             instack[t] = 0;
 61             belong[t] = cnt;
 62         } while(t != v) ;
 63     }
 64 }
 65 void solve() {
 66     for (int i = 1 ; i <= n ; i++)
 67         if (!dfn[i]) tarjin(i);
 68 }
 69 int ans[maxn];
 70 int main() {
 71     while(scanf("%d", &n) != EOF) {
 72         init();
 73         int num;
 74         memset(ans,0,sizeof(ans));
 75         for (int i = 1 ; i <= n ; i++) {
 76             num=Scan();
 77             while(num--) {
 78                 int v;
 79                 v=Scan();
 80                 add(i, n + v);
 81             }
 82         }
 83         for (int i = 1 ; i <= n ; i++) {
 84             int v;
 85             v=Scan();
 86             add(n + v, i);
 87         }
 88         for (int i = 1  ; i <= 2*n ; i++)
 89             if (!dfn[i]) tarjin(i);
 90         for (int i = 1 ; i <= n ; i++) {
 91             int k = 0;
 92             for (int j = head[i] ; ~j ; j = edge[j].next) {
 93                 if (belong[edge[j].v] == belong[i]) ans[k++] = edge[j].v - n;
 94             }
 95             sort(ans, ans + k);
 96             Out(k);
 97             for (int i = 0 ; i < k ; i++){
 98                 putchar(' ');
 99                 Out(ans[i]);
100             }
101             putchar('\n');
102         }
103     }
104     return 0;
105 }

 

版权声明:本文为qldabiaoge原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/qldabiaoge/p/9073805.html