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

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

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

 #include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = 1e6 + ;
int n, m, u, v, tot, top, cnt, flag;
int Scan()
{
int res=,ch,flag=;
if((ch=getchar())=='-')
flag=;
else if(ch>=''&&ch<='')
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return flag?-res:res;
}
void Out(int a)
{
if(a>)
Out(a/);
putchar(a%+'');
}
struct node {
int v, next;
} edge[maxn];
int head[maxn], instack[maxn], s[maxn];
int dfn[maxn], low[maxn], belong[maxn];
void init() {
tot = cnt = top = flag = ;
memset(s, , sizeof(s));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(instack, , sizeof(instack));
}
void add(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjin(int v) {
dfn[v] = low[v] = ++flag;
instack[v] = ;
s[top++] = v;
for (int i = head[v] ; i != - ; i = edge[i].next) {
int j = edge[i].v;
if (!dfn[j]) {
tarjin(j);
low[v] = min(low[v], low[j]);
} else if (instack[j]) low[v] = min(low[v], dfn[j]);
}
if (dfn[v] == low[v]) {
cnt++;
int t;
do {
t = s[--top];
instack[t] = ;
belong[t] = cnt;
} while(t != v) ;
}
}
void solve() {
for (int i = ; i <= n ; i++)
if (!dfn[i]) tarjin(i);
}
int ans[maxn];
int main() {
while(scanf("%d", &n) != EOF) {
init();
int num;
memset(ans,,sizeof(ans));
for (int i = ; i <= n ; i++) {
num=Scan();
while(num--) {
int v;
v=Scan();
add(i, n + v);
}
}
for (int i = ; i <= n ; i++) {
int v;
v=Scan();
add(n + v, i);
}
for (int i = ; i <= *n ; i++)
if (!dfn[i]) tarjin(i);
for (int i = ; i <= n ; i++) {
int k = ;
for (int j = head[i] ; ~j ; j = edge[j].next) {
if (belong[edge[j].v] == belong[i]) ans[k++] = edge[j].v - n;
}
sort(ans, ans + k);
Out(k);
for (int i = ; i < k ; i++){
putchar(' ');
Out(ans[i]);
}
putchar('\n');
}
}
return ;
}

最新文章

  1. php实现转换html格式为文本格式的方法
  2. 在Android上用AChartEngine轻松绘制图表
  3. (实用篇)PHP ftp上传文件操作类
  4. T-SQL 运行时生成语句
  5. OCMOCM
  6. CentOS6.3配置SVN之subversion1.7.7
  7. 安装了C
  8. 在asp.net web api 2 使用 Serilog 记录日志
  9. 学习 JavaScript(二)在 HTML 中使用 JS
  10. μC/OS-II 中的任务管理
  11. nginx代理配置 配置中的静态资源配置,root 和 alias的区别。启动注意事项
  12. strdup strcpy 的区别
  13. zxing源码编译与运行
  14. Mysql 数据类型、约束类型
  15. win10家庭版 监听无法启动 报TNS-12560 TNS-00530
  16. C#和网页js互调代码
  17. 《精通Python设计模式》学习行为型之责任链模式
  18. winform禁用标题栏
  19. scrapy--json(360美图)
  20. 设计模式之第14章-命令模式(Java实现)

热门文章

  1. mac os 中如何修改顶栏图标的顺序
  2. Struct和Union在内存大小上的区别
  3. ruby中printf &quot;%x&quot;%-4为何会打印开头..
  4. Oracle常用数据库对象(片段)
  5. 实现Redhat Linux 6和Windows通过Windows Server AD统一认证并共享访问Oracle ZS存储系统
  6. IT轮子系列(五)——MVC API 文件上传,总有一款是你需要的
  7. letter combinations of a phone number(回溯)
  8. Mysql安装和登录相关操作
  9. Android Studio 插件开发详解四:填坑
  10. 微信小程序入门一