锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
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
棰樻剰錛氱粰鍑轟竴涓猲瀵筺鐨勪簩鍒嗗浘浠ュ強瀹冪殑涓涓渶澶у尮閰峂錛岄棶瀵逛竴涓偣錛屽彲浠ラ変粈涔堢偣錛屼嬌寰楀叾浠栫偣浠嶇劧鍙互寰楀埌鏈澶у尮閰嶃?br>瀵逛簬xi鍠滄鐨刴m錛岃繛涓鏉″崟鍚戣竟錛屽浜庣粰鍑虹殑鍖歸厤錛屼粠mm榪炲悜閭d釜鐢風(fēng)敓銆?br>鍒嗘瀽錛氬浜庣粰鍑虹殑鍖歸厤涓璛闆嗗悎錛屼粠xi鍑哄彂鐪嬬湅鏄惁鏈夊叾浠栦氦閿欒礬寰勶紝浣垮緱xi鍙互閫夊叾浠栧コ浜恒傚浜庣粰鍑虹殑鍖歸厤xi->yi,鐜板湪灝濊瘯浠巟i鍑哄彂閫夊埆鐨勫コ瀛﹛i->Y-yi錛岀湅鏈鍚庤兘鍚﹁揪鍒扮粓鐐箉i錛屽艦鎴愪竴涓幆銆傚鏋滃瓨鍦ㄨ繖鏍風(fēng)殑璺緞錛屽垯璺緞浼氱粡榪囦竴鏍峰鐨剎鐐硅窡y鐐癸紝浣垮緱涓涓瀵瑰簲錛岃繖鏉¤礬寰勪腑鍒嗕負(fù)涓ゆ潯浜ら敊璺緞錛屼竴涓槸鍘熸潵鐨勫尮閰嶏紝涓涓槸鍙︿竴涓彲琛屾浛鎹㈠尮閰嶏紝閫氳繃濂充漢璧板埌鐢蜂漢鐨勬槸緇欏畾鐨凪涓殑杈癸紝浠庣敺浜鴻蛋鍒板コ浜虹殑鏄潪鍖歸厤鐨勮竟銆傞偅涓涓嚭鍙戠偣xi鍙互瀛樺湪澶氫釜榪欐牱鐨勭幆錛岄棶棰樺氨鎴愪負(fù)鎵懼己榪為氬垎閲忥紝xi鍙互閰嶅鐨勫コ浜哄氨鍦ㄩ偅榪為氬垎閲忛噷銆?
#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;
}
}
void add(int u, int v)
{
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寮濮?/span>
instack[u] = 1;//鏍囪榪涙爤
for (k = g[u]; k != -1; k = fn[k].next)
{
v = fn[k].v;
if (dfn[v] == 0)//dfn鏄椂闂存埑錛岄兘鍒濆鍖栦負(fù)0錛岃璁塊棶榪囩殑搴楀甫鏈夋椂闂?/span>
{
dfs(v);
low[u] = Min(low[u], low[v]);
}
else if (instack[v])//濡傛灉鍦ㄦ爤涓紝鍒欒〃紺鴻鐐規(guī)病琚綊鍏ヤ換浣曚竴涓猻cc涓紝榪樺湪褰撳墠鎼滅儲鏍?wèi)涓?/span>
{
low[u] = Min(low[u], low[v]);
}
}
if (dfn[u] == low[u])
{
id++;
do
{
v = stack[top--];
instack[v] = 0;//闃叉浜ゅ弶杈歸暱鏃惰繘琛宭ow[u] = Min(low[u], low[v]).鍥犱負(fù)v宸茬粡灞炰簬涓涓繛閫氬垎閲?/span>
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;//瑕佽涓?
scc[i] = i;
instack[i] = 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);
add(i, v + n);
}
}
for (i = 1; i <= n; i++)
{
scanf("%d", &v);
add(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
*/
]]>