King's Quest

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

#include <iostream>
#include
<stdlib.h>
#include
<stdio.h>
#define maxn 4100
#define Min(a, b) a < b ? a : b
using namespace std;
struct T
{

int v, next;
}fn[maxn
* maxn];
int g[maxn], ans[maxn];
int stack[maxn], visit[maxn], scc[maxn], dfn[maxn], low[maxn], instack[maxn];
int n, th, top, id, time;
int cmp(const void * a, const void * b)
{

return *((int*)a) - *((int*)b);
}
void set()
{

int i;
th
= 1;

for (i = 0; i <= 2 * n; i++)
{
g[i]
= -1;
}
}
{
fn[th].v
= v, fn[th].next = g[u], g[u] = th++;
}
void dfs(int u)
{

int v, k;
dfn[u]
= low[u] = ++time;
stack[
++top] = u;//栈从1开始
instack[u] = 1;//标记进栈
for (k = g[u]; k != -1; k = fn[k].next)
{
v
= fn[k].v;

if (dfn[v] == 0)//dfn是时间戳，都初始化为0，被访问过的店带有时间
{
dfs(v);
low[u]
= Min(low[u], low[v]);
}

else if (instack[v])//如果在栈中，则表示该点没被归入任何一个scc中，还在当前搜索树中
{
low[u]
= Min(low[u], low[v]);
}
}

if (dfn[u] == low[u])
{
id
++;

do
{
v
= stack[top--];
instack[v]
= 0;//防止交叉边长时进行low[u] = Min(low[u], low[v]).因为v已经属于一个连通分量
scc[v] = id;
}
while (v != u);
}
}
void tarjan()
{
id
= 0, top = 0, time = 0;

int t = 0, i;

for (i = 1; i <= 2 * n; i++)
{
dfn[i]
= 0;//要设为0
scc[i] = i;
instack[i]
= 0;//要设为0
}

for (i = 1; i <= 2 * n; i++)
{

if (!dfn[i])
{
dfs(i);
}
}
}
int main()
{

int i, j, k, m, v;

while (scanf("%d"&n) - EOF)
{

set();

for (i = 1; i <= n; i++)
{
scanf(
"%d"&m);

while (m--)
{
scanf(
"%d"&v);
+ n);
}
}

for (i = 1; i <= n; i++)
{
scanf(
"%d"&v);
+ n, i);
}
tarjan();

for (i = 1; i <= n; i++)
{

for (j = g[i], k = 0; j != -1; j = fn[j].next)
{
v
= fn[j].v;

if (scc[i] == scc[v])
{
ans[k
++= v - n;
}
}
qsort(ans, k,
sizeof(int), cmp);

for (printf("%d", k), j = 0; j < k; j++)
{
printf(
" %d", ans[j]);
}printf(
"\n");
}
}

return 0;
}
/*
3
2 1 2
3 1 2 3
2 2 3
1 2 3

2
2 1 2
2 1 2
1 2

input :
5
3 3 4 5
2 4 5
1 3
3 1 2 3
3 1 4 5
5 4 3 2 1

output :
2 4 5
2 4 5
1 3
1 2
1 1

*/