King's Quest
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 10305   Accepted: 3798
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

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个儿子,每个儿子喜欢ki个女孩,国王想让王子和他喜欢的人结婚,
就让巫师做一个列表,就是国王想知道每个王子可以和哪些女生结合且不影响其他王子也能和自己喜欢的人结婚
其实就是求王子和哪些女生结婚不影响最大匹配!!!
做法:
对每个王子喜欢的女生,王子到女孩建边
对完备匹配,女孩到王子建边
然后和王子x属于同一个SCC的且王子喜欢的女生就是王子x在不影响最大匹配的情况下,可以结婚的女生
1.为什么对完备匹配反向建边?
保证完备匹配的女生y和王子x是属于同一个SCC
2.为什么该图是二分图?
题目说n个王子,n个女生,保证每个王子都可以匹配到喜欢的女生
所以该图是二分图
且同一个SCC中王子和女生的数目是相同的!!
3.为什么王子不能选择其他SCC的女生?
反证法:如果强连通分量1中的王子选择了强连通分量2中的妹子,
那么势必强连通分量2中的一个王子无法在自己的强连通分量中找到妹子,
那么他就会去别的强连通分量找妹子,这样一直循环下去,
我们知道最终一定是经过了强连通分量1,2,x1,x2,xn,……,1,
王子们才能都找到自己的妹子,这样这些强连通分量1,2,x1,x2,xn,……,1会构成一个强连通分量,
与题设在不同强连通分量中找妹子不符
和王子的同一个SCC里面的女生,该王子都可以选择与其结婚(前提是该王子喜欢)
坑点:
1.对在同一个SCC里面的女生y,王子x不一定喜欢
2.输出的编号要升序排序
 
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
}
void out(int a)
{
if(a>)
out(a/);
putchar(a%+'');
} #define max_v 20005
int vis[max_v];
int dfn[max_v];
int low[max_v];
int color[max_v];
int stk[max_v];
vector<int> G[max_v];
int n,m;
int sig,cnt,sp; priority_queue<int,vector<int>,greater<int> > q;//优先队列 升序
void init()
{
me(vis,);
me(dfn,);
me(color,);
me(low,);
for(int i=;i<=n*;i++)
{
G[i].clear();
}
sig=;
cnt=;
sp=-;
} void tarjan(int u)
{
vis[u]=;
dfn[u]=low[u]=cnt++;
stk[++sp]=u;
for(int j=;j<G[u].size();j++)
{
int v=G[u][j];
if(vis[v]==)
tarjan(v);
if(vis[v]==)
low[u]=min(low[v],low[u]);
}
if(low[u]==dfn[u])
{
sig++;
do
{
color[stk[sp]]=sig;
vis[stk[sp]]=-;
}while(stk[sp--]!=u);
}
}
int main()
{
int x,y;
while(~scanf("%d",&n))
{
init();
for(int i=;i<=n;i++)
{
m=getval();//输入外挂
for(int j=;j<=m;j++)
{
y=getval();
y+=n;
if(count(G[i].begin(),G[i].end(),y)==)
G[i].push_back(y);
}
}
for(int i=;i<=n;i++)
{
x=getval();
x+=n;
if(count(G[x].begin(),G[x].end(),i)==)
G[x].push_back(i);
}
for(int i=;i<=n+n;i++)
{
if(vis[i]==)
tarjan(i);
}
int sum=;
for(int i=;i<=n;i++)
{
sum=;
for(int j=;j<G[i].size();j++)//只考虑王子i喜欢的女生
{
if(color[i]==color[G[i][j]])//颜色相同代表属于同一个SCC
{
sum++;
q.push(G[i][j]-n);
}
}
out(sum);
while(!q.empty())
{
printf(" ");
out(q.top());//输出外挂
q.pop();
}
printf("\n");
}
}
return ;
} /*
题目意思:
国王有n个儿子,每个儿子喜欢ki个女孩,国王想让王子和他喜欢的人结婚,
就让巫师做一个列表,就是国王想知道每个王子可以和哪些女生结合且不影响其他王子也能和自己喜欢的人结婚
其实就是求王子和哪些女生结婚不影响最大匹配!!! 做法:
对每个王子喜欢的女生,王子到女孩建边
对完备匹配,女孩到王子建边
然后和王子x属于同一个SCC的且王子喜欢的女生就是王子x在不影响最大匹配的情况下,可以结婚的女生 1.为什么对完备匹配反向建边?
保证完备匹配的女生y和王子x是属于同一个SCC 2.为什么该图是二分图?
题目说n个王子,n个女生,保证每个王子都可以匹配到喜欢的女生
所以该图是二分图
且同一个SCC中王子和女生的数目是相同的!! 3.为什么王子不能选择其他SCC的女生?
反证法:如果强连通分量1中的王子选择了强连通分量2中的妹子,
那么势必强连通分量2中的一个王子无法在自己的强连通分量中找到妹子,
那么他就会去别的强连通分量找妹子,这样一直循环下去,
我们知道最终一定是经过了强连通分量1,2,x1,x2,xn,……,1,
王子们才能都找到自己的妹子,这样这些强连通分量1,2,x1,x2,xn,……,1会构成一个强连通分量,
与题设在不同强连通分量中找妹子不符 和王子的同一个SCC里面的女生,该王子都可以选择与其结婚(前提是该王子喜欢) 坑点:
1.对在同一个SCC里面的女生y,王子x不一定喜欢
2.输出的编号要升序排序 */

POJ 1904 King's Quest(SCC的巧妙应用,思维题!!!,经典题)的更多相关文章

  1. 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 ...

  2. poj 1904 King's Quest

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

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

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

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

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

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

    题意:国王有n个儿子,现在这n个儿子要在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 强连通分量+二分图增广判定

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

  9. poj 1904 King's Quest tarjan求二分图的所有可选最大匹配边

    因为是完美匹配,所以每个点都已经匹配了,那么如果要选择一条别的边,增光路的最后必定找到原来所匹配的点,加上匹配的边,那么就是一个环.所以可选边在一个强连通分量里. #include <iostr ...

随机推荐

  1. POJ2411(SummerTrainingDay02-I 状态压缩dp)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17187   Accepted: 991 ...

  2. Javascript 随机数函数 学习之二:产生服从正态分布随机数

    一.为什么需要服从正态分布的随机函数 一般我们经常使用的随机数函数 Math.random() 产生的是服从均匀分布的随机数,能够模拟等概率出现的情况,例如 扔一个骰子,1到6点的概率应该相等,但现实 ...

  3. 前端hash路由基本原理,及代码的基本实现

    路由就是指随着浏览器地址栏的变化,展示给用户的页面也不相同. 早期的路由都是后端实现的,直接根据 url 来 reload 页面,页面变得越来越复杂服务器端压力变大,随着 ajax 的出现,页面实现非 ...

  4. 单元测试(四)-隔离框架NSubstitute

    之前学习了单元测试的基础知识,以及桩对象和模拟对象的不同作用.但在实际应用中,往往不会直接手写桩对象或者模拟对象,而是使用隔离框架动态的创建这些对象,这可以让测试变得更简便.快捷,还可以更好地应对复杂 ...

  5. HTIML5 真的打败了Flash?新测试结果出人意料

    [编者按]本文最早发布于 2010 年,通过 Flash 与 HTML5 在 Mac 及 Windows 平台不同浏览器中的测试表现,比较两者的性能并分析背后的原因.虽然是一篇老文,但其客观冷静的分析 ...

  6. Percona Xtradb Cluster的设计与实现

    Percona Xtradb Cluster的设计与实现   Percona Xtradb Cluster的实现是在原mysql代码上通过Galera包将不同的mysql实例连接起来,实现了multi ...

  7. shell脚本常用技巧

    shell脚本常用技巧 1.获取随机字符串或数字 ~]#echo $RANDOM | md5sum | cut -c 1-6 ~]#openssl rand -base64 4 | cut -c 1- ...

  8. 《SQL Server 2008从入门到精通》--20180723

    目录 1.架构 1.1.创建架构并在架构中创建表 1.2.删除架构 1.3.修改表的架构 2.视图 2.1.新建视图 2.2.使用视图修改数据 2.3.删除视图 3.索引 3.1.聚集索引 3.2.非 ...

  9. MySQL面试之说明myisam和innodb两种存储引擎的不同之处

    1.事务的支持不同(innodb支持事务,myisam不支持事务) 2.锁粒度(innodb行锁应用,myisam表锁) 3.存储空间(innodb既缓存索引文件又缓存数据文件,myisam只能缓存索 ...

  10. 【第一次玩Travis CI】终于弄好了我的马鸭

    真是不容易,我都要哭了.熬了半天终于弄完了!! 终于可以坐这儿挺会小曲,写写感受了. 作为一个程序写的不咋滴的程序员,倒是特别喜欢写博客,也是绝了. 高三的时候,用OneNote,后来转到Lofter ...