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. [Tools] Create your own mobile emulator device by using Chrome dev tool

    Using the New Device Emulation Interface The Device Emulation interface changed a bit with the newer ...

  2. c# 获取文件夹下面所有文件夹列表

    方法一: string dirPath = @"D:\App1"; List<string> dirs = new List<string>(Directo ...

  3. angular4过滤器

    Angular4中过滤器 一.大小写转换过滤器 uppercase将字符串转换为大写 lowercase将字符串转换为小写 <p>将字符串转换为大写{{str | uppercase}}& ...

  4. NOIP 2012 T2 国王游戏 (贪心+高精)

    思路: 呃呃网上那么多题解写得都不错-.. 就是高精 巨坑... 这里展出的是任氏高精(纯自己yy滴) //By SiriusRen #include <cstdio> #include ...

  5. POJ 2184 DP

    思路: f[j]表示当ts的和为j的时候tf的最大值. 这时候要分情况讨论: (我把状态平移了101000) 若ts[i]>=0倒序循环 否则正序 (防止ts被用了多次) f[101000]=0 ...

  6. 在Jquery里格式化Date日期时间数据

    在Jquery里格式化Date日期时间数据: $(function(){ //当前时间格式化yyyy-MM-dd HH:mm:ss alert(timeStamp2String(new Date(). ...

  7. Windows下安装和使用MongoDB

    支持平台:从2.2版本开始,MongoDB不再支持Windows XP.要使用新版本的MongoDB,请用更新版本的Windows系统. 重要:如果你正在使用Windows Server 2008 R ...

  8. jQuery学习(二)——使用JQ完成页面定时弹出广告

    1.JQuery效果 2.步骤分析: 第一步:引入jQuery相关的文件 第二步:书写页面加载函数 第三步:在页面加载函数中,获取显示广告图片的元素. 第四步:设置定时操作(显示广告图片的函数) 第五 ...

  9. AbstractQueuedSynchronizer中CAS的疑惑

    这段代码是AQS框架中将当前节点入队的操作. Node pred = tail; if (pred != null) { node.prev = pred; if (compareAndSetTail ...

  10. atom安装插件失败 latex

    用atom写latex 链接 http://www.cnblogs.com/schaepher/p/5934184.html 但在gui下安装插件失败 按照以下步骤解决了 安装gitbash cd . ...