http://poj.org/problem?id=1904

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.

/**
poj1904 二分图匹配+强连通分量
题目大意:有n个王子和n个公主,每一个王子仅仅能和他喜欢的公主结婚,公主能够和随意王子结婚。如今知道,每一个王子的结婚对象(都有结婚对象),和每一个王子所喜欢的公主,问
每一个王子能够和那些公主结婚,而且保证每一个王子都得有结婚对象
解题思路:每一个王子向他喜欢的公主连一条有向边,每一个公主向她的结婚对象连一条有向边,求取每一个王子所在的强连通分量,该王子可和他在同一个强连通分量里的公主结婚。满足
条件。为什么呢?由于每一个王子仅仅能和喜欢的妹子结婚,初始完美匹配中的丈夫和妻子之间有两条方向不同的边能够互达,则同一个强连通分量中的王子数和妹子数一定是相等的,若王子x能够和另外的一个妹子a结婚,妹子a的原配王子y肯定能找到另外一个妹子b结婚,由于假设找不到的话,则x和a必不在同一个强连通分量中。 所以一个王子能够和全部与他同一强连通分量的妹子结婚,而这不会导致同一强连通分量中的其它王子找不到妹子结婚。
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
const int maxn=5005;
int head[maxn],ip; void init()
{
memset(head,-1,sizeof(head));
ip=0;
} struct note
{
int v,next;
} edge[400005]; void addedge(int u,int v)
{
edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
} int n,cnt[maxn];
int dfn[maxn],low[maxn],belong[maxn],index,cnt_tar,cont[maxn],instack[maxn*2];
stack<int>q; void tarjan(int u)
{
dfn[u]=low[u]=++index;
q.push(u);
instack[u]=1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
cnt_tar++;
int j;
do
{
j=q.top();
q.pop();
belong[j]=cnt_tar;
instack[j]=0;
cont[cnt_tar]++;
}
while(j!=u);
}
} void solve()
{
index=0,cnt_tar=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(instack,0,sizeof(instack));
memset(cont,0,sizeof(cont));
for(int i=1; i<=n; i++)
{
if(!dfn[i])
tarjan(i);
}
} int main()
{
while(~scanf("%d",&n))
{
init();
for(int i=1; i<=n; i++)
{
int x,y;
scanf("%d",&x);
for(int j=0; j<x; j++)
{
scanf("%d",&y);
addedge(i,y+n);
}
}
for(int i=1; i<=n; i++)
{
int x;
scanf("%d",&x);
addedge(x+n,i);
}
solve();
for(int u=1; u<=n; u++)
{
int k=0;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(belong[u]==belong[v])
{
cnt[k++]=v-n;
}
}
sort(cnt,cnt+k);
printf("%d",k);
for(int i=0; i<k; i++)
{
printf(" %d",cnt[i]);
}
printf("\n");
}
}
return 0;
}

最新文章

  1. CSS属性之float学习心得
  2. hungary
  3. Sql Server 里的向上取整、向下取整、四舍五入取整
  4. 转:关于C++14:你需要知道的新特性
  5. Zookeeper 脑裂
  6. Device eth0 does not seem to be present
  7. Linux学习笔记18——信号1
  8. php 异常捕获
  9. FormData实现文件上传实例
  10. 【C++知识点】单例模式的简单实现
  11. [Swift]LeetCode486. 预测赢家 | Predict the Winner
  12. magento2 - Invalid credentials for &#39;https://repo.magento.com/packages.json&#39;, aborting.
  13. WPF Path总结(一)
  14. HDU 1263(水果统计 **)
  15. IO流-学习使人快乐2
  16. (win10)Wamp环境下php升级至PHP7.2
  17. 《嵌入式系统原理与接口技术》&mdash;&mdash;嵌入式系统接口应用基础
  18. 第3课 进化后的 const分析
  19. Week2-作业1-part2.阅读与思考
  20. php下保存远程图片到本地的函数

热门文章

  1. unnamed not found for the web module
  2. 《3+1团队》【Alpha】Scrum meeting 1
  3. linux php安装ODBC扩展
  4. 22. SCHEMA_PRIVILEGES
  5. python版 定时任务机制
  6. 前端基础之CSS_1
  7. luogu2234 [HNOI2002]营业额统计
  8. Leetcode 297.二叉树的序列化和反序列化
  9. CF651B-Beautiful Paintings
  10. 【shell】通过shell编写ping包及arp的监控并发送短信