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 ...
 
随机推荐
- dnf的动画脚本研究
			
. 1.0x00 : 帧数 int16 2.0x02 : 总为1(?) int16 3.0x04 : 资源文件名长度 int32 4.长度+1 : 0,1(未知用 ...
 - WdatePicker 设置开始时间和结束时间
			
开始时间: <input type="text" placeholder=" -请选择- " readonly="readonly" ...
 - Mybatis 示例之 SelectKey(转)
			
参考:http://blog.csdn.net/isea533/article/details/21153791 SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问 ...
 - checkbox复选框样式
			
随着现代浏览器的流行,纯CSS设置checkbox也变的很是实用,下面会讲到5种与众不同的checkbox复选框. 首先,需要添加一段CSS隐藏所有的Checkbox复选框,下面我们会改变它的外观.要 ...
 - PhoneGap 3.0 官方 安装 方法
			
为使用最新版本PhoneGap ,决定使用官方提供的方法安装一次. 官方提供方法有些地方没有提到,因此这里记录完整的安装过程: 0.下载java sdk 1.6以上版本 1.下载Android Dev ...
 - 写作技巧--Simile明喻
 - LA 4731
			
dp[i][j]意思是前i个分成j组最小的花费 #include<cstdio> #include<algorithm> #include<cstring> #in ...
 - Scala中的Extractor
			
Scala中使用unapply方法可以实现三种extractor(另外使用unapplySeq也可以实现extractor) def unapply(object: S): Option[(T1, . ...
 - Avro RPC 之 Protocol 定义和代码生成
			
摘自http://avro.apache.org/docs/current/spec.html#Protocol+Declaration,1.7.6版 Protocol Declaration Avr ...
 - STM32移植UCGUI3.90笔记
			
在MDK环境下,终于将3.90版本的UCGUI移植到STM32下了,在网上看到的都是例程代码,很少看到有关于在STM32下移植UCGUI的教程方法,为了方便大家,特写此移植方法,大家可以借鉴(有错误之 ...