这个题就是建图费点劲,别的和我上一篇博客一样

然后,参考思路请戳这里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 强连通分量+二分图判定的更多相关文章

  1. hdu 4685(强连通分量+二分图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:n个王子和m个公主,王子只能和他喜欢的公主结婚,公主可以和所有的王子结婚,输出所有王子可能 ...

  2. hdu 4685(强连通分量+二分图的完美匹配)

    传送门:Problem 4685 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:二分图的最大匹配.完美匹配和匈牙利算法 [ ...

  3. Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定

    题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...

  4. POJ 1904 King's Quest 强连通分量+二分图增广判定

    http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #inc ...

  5. POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)

    题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...

  6. HDU 4685 Prince and Princess(二分图+强连通分量)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:给出n个王子和m个公主.每个王子有一些自己喜欢的公主可以匹配.设最大匹配为M.那么对于每个 ...

  7. DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)

    一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...

  8. 强连通+二分匹配(hdu4685 Prince and Princess)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  9. 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 ...

随机推荐

  1. 子查询注意这几点, 就可以写出好的sql语句

    执行sql时子查询的语句一般优先执行 理论上多表查询效率是高于子查询(根据子查询不值一个查询语句可能会有多个from但是多表查询只产生一个from), 但是在oracle中子查询效率一般会高于多表查询

  2. Critical Rendering Path

    1.生成 dom & cssom https://developers.google.com/web/fundamentals/performance/critical-rendering-p ...

  3. myeclipse2013 for linux及其破解补丁百度网盘下载

    FQ下载1.1G的东西不是开玩笑的,用GA下载了两回均失败,还是用了某某门在win下下载好的,来之不易,所以特意上传分享给大家,免得FQ.破解文件也一并附上: 注意:本人这个是在原文件基础上bzip2 ...

  4. hadoop 错误处理机制

    hadoop 错误处理机制 1.硬件故障 硬件故障是指jobtracker故障或TaskTracker 故障 jobtracker是单点,若发生故障,目前hadoop 还无法处理,唯有选择最牢靠的硬件 ...

  5. [转载]C#导入XLS数据到数据库

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  6. Why are very few schools involved in deep learning research? Why are they still hooked on to Bayesian methods?

    Why are very few schools involved in deep learning research? Why are they still hooked on to Bayesia ...

  7. spring中注解事务认识

    1.配置事务管理器 <!-- 设定transactionManager事务管理器 --> <bean id="txManager" class="org ...

  8. UR #13 Ernd

    考试的时候没有注意到可以将(a,b)放在二维平面上之后旋转坐标系,使得转移变成树状数组二维偏序 这样就算我想出来了第二个转移的斜率优化也没有什么卵用啊(摔西瓜 设g(i)表示当前站在第i个水果下面且第 ...

  9. codeforces #309 div1 B

    题目啰里啰嗦说了一大堆(耐心读完题目就可以秒题了) 首先我们考虑当前置换的开头的循环节的开头 1.如果是1 1->1形成循环节 问题变成i-1的子问题 2.如果是2 1->2->1形 ...

  10. redis的key过期时间

    public void set(String key,String value,int liveTime){ this.set(key, value); this.getJedis().expire( ...