poj1904 完美匹配+Tarjan
| Time Limit: 15000MS | Memory Limit: 65536K | |
| Total Submissions: 9460 | Accepted: 3497 | |
| Case Time Limit: 2000MS | ||
Description
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 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
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
题解这里
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=4e6+;
vector<int>sc[N];
vector<int>G[N];
int head[N],dfn[N],low[N],q[N];
bool instack[N],mp[][];
int tot,scnt,l,cnt;
struct node
{
int to,next;
} e[M];
void add(int u,int v)
{
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++cnt;
q[l++]=u;
instack[u]=;
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].to;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]&&dfn[v]<low[u]) low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
++scnt;
int t;
do
{
t=q[--l];
sc[scnt].push_back(t);
instack[t]=;
}
while(t!=u);
}
}
int main()
{
int n,x,y;
while(scanf("%d",&n)!=EOF)
{
tot=scnt=cnt=l=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(mp,,sizeof(mp));
for(int i=; i<=n; ++i)
{
scanf("%d",&x);
while(x--)
{
scanf("%d",&y);
add(i,y+n);
G[i].clear();
sc[i].clear();
mp[i][y]=;
}
}
for(int i=; i<=n; ++i)
{
scanf("%d",&x);
add(x+n,i);
}
for(int i=; i<=n; ++i) if(!dfn[i]) Tarjan(i);
for(int i=; i<=scnt; ++i) sort(sc[i].begin(),sc[i].end());
for(int i=; i<=scnt; ++i)
{
int tc=upper_bound(sc[i].begin(),sc[i].end(),n)-sc[i].begin();
for(int j=; j<tc; ++j) for(int k=tc; k<(int)sc[i].size(); ++k) if(mp[sc[i][j]][sc[i][k]-n] ) G[sc[i][j]].push_back(sc[i][k]-n);
}
for(int i=; i<=n; ++i)
{
printf("%d",(int)G[i].size());
for(int j=; j<(int)G[i].size(); ++j) printf(" %d",G[i][j]);
puts("");
}
}
}
poj1904 完美匹配+Tarjan的更多相关文章
- poj1904 二分图匹配+强连通分量
		http://poj.org/problem?id=1904 Description Once upon a time there lived a king and he had N sons. An ... 
- POJ 1904 King's Quest ★(强连通分量:可行完美匹配边)
		题意 有n个女生和n个男生,给定一些关系表示男生喜欢女生(即两个人可以结婚),再给定一个初始匹配,表示这个男生和哪个女生结婚,初始匹配必定是合法的.求每个男生可以和哪几个女生可以结婚且能与所有人不发生 ... 
- POJ 1904 King's Quest (强连通分量+完美匹配)
		<题目链接> 题目大意: 有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王 ... 
- poj 1904(强连通分量+完美匹配)
		传送门:Problem 1904 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:http://www.cnblogs.co ... 
- ZOJ-3933 Team Formation (二分图最佳完美匹配)
		题目大意:n个人,分为两个阵营.现在要组成由若干支队伍,每支队伍由两个人组成并且这两个人必须来自不同的阵营.同时,每个人都有m个厌恶的对象,并且厌恶是相互的.相互厌恶的人不能组成一支队伍.问最多能组成 ... 
- UVA 11383 Golden Tiger Claw(最佳二分图完美匹配)
		题意:在一个N*N的方格中,各有一个整数w(i,j),现在要求给每行构造row(i),给每列构造col(j),使得任意w(i,j)<=row(i)+col(j),输出row(i)与col(j)之 ... 
- UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
		题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi ... 
- UVALive 2238 Fixed Partition Memory Management(二分完美匹配)
		题意:计算机中有一些固定大小的内存,内存越大,处理速度越快.对于一个程序,加入不同的内存空间,处理所需时间不同.现给出m个内存空间,n个程序,对于每个程序程序,有k组数据(s,t),分别表示当程序 i ... 
- UVALive 4043 Ants(二分图完美匹配)
		题意:每个蚁群有自己的食物源(苹果树),已知蚂蚁靠气味辨别行进方向,所以蚁群之间的行动轨迹不能重叠.现在给出坐标系中n个蚁群和n棵果树的坐标,两两配对,实现以上要求.输出的第 i 行表示第 i 个蚁群 ... 
随机推荐
- BeanDefinition源码解析
			我们知道BeanDefintion定义了Bean在IoC容器内的基本数据结构.在学习IoC之前先了解BeanDefition对我们理解IoC容器是有帮助的. 首先BeanDefinition是一个接口 ... 
- MongoDB学习(四):通过Java使用MongoDB
			环境配置 在Java项目中使用MongoDB,需要在项目中引入mongo.jar这个包.下载地址:下载 请尽量下载较新的版本,本文用的是2.10.1. 连接MongoDB public synchro ... 
- flask学习笔记(二)
			一.视图函数的传参方式 修改前: 目标: 传参方式改成 途径: 通过request获取参数 注意:args并不是地点类型,而是dict的一个子类,如图: immutable意思是不可变 不可变的字典转 ... 
- 关于LinearLayout设置权重后width或height不设置0dp的影响说明
			摘要 平时没那么注意LinearLayout布局时权重的问题,设置了权重属性后,通常建议将width或height的属性值设置为0dp,有时候设置权重后,还是习惯将width或height的属性设置为 ... 
- [开发笔记]-unix时间戳、GMT时间与datetime类型时间之前的转换
			前段时间项目中涉及到了MySql和MsSql数据类型之间的转换,最近又在研究新浪微博的API,涉及到了带有时区的GMT时间类型的转换,所以,特记录于此,以备日后查询. 一:UNIX时间戳与dateti ... 
- INTERVIEW #3
			菊厂的面试本来没打算记录,因为当时投的是非技术岗(技术支持).为了全面,就寥做记录. 菊厂的面试因为有口头保密协议,所以不能透露具体题目. 0 群面 简历通过筛选后,会有短信通知去面试. 非技术岗第一 ... 
- 你这些知识点都不会,你学个锤子SQL数据库!
			全套的数据库的知识都在这里,持续更新中ing 快戳我查看,快戳戳,不管是Oracle还是mysql还是sqlsever,SQL语言都是基础. 一.关系 单一的数据结构----关系 现实世界的实体以及实 ... 
- js数组中返回具有某个属性具有特定值的对象
			const drDataArr = [{ date: 0, type: '心率', value: 82 },{ date: 1, type: '心率', value: 80 },{ date: 2, ... 
- wmic 内网使用
			先决条件: 1.远程服务器启动Windows Management Instrumentation服务,开放TCP135端口,防火墙放开对此端口的流量(默认放开): 2.远程服务器的本地安全策略的“网 ... 
- 用js写出一个漂亮的单选框选中效果
			一般的input框比较简单,我们可以用JavaScript配合css背景图片定位让我们模拟写出一个点击选中效果 首先需要有个图片素材,当页面加载的时候是背景图片定位到左图,当我们点击图片的时候,背景图 ... 
