HDU4865 Prince and Princess 强连通分量+二分图判定
这个题就是建图费点劲,别的和我上一篇博客一样
然后,参考思路请戳这里http://www.cnblogs.com/wally/archive/2013/09/12/3317883.html
补充:这个思路是对的,然后请注意虚拟只和现实的连接,虚拟的不会和虚拟连接
这样可以保证如果在同一连通分量内,还会形成完美匹配,而且原最大匹配也不会有影响
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long LL;
const int N=2e3+;
const int INF=0x3f3f3f3f;
struct Edge{
int v,next;
}edge[N*N];
int head[N],tot,n,m;
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
bool vis[N];
int match[N];
bool dfs(int u){
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(vis[v])continue;
vis[v]=true;
if(match[v]==-||dfs(match[v])){
match[u]=v;
match[v]=u;
return true;
}
}
return false;
}
stack<int>s;
bool instack[N],mp[N/][N/];
int dfn[N],low[N],clk,cnt,bel[N];
void targin(int u){
dfn[u]=low[u]=++clk;
instack[u]=true;s.push(u);
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(!dfn[v]){
targin(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
++cnt;int k;
do{
k=s.top();
s.pop();
instack[k]=false;
bel[k]=cnt;
}while(k!=u);
}
}
vector<int>g;
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--){
printf("Case #%d:\n",++cas);
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
memset(mp,,sizeof(mp));
clk=cnt=tot=;
for(int i=;i<=n;++i){
int k;scanf("%d",&k);
for(int j=;j<k;++j){
int u;scanf("%d",&u);
add(i,u+n);
mp[i][u+n]=true;
}
}
int tol=;
memset(match,-,sizeof(match));
for(int i=;i<=n;++i){
memset(vis,,sizeof(vis));
if(dfs(i))++tol;
}
for(int i=;i<=m-tol;++i){
int u=i+;
for(int j=n+;j<=n+m;++j)
add(u,j);
}
for(int i=;i<=n-tol;++i){
int v=+m-tol+i;
for(int j=;j<=n;++j)
add(j,v);
}
int cur=;
for(int i=n+;i<=n+m;++i){
if(match[i]!=-)add(i,match[i]);
else add(i,++cur);
}
cur=+m-tol;
for(int i=;i<=n;++i){
if(match[i]!=-)continue;
else add(++cur,i);
}
memset(instack,,sizeof(instack));
memset(dfn,,sizeof(dfn));
for(int i=;i<=n;++i)
if(!dfn[i])targin(i);
for(int i=;i<=+m-tol;++i)
if(!dfn[i])targin(i);
for(int i=;i<=n;++i){
g.clear();
for(int j=n+;j<=n+m;++j){
if(!mp[i][j]||bel[i]!=bel[j])continue;
g.push_back(j-n);
}
printf("%d",g.size());
for(int j=;j<g.size();++j)
printf(" %d",g[j]);
printf("\n");
}
}
return ;
}
HDU4865 Prince and Princess 强连通分量+二分图判定的更多相关文章
- hdu 4685(强连通分量+二分图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:n个王子和m个公主,王子只能和他喜欢的公主结婚,公主可以和所有的王子结婚,输出所有王子可能 ...
- hdu 4685(强连通分量+二分图的完美匹配)
传送门:Problem 4685 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:二分图的最大匹配.完美匹配和匈牙利算法 [ ...
- Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定
题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...
- POJ 1904 King's Quest 强连通分量+二分图增广判定
http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #inc ...
- POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)
题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...
- HDU 4685 Prince and Princess(二分图+强连通分量)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:给出n个王子和m个公主.每个王子有一些自己喜欢的公主可以匹配.设最大匹配为M.那么对于每个 ...
- DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)
一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...
- 强连通+二分匹配(hdu4685 Prince and Princess)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
随机推荐
- CSS两列及三列自适应布局方法整理
布局 自适应 两列 三列 在传统方法的基础上加入了Flex布局并阐述各方法的优缺点,希望对大家有所帮助.先上目录: 两列布局:左侧定宽,右侧自适应 方法一:利用float和负外边距 方法二:利用外边距 ...
- 通过注册表查找oracle_home的位置
运行regedit进入注册表 ctrl+f查找oracle_home
- encodeURIComponent()编码和decodeURIComponent()解码
html1: <!DOCTYPE HTML> <meta charset=utf-8> <meta http-equiv="X-UA-Compatible&qu ...
- html5移动web开发实战必读书记
原文 http://itindex.net/detail/50689-html5-移动-web 主题 HTML5 一.配置移动开发环境 1.各种仿真器.模拟器的下载安装 http://www.mob ...
- 宏 #,##,_ _VA_ARGS_ _
宏里面使用: 一.# 转为字符串 #define PSQR(x) printf("the square of" #x "is %d.\n",(x)*(x)) ...
- 我见过的 Objective-C, 讲的最通俗易懂的入门教程....
http://www.cnblogs.com/mjios/category/454764.html ---- 给力...
- QGraphicsEffect介绍(十分漂亮)
原文链接:Qt 图形特效(Graphics Effect)介绍 QGraphicsEffect也是Qt-4.6引入的一个新功能.它让给图形元素QGraphicsItem增加更佳视觉效果的编程变得非常简 ...
- 雷军北大演讲:除了聪明和勤奋我们还需要什么(关键是有了梦想以后,你能不能把这个东西付诸实践)good
雷军北大演讲:除了聪明和勤奋我们还需要什么 昨天我在乌镇参加了全球互联网峰会,在这个会议上有马云,也有苹果公司的高级副总裁,主持人抛出了一个问题,说雷军你说你有一个目标,要用5到10年的时间做智能手机 ...
- update多表陷阱
今天同学发了个sql题目 A1表 B1表 id num id snum 1 10 1 90 2 2000 3 4000 3 30 B表的数据插入A表当中 最后的结果 A表 1 90 2 2000 3 ...
- 208. Implement Trie (Prefix Tree)
题目: Implement a trie with insert, search, and startsWith methods. 链接: http://leetcode.com/problems/i ...