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. 华中农业大学第五届程序设计大赛网络同步赛-A

    Problem A: Little Red Riding Hood Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 860  Solved: 133[S ...

  2. linux下lamp环境修改网站根目录

    Apache默认的网站目录是在/var/www/html,我们现在要把网站目录更改成 /var/www 目录下,操作如下: 1.修改httpd,conf文件 vi /etc/httpd/conf/ht ...

  3. innerHTML在ie9有部分无法添加

    在高版本的浏览器,innerHTML就如正常时候,里面可以套任何字符串,但是在ie9下,innerHTML不能是table ,tr td等标签字符串,解决方法如下: 在table添加一个tr var ...

  4. OSGI企业应用开发(八)整合Spring和Mybatis框架(一)

    到目前为止,我们已经学习了如何使用Blueprint將Spring框架整合到OSGI应用中,并学习了Blueprint&Gemini Blueprint的一些使用细节.本篇文章开始,我们將My ...

  5. SD从零开始03-04

    [原创]SD从零开始3 SD中的主数据 客户主数据Customer master(分层维护) 一般数据general data: 与销售和财务都有关,对所有的组织单元有效: 销售区域数据sales a ...

  6. Reactjs-generator-cli 一款基于Ink构建用于快速搭建React应用的CLI scaffolding工具

    Reactjs-generator-cli 一款基于Ink构建用于快速搭建React应用的CLI scaffolding工具 A simple CLI for scaffolding React.js ...

  7. Python+Selenium笔记(十三):Page Object设计模式

    (一) 前言 简单的说就是分为2层,页面class 和测试class. 页面class:分为父类和子类(子类指具体的页面,每一个页面都创建一个类),父类中定义公有的属性和方法(操作). #对面向对象有 ...

  8. 学习vue.js的正确姿势(转载)

    最近饶有兴致的又把最新版 Vue.js 的源码学习了一下,觉得真心不错,个人觉得 Vue.js 的代码非常之优雅而且精辟,作者本身可能无 (bu) 意 (xie) 提及这些.那么,就让我来吧:) 程序 ...

  9. .NET(C#)使用Serialize、Deserialize序列和反序列化XML文档

    本文给大家分享一下C#操作(读取.写入)XML文档的实用方法,即用.NET本身提供的Deserialize和Serialize进行反序列化和序列化XML文档.这种方法主要是对比较规范的XML文档进行操 ...

  10. 【转】HTTP学习---Web 缓存

    [原文]https://www.toutiao.com/i6592743068623962632/ 1. 前端缓存概述 前端缓存主要是分为HTTP缓存和浏览器缓存.其中HTTP缓存是在HTTP请求传输 ...