King's Quest
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 9460   Accepted: 3497
Case Time Limit: 2000MS

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<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=4e6+;
vector<int>sc[N];
vector<int>G[N];
int head[N],dfn[N],low[N],q[N];
bool instack[N],mp[][];
int tot,scnt,l,cnt;
struct node
{
int to,next;
} e[M];
void add(int u,int v)
{
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++cnt;
q[l++]=u;
instack[u]=;
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].to;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]&&dfn[v]<low[u]) low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
++scnt;
int t;
do
{
t=q[--l];
sc[scnt].push_back(t);
instack[t]=;
}
while(t!=u);
}
}
int main()
{
int n,x,y;
while(scanf("%d",&n)!=EOF)
{
tot=scnt=cnt=l=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(mp,,sizeof(mp));
for(int i=; i<=n; ++i)
{
scanf("%d",&x);
while(x--)
{
scanf("%d",&y);
add(i,y+n);
G[i].clear();
sc[i].clear();
mp[i][y]=;
}
}
for(int i=; i<=n; ++i)
{
scanf("%d",&x);
add(x+n,i);
}
for(int i=; i<=n; ++i) if(!dfn[i]) Tarjan(i);
for(int i=; i<=scnt; ++i) sort(sc[i].begin(),sc[i].end());
for(int i=; i<=scnt; ++i)
{
int tc=upper_bound(sc[i].begin(),sc[i].end(),n)-sc[i].begin();
for(int j=; j<tc; ++j) for(int k=tc; k<(int)sc[i].size(); ++k) if(mp[sc[i][j]][sc[i][k]-n] ) G[sc[i][j]].push_back(sc[i][k]-n);
}
for(int i=; i<=n; ++i)
{
printf("%d",(int)G[i].size());
for(int j=; j<(int)G[i].size(); ++j) printf(" %d",G[i][j]);
puts("");
}
}
}

最新文章

  1. $\LaTeX$笔记:Section 编号方式(数字、字母、罗马)&amp;计数器计数形式修改
  2. 【ipv6惹的祸】curl 超时
  3. 使用ActivityManager实现进程管理
  4. nginx server中的server_name配置的域名在客户机上无法访问
  5. Android(Xamarin)之旅(四)
  6. 纯css3代码写九宫格效果
  7. 开篇 hello 内Cool超人
  8. [AngularJS] New in Angular 1.3 - bindToController
  9. Struts2使用Interceptor实现权限控制的应用实例详解
  10. svn up出现类似svn: Error converting entry in directory &#39;.&#39; to UTF-8问题解决
  11. 大神教你如何解决Linux系统80端口被占用
  12. Xshell 连接Linux服务器自动中断问题
  13. linux install Openvino
  14. 从Oracle数据库中查询前几个月数据时需要注意的一些问题
  15. 高能天气——团队Scrum冲刺阶段-Day 2
  16. 使用 ImageEnView 给图片加水印,及建缩略图
  17. NGINX如何反向代理Tomcat并且实现Session保持
  18. 微信小程序组件 加减号弹出框
  19. QTREE5 - Query on a tree V(LCT)
  20. .net将List序列转为Json字符串

热门文章

  1. Spring Boot JPA中关联表的使用
  2. QML-AES加解密小工具
  3. Linux系统目录结构:目录层次标准、常用目录和文件
  4. Node.js快速创建一个访问html文件的服务器
  5. HTML入门(HB、DW)
  6. CCF系列奖获奖名单公布,鲍虎军、周志华获CCF王选奖 | CNCC 2017
  7. Android FrameWork学习(二)Android系统源码调试
  8. 解析.xml并保存结点信息至.txt中
  9. 数学--数论--HDU2136 Largest prime factor 线性筛法变形
  10. 使用 kind 快速搭建 kubernetes 环境