Time Limit: 15000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

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.



有n个王子n个女生,输入时第一个数字是n,然后n行,每一行第一个数字表示该王子喜欢几个人,然后接这几个女孩的编号,第n+1行表示第i个女孩喜欢ni王子



将女孩虚拟为n+i节点,建图时i--j+n表示i王子喜欢女孩j,因为男女编号有重复,所以j+n,j+n--i表示j喜欢i,找出每一个scc里的点判断就好

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
#define MAX 5000
int low[MAX],dfn[MAX];
int head[MAX],sccno[MAX],scc_cnt,dfs_clock;
int cnt,m,n,map[MAX][MAX],rec[MAX];
bool Instack[MAX];
stack<int>s;
vector<int>scc[MAX];
struct node
{
int u,v;
int next;
}edge[MAX*100];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void getmap()
{
int num,a;
for(int i=1;i<=n;i++)
{
scanf("%d",&num);
while(num--)
{
scanf("%d",&a);
add(i,a+n);
map[i][a]=1;
}
}
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
add(a+n,i);
}
}
void tarjan(int u,int fa)
{
int v;
low[u]=dfn[u]=++dfs_clock;
s.push(u);
Instack[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(Instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
++scc_cnt;
for(;;)
{
v=s.top();
s.pop();
Instack[v]=false;
sccno[v]=scc_cnt;
scc[scc_cnt].push_back(v);
if(v==u) break;
}
}
}
void find(int l,int r)
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(Instack,false,sizeof(Instack));
memset(sccno,0,sizeof(sccno));
dfs_clock=scc_cnt=0;
for(int i=l;i<=r;i++)
if(!dfn[i])
tarjan(i,-1);
}
void slove()
{
for(int i=1;i<=n;i++)
{
int u=sccno[i];
int sum=0;
for(int j=0;j<scc[u].size();j++)
{
if(scc[u][j]>n&&map[i][scc[u][j]-n])
rec[sum++]=scc[u][j]-n;
}
sort(rec,rec+sum);
printf("%d",sum);
for(int j=0;j<sum;j++)
printf(" %d",rec[j]);
printf("\n");
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
getmap();
find(1,2*n);
slove();
}
return 0;
}

poj--1904--King's Quest(scc建图)的更多相关文章

  1. POJ 1904 King's Quest(SCC的巧妙应用,思维题!!!,经典题)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 10305   Accepted: 3798 Ca ...

  2. POJ 1904 King's Quest tarjan

    King's Quest 题目连接: http://poj.org/problem?id=1904 Description Once upon a time there lived a king an ...

  3. Poj 1904 King's Quest 强连通分量

    题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...

  4. poj 1904 King's Quest

    King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...

  5. POJ 1904 King's Quest ★(强连通分量:可行完美匹配边)

    题意 有n个女生和n个男生,给定一些关系表示男生喜欢女生(即两个人可以结婚),再给定一个初始匹配,表示这个男生和哪个女生结婚,初始匹配必定是合法的.求每个男生可以和哪几个女生可以结婚且能与所有人不发生 ...

  6. POJ 1904 King's Quest (强连通分量+完美匹配)

    <题目链接> 题目大意: 有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王 ...

  7. POJ 1904 King's Quest(强连通图)题解

    题意:n个王子有自己喜欢的ki个公主,有n个公主,每个王子只能娶一个自己喜欢的公主且不能绿别的王子.现在给你一种王子娶公主的方案,并且保证这种方案是正确的.请你给出,每个王子能娶哪些公主,要求娶这些公 ...

  8. POJ 1904 King's Quest 强联通分量+输入输出外挂

    题意:国王有n个儿子,现在这n个儿子要在n个女孩里选择自己喜欢的,有的儿子可能喜欢多个,最后国王的向导给出他一个匹配.匹配有n个数,代表某个儿子和哪个女孩可以结婚.已知这些条件,要你找出每个儿子可以和 ...

  9. [poj 1904]King's Quest[Tarjan强连通分量]

    题意:(当时没看懂...) N个王子和N个女孩, 每个王子喜欢若干女孩. 给出每个王子喜欢的女孩编号, 再给出一种王子和女孩的完美匹配. 求每个王子分别可以和那些女孩结婚可以满足最终每个王子都能找到一 ...

  10. POJ 1904 King's Quest 强连通分量+二分图增广判定

    http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #inc ...

随机推荐

  1. pcapy-0.10.8 安装

    (1)下载 http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name= ...

  2. HDU 4390 Number Sequence (容斥原理+组合计数)

    HDU 4390 题意: 大概就是这样.不翻译了: Given a number sequence b1,b2-bn. Please count how many number sequences a ...

  3. scrapy研究探索(二)——爬w3school.com.cn

    下午被一个问题困扰了好一阵.终于使用还有一种方式解决. 開始教程二.关于Scrapy安装.介绍等请移步至教程(一)(http://blog.csdn.net/u012150179/article/de ...

  4. Coding上部署Ghost博客

    Ghost构建于Node.js平台之上.支持0.10.*版本号的Node.js. 在你的本地计算机上执行Ghost事实上非常easy,前提是你已经安装了Node.js. 什么是Node.js? 略过 ...

  5. [JZOJ 5875] [NOIP2018提高组模拟9.20] 听我说,海蜗牛 解题报告(BFS+二分)

    题目链接: http://172.16.0.132/senior/#main/show/5875 题目: 题解: 注意这题只能经过开放的港口 我们考虑用vector存下每个点不能到的点,并把并让vec ...

  6. R和中心度、中心势

    最近用R画论文里的弦图,恰好借的书里着重写了中心度等问题. 网上流行一套密歇根大学社交计算的教程.但是前两年看了好几遍总是搞不清,即便是记公式也是收效不大.不妨按照书上总结一下. 绝对法: 无向图点度 ...

  7. python的一些配置

    昨天西邮的学友让我看一段python svm的输入文件格式,但是我打开很久不用的pycharm后发觉python包早已过时.于是搜了一下教程,看来python也得同时补习了 另外,机器学习还需要装很多 ...

  8. Sublimi Text3 下Emmet使用技巧

    Emmet真的好用,可以少写很多代码. 初始化文档 HTML文档需要包含一些固定的标签,比如<html>.<head>.<body>等,现在你只需要1秒钟就可以输入 ...

  9. [原创]Linux 下 redis 链接一次

    刚接触 Linux ,在 Linux 下安装 redis 链接redis 出现了以下问题  Could not connect to Redis at 127.0.0.1:6379: Connecti ...

  10. rescan-scsi-bus.sh linux扫盘 脚本

    [root@ftp:/home/tools/shell] > yum install sg3_utils* Loaded plugins: fastestmirror Repository ba ...