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 个蚁群 ...
随机推荐
- Oracle把表记录恢复到指定时间节点
可以执行以下命令alter table 表名 enable row movement; --开启表行移动flashback table 表名 to timestamp to_timestamp('20 ...
- js 之 object
js之object 对象 ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 尽管 ECMAScrip ...
- OC的消息机制简单介绍
在OC的消息机制中主要分为三个阶段,分别为: 1.消息发送阶段:从类以及父类的方法缓存列表和方法列表查找方法. 2.动态解析阶段:在消息发送阶段没有找到方法,则会进入这个阶段,负责动态添加方法实现. ...
- Netty(五):ServerBootstrap启动流程
这篇文章主要是对ServerBootstrap启动流程做一个梳理,方便我们串联起各个类,同时也对主要的一些类有个大概的印象,方便之后逐个类的深入学习. 本篇文章不在具体贴出代码,而是对整个启动流程画了 ...
- POJ 1905 Expanding Rods(二分)
Expanding Rods Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20224 Accepted: 5412 Descr ...
- 2019-2020 ICPC, Asia Jakarta Regional Contest C. Even Path(思维)
Pathfinding is a task of finding a route between two points. It often appears in many problems. For ...
- Mahout聚类和kafaka相关知识
1.说几种距离测度Mahout: 欧式距离测度:平方欧式距离测度:曼哈顿距离测度:余弦距离测度:加权距离测度 2.K-means算法参数:
- Java return 关键字
一.基本概念 return一方面用在循环语句中来结束循环,另一方面用来终止函数的执行或者退出类的方法,并把控制权返回该方法的调用者.如果方法有返回类型,则return的返回该类型的值:如果没有返回值, ...
- leetcode_二叉树验证(BFS、哈希集合)
题目描述: 二叉树上有 n 个节点,按从 0 到 n - 1 编号,其中节点 i 的两个子节点分别是 leftChild[i] 和 rightChild[i]. 只有 所有 节点能够形成且 只 形成 ...
- css布局的漂浮、position定位
float: left:文本流向对象的右边 right:文本流向对象的左边 div具有自动换行效果 position: absolute:将对象从文档流中拖出,即飘到最上面一层,形成层叠,不占位置:可 ...