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. vue 相对路径的图片 不显示问题

    例如 data () { return { img: '../../images/jifen/index/img_list_default_pic.jpg' //路径也没问题啊,怎么不显示呢,难道他瞎 ...

  2. python-命令模式

    源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 命令在发送方被激活,而在接收方被响应.一个对象既可以作为命令的发送方,也可以作为 ...

  3. nodejs 新建项目

    第一步: 新建工程-->选择nodejs-->creat 注意: 如果出错就使用第二步!! 第二步:建立express 模板的nodejs 点击下图的命令窗口,依次输入下面命令 命令: & ...

  4. 记录Ubuntu 16.04 安装Docker CE

    一.Docker的两个版本 Docker有两个版本: 社区版(CE) 企业版(EE) Docker Community Edition(CE)非常适合希望开始使用Docker并尝试使用基于容器的应用程 ...

  5. 从零开始学习html(九)CSS的继承、层叠和特殊性

    一.继承 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" co ...

  6. Azure 实例元数据服务

    Azure 实例元数据服务提供有关可用于管理和配置虚拟机的正在运行的虚拟机实例的信息. 这包括 SKU.网络配置和即将发生的维护事件等信息. 若要详细了解可用信息类型,请参阅元数据类别. Azure ...

  7. Oracle EBS INV 创建物料搬运单

    Create or Replace PROCEDURE ProcessMoveOrder AS -- Common Declarations l_api_version NUMBER := 1.0; ...

  8. python自学——文件打开

    #文件的打开 新建一个文件new file.txt #方法一:f=open("yesterday","r",encoding="utf-8" ...

  9. Linux内核同步机制之completion【转】

    Linux内核同步机制之completion 内核编程中常见的一种模式是,在当前线程之外初始化某个活动,然后等待该活动的结束.这个活动可能是,创建一个新的内核线程或者新的用户空间进程.对一个已有进程的 ...

  10. orcl 如何快速删除表中百万或千万数据

    orcl 数据库表中数据达到上千万时,已经变的特别慢了,所以时不时需要清掉一部分数据. bqh8表中目前有10000000条数据,需要保留19条数据,其余全部清除掉. 以下为个人方法: 1.首先把需要 ...