King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
King’s Quest
- Time Limit: 15000MS Memory Limit: 65536K
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.
Source
Northeastern Europe 2003
题意 :国王有n个儿子,同时在他的王国里有n个漂亮的女孩,国王知道他的儿子喜欢那些女孩(不止一个哦),国王要求谋士为他的每一个儿子挑一个他喜欢的女孩,让他的儿子娶这个女孩,每一个女孩只能嫁给一个国王的儿子,当国王看到谋士给他的选择名单后,不是很满意,他需要知道他的儿子可以和哪些女孩结婚,当然只要他和选择女孩结婚,其他的兄弟就能选到他喜欢的其他女孩结婚。
思路 : 可以将儿子与女孩之间的关系看做边,人看做点,建立有向图儿子[1,n],女孩[n+1,2n]。由于名单的原因,所以每一个儿子在可以结婚的女孩中必有名单上的女孩,所以可以在名单上的女孩和对应的儿子之间建立一条边,使得儿子和女孩处于同一个强连通分量中,建立图以后可以发现强连通分量中的元素的女孩是都可以娶的,再找出他喜欢的就是对应儿子可以娶的。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const int MaxM = 210000;
const int MaxN = 4100;
vector<int>Map[MaxN];
int dfn[MaxN],low[MaxN],vis[MaxN],dep;
int pre[MaxN],num;
vector<int>Gnum[MaxN];
stack <int> S;
int n;
void Init()
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=2*n;i++)
{
Gnum[i].clear();
Map[i].clear();
}
dep = 0; num = 0 ;
}
void Tarjan(int u) //Tarjan缩点
{
dfn[u] = low[u] = dep++;
vis[u] = 1;
S.push(u);
for(int i = 0 ;i<Map[u].size();i++)
{
int v = Map[u][i];
if(vis[v]==1)
{
low[u] = min(low[u],dfn[v]);
}
else if(vis[v]==0)
{
Tarjan(v);
low[u] = min(low[u],low[v]);
}
}
if(dfn[u] == low[u])
{
while(!S.empty())
{
int v = S.top();
S.pop();
pre[v] = num;
vis[v] = 2;
if(v == u)
{
break;
}
}
num++;
}
}
int Scan() //输入外挂
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
void Out(int a) //输出外挂
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
int main()
{
while(~scanf("%d",&n))
{
Init();
int v,Ki;
for(int i=1;i<=n;i++)
{
Ki = Scan();
for(int j=1;j<=Ki;j++)
{
v = Scan();
Map[i].push_back(v+n);
}
}
for(int i=1;i<=n;i++)
{
v = Scan();
Map[v+n].push_back(i);
}
for(int i=1;i<=n;i++) //使每一个儿子喜欢的女孩编号有序
{
sort(Map[i].begin(),Map[i].end());
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
Tarjan(i);
}
}
for(int i=1;i<=n;i++)
{
int ans = 0;
for(int j = 0;j<Map[i].size();j++)
{
if(pre[i]==pre[Map[i][j]])//处于同一个强连通分量为可娶的。
{
ans++;
}
}
Out(ans);
for(int j=0;j<Map[i].size();j++)
{
if(pre[i]==pre[Map[i][j]])
{
putchar(' ');
Out(Map[i][j]-n);
}
}
puts("");
}
// puts(""); ZOJ多一个换行
}
return 0;
}
King's Quest —— POJ1904(ZOJ2470)Tarjan缩点的更多相关文章
- UVA1327 && POJ1904 King's Quest(tarjan+巧妙建图+强连通分量+缩点)
UVA1327 King's Quest POJ1904 King's Quest 题意: 有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚.现有一个匹配表,将每个王子都与一个自己 ...
- 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 ...
- POJ1904:King's Quest(强连通+思维)
King's Quest Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 10352 Accepted: 3815 题目 ...
- POJ1904 King's Quest
King's Quest Language:Default King's Quest Time Limit: 15000MS Memory Limit: 65536K Total Submission ...
- poj 1904 King's Quest
King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...
- POJ 1904 King's Quest(SCC的巧妙应用,思维题!!!,经典题)
King's Quest Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 10305 Accepted: 3798 Ca ...
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- 【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率
2438: [中山市选2011]杀人游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1638 Solved: 433[Submit][Statu ...
随机推荐
- 配置opencv时计算机显示丢失opencv_world300d.dll如何解决
在自己安装路径里找到opencv_world300d.dll文件: 然后把opencv_world300d.dll文件复制到C://Windows/System32里:
- Scala的模式匹配
1.概述 2.程序示例(普通的示例) 3.模式匹配(Array) 4.程序示例(Array) 5.模式匹配(List) 6.程序示例 7.遍历 8.模式匹配(case class) 9.程序示例(传统 ...
- HAProxy
1. HAProxy是支持虚拟主机的,可以工作在4. 7层(支持多网段):2. 能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作:3. 支持url检测后端的服务器:4. ...
- php识别中文编码并自动转换为UTF-8
原文地址:http://www.codefans.net/articles/1272.shtml php自动识别编码,若里面有中文的话,将其转换为UTF-8就最好了,因为中文在Gbk编辑情况情况下,有 ...
- Chrome 控制台使用大全
Chrome的开发者工具已经强大到没朋友的地步了,特别是功能丰富界面友好的console 一.console.log 将输出到控制台的信息进行分类会更好: console.log(); 普通信息 ...
- PS通过滤色实现简单的图片拼合
素材如下: 素材一: 雪山 素材二: 月亮 效果: 实现步骤 1.在PS中打开雪山素材一 2.将月亮素材直接拖入雪山所在的图层中 3.锁定置入素材的高宽比(点击一下链状按钮) 4.调整月亮到合适大 ...
- 实时控制软件设计第一周作业-汽车ABS软件系统案例分析
汽车ABS软件系统案例分析 ABS 通过控制作用于车轮制动分泵上的制动管路压力,使汽车在紧急刹车时车轮不会抱死,这样就能使汽车在紧急制动时仍能保持较好的方向稳定性. ABS系统一般是在普通制动系统基础 ...
- python之面向对象编程
1.面向对象介绍: 世界万物,皆可分类 世界万物,皆为对象 只要是对象,就肯定属于某种类 只要是对象,就肯定有属性 2. 面向对象的几个特性: class类: 一个类即对一类拥有相同属性的对象的抽象, ...
- Linux上 .vimrc文件
在Linux上面对VIM编辑器的格式的设置通常可以提升工作效率,下面对工作机器上的.vimrc文件的内容进行一总结,以备后续的查询 set smarttab set tabstop=4 set shi ...
- Android touch 事件传递机制
前言: (1)在自定义view的时候经常会遇到事件拦截处理,比如在侧滑菜单的时候,我们希望在侧滑菜单里面有listview控件,但是我们希望既能左右滑动又能上下滑动,这个时候就需要对触摸的touch事 ...