poj1904 二分图匹配+强连通分量
http://poj.org/problem?id=1904
Description
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
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
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
/**
poj1904 二分图匹配+强连通分量
题目大意:有n个王子和n个公主,每一个王子仅仅能和他喜欢的公主结婚,公主能够和随意王子结婚。如今知道,每一个王子的结婚对象(都有结婚对象),和每一个王子所喜欢的公主,问
每一个王子能够和那些公主结婚,而且保证每一个王子都得有结婚对象
解题思路:每一个王子向他喜欢的公主连一条有向边,每一个公主向她的结婚对象连一条有向边,求取每一个王子所在的强连通分量,该王子可和他在同一个强连通分量里的公主结婚。满足
条件。为什么呢?由于每一个王子仅仅能和喜欢的妹子结婚,初始完美匹配中的丈夫和妻子之间有两条方向不同的边能够互达,则同一个强连通分量中的王子数和妹子数一定是相等的,若王子x能够和另外的一个妹子a结婚,妹子a的原配王子y肯定能找到另外一个妹子b结婚,由于假设找不到的话,则x和a必不在同一个强连通分量中。 所以一个王子能够和全部与他同一强连通分量的妹子结婚,而这不会导致同一强连通分量中的其它王子找不到妹子结婚。
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
const int maxn=5005;
int head[maxn],ip; void init()
{
memset(head,-1,sizeof(head));
ip=0;
} struct note
{
int v,next;
} edge[400005]; void addedge(int u,int v)
{
edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
} int n,cnt[maxn];
int dfn[maxn],low[maxn],belong[maxn],index,cnt_tar,cont[maxn],instack[maxn*2];
stack<int>q; void tarjan(int u)
{
dfn[u]=low[u]=++index;
q.push(u);
instack[u]=1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
cnt_tar++;
int j;
do
{
j=q.top();
q.pop();
belong[j]=cnt_tar;
instack[j]=0;
cont[cnt_tar]++;
}
while(j!=u);
}
} void solve()
{
index=0,cnt_tar=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(instack,0,sizeof(instack));
memset(cont,0,sizeof(cont));
for(int i=1; i<=n; i++)
{
if(!dfn[i])
tarjan(i);
}
} int main()
{
while(~scanf("%d",&n))
{
init();
for(int i=1; i<=n; i++)
{
int x,y;
scanf("%d",&x);
for(int j=0; j<x; j++)
{
scanf("%d",&y);
addedge(i,y+n);
}
}
for(int i=1; i<=n; i++)
{
int x;
scanf("%d",&x);
addedge(x+n,i);
}
solve();
for(int u=1; u<=n; u++)
{
int k=0;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(belong[u]==belong[v])
{
cnt[k++]=v-n;
}
}
sort(cnt,cnt+k);
printf("%d",k);
for(int i=0; i<k; i++)
{
printf(" %d",cnt[i]);
}
printf("\n");
}
}
return 0;
}
poj1904 二分图匹配+强连通分量的更多相关文章
- UESTC 898 方老师和缘分 --二分图匹配+强连通分量
这题原来以为是某种匹配问题,后来好像说是强连通的问题. 做法:建图,每个方老师和它想要的缘分之间连一条有向边,然后,在给出的初始匹配中反向建边,即如果第i个方老师现在找到的是缘分u,则建边u-> ...
- Luogu3731 HAOI2017新型城市化(二分图匹配+强连通分量)
将未建立贸易关系看成连一条边,那么这显然是个二分图.最大城市群即最大独立集,也即n-最大匹配.现在要求的就是删哪些边会使最大匹配减少,也即求哪些边一定在最大匹配中. 首先范围有点大,当然是跑个dini ...
- hdu 4685 二分匹配+强连通分量
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 题解: 这一题是poj 1904的加强版,poj 1904王子和公主的人数是一样多的,并且给出 ...
- hdu 4685(匹配+强连通分量)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 思路:想了好久,终于想明白了,懒得写了,直接copy大牛的思路了,写的非常好! 做法是先求一次最 ...
- 【题解】新型城市化 HAOI2017 网络流 二分图最大匹配 强连通分量
Prelude 好,HAOI2017终于会做一道题了! 传送到洛谷:→_→ 传送到LOJ:←_← 本篇博客链接:(●'◡'●) Solution 首先要读懂题. 考场上我是这样想的QAQ. 我们把每个 ...
- H - Prince and Princess - HDU 4685(二分匹配+强连通分量)
题意:有N个王子M个公主,王子喜欢一些公主,而且只能是王子喜欢的人,他们才可以结婚,现在让他们尽可能多的结婚的前提下找出来每个王子都可以和谁结婚. 分析:先求出来他们的最大匹配,因为给的数据未必是完备 ...
- POJ1904 King's Quest(完备匹配可行边:强连通分量)
题目大概就是说给一张二分图以及它的一个完备匹配,现在问X部的各个点可以与Y部那些些点匹配,使得X部其余点都能找到完备匹配. 枚举然后匹配,当然不行,会超时. 这题的解法是,在二分图基础上建一个有向图: ...
- hdu 4685(强连通分量+二分图的完美匹配)
传送门:Problem 4685 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:二分图的最大匹配.完美匹配和匈牙利算法 [ ...
- HDU 4685 Prince and Princess(二分图+强连通分量)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:给出n个王子和m个公主.每个王子有一些自己喜欢的公主可以匹配.设最大匹配为M.那么对于每个 ...
随机推荐
- 关于MessageBox返回值
风格设置MB_OK. 此时无论点击确定还是点击X,都返回IDOK.风格设置MB_OKCANCEL,点击确认返回IDOK,点击取消和X都返回IDCANCEL.风格设置MB_YESNO,点击是返回IDYE ...
- nginx发布web网站
修改/conf/nginx.conf配置文件 server { listen *:; # Listen server_name ""; # Don't worry if " ...
- POI导出,开发中经常会遇到数据导出这样的问题,下面是我在开发中采用的解决方法,大家可以参考,具体的实现害的结合你自身的业务逻辑
@RequestMapping(value = "/drawPayFailExport",method = RequestMethod.GET,produces = "a ...
- 文本三剑客之grep
接受正则表达式,按行匹配,将会过滤出匹配的所有行 格式: grep [OPTION]... PATTERN [FILE]... 可以看出,grep后可以同时接多个文件 选项OPTIO ...
- 【HIHOCODER 1575】 两个机器人(BFS)
描述 一个N × M的2D迷宫中有两个机器人.机器人A在迷宫左上角,只能向右或向下移动:机器人B在迷宫右下角,只能向左或向上移动.机器人不能移动到迷宫外.此外,由于奇怪的同步机制,这两个机器人只能同时 ...
- hdu 6201 transaction (最短路变形——带负权最长路)
题意: 给定n个城市的货物买卖价格, 然后给定n-1条道路,每条路有不同的路费, 求出从某两个城市买卖一次的最大利润. 利润 = 卖价 - (买价 + 路费) 样例数据, 最近是从第一个点买入, 第4 ...
- 2016年工作中遇到的问题41-50:Dubbo注册中心奇葩问题,wifi热点坑了
41.获得JSON中的变量.//显示json串中的某个变量,name是变量名function json(json,name){ var jsonObj = eval(json); return jso ...
- Leetcode 239.滑动窗口最大值
滑动窗口最大值 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值. 示例: ...
- PHP include和require 区别
require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require ...
- MD5散列算法的示例
在很多地方,都用到了数据加密,比较多的就是MD5了,也比较安全,下面就贴上个示例,输入一串字符串,通过MD5加密 加密算法如下 public static string MD5_Encrypt(str ...