意甲冠军:今天,有n王子,m公主。现在给他们配对,与王子会嫁给一个男人,他喜欢。公主无法做出选择。

这标题去咬硬,还有一类似的题目poj1904。那个题目也是给王子与公主配对,但那个是王子公主各n个,且给定了一个完美匹配,然后求每一个王子能够做出的选择且不影响最大匹配数目。那题是先建各条喜欢关系的边。然后在由被选择的公主连一条边到与之配对的王子。强连通之后假设一个王子和一个公主在一个强连通分量中,那么他们结合的话,他们的还有一半也各自能在强连通中找到另外的匹配,就是符合题意的结果了。

这个题目算是升级版把。我们须要做的先是用匈牙利算法求出最大匹配res,然后建立m-res个虚拟王子与m-res单身公主准备匹配,建立n-res个虚拟公主与n-res个单身王子准备匹配。过程就是虚拟王子要喜欢每个公主,相同虚拟公主也要被每个王子喜欢,这样最大匹配一定是n+m-res.求出一个这种完美匹配,然后再套用poj1904的思路用强连通做。建议先做下1904额。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std; const int eps=0.00000001;
const int maxn=2005;
const int maxm=maxn*maxn/2; int first[maxn],link[maxn];
int nex[maxm],w[maxm],v[maxm],u[maxm];
bool done[maxn],g[maxn][maxn];
int n,m,ecnt; void add_(int a,int b,int c=0)
{
u[ecnt]=a;
v[ecnt]=b;
w[ecnt]=c;
nex[ecnt]=first[a];
first[a]=ecnt++;
}
bool dfs(int s)
{
for(int e=first[s];~e;e=nex[e])
if(!done[v[e]])
{
done[v[e]]=true;
if(link[v[e]]==-1||dfs(link[v[e]]))
{
link[v[e]]=s;
return true;
}
}
return 0;
} int hungary(int n)
{
int ans=0;
clr(link,-1);
for(int i=1;i<=n;i++)
{
clr(done,false);
if(dfs(i))ans++;
}
return ans;
}
int low[maxn],dfn[maxn],stck[maxn],belong[maxn];
int index,top,scc;
bool ins[maxn];
int num[maxn];
int in[maxn],out[maxn]; void tarjan(int u)
{
low[u]=dfn[u]=++index;
stck[top++]=u;
ins[u]=1;
for(int e=first[u];~e;e=nex[e])
{
if(!dfn[v[e]])
{
tarjan(v[e]);
low[u]=min(low[u],low[v[e]]);
}
else if(ins[v[e]])low[u]=min(low[u],dfn[v[e]]);
}
if(low[u]==dfn[u])
{
int v;
scc++;
do
{
v=stck[--top];
ins[v]=false;
belong[v]=scc;
num[scc]++;
}while(v!=u);
}
}
void solve(int n)
{
clr(dfn,0);
clr(ins,0);
clr(num,0);
index=scc=top=0;
for(int i=1;i<=n;i++)
if(!dfn[i])tarjan(i);
} int main()
{
int t,a,b,c,k,cas=1,key=1000;
scanf("%d",&t);
while(t--)
{
clr(first,-1);ecnt=0;
clr(g,false);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&a);
if(!g[i][a])
{
g[i][a]=1;
add_(i,a+key);
}
}
}
int res=hungary(n);
int nn=n+m-res;
for(int i=n+1;i<=nn;i++)
for(int j=1;j<=nn;j++)
add_(i,j+key),g[i][j]=1;
for(int i=1;i<=n;i++)
for(int j=m+1;j<=nn;j++)
add_(i,j+key),g[i][j]=1;
hungary(nn); ecnt=0;clr(first,-1);
for(int i=1;i<=nn;i++)
if(link[i+key]!=-1)add_(i+nn,link[i+key]); for(int i=1;i<=nn;i++)
for(int j=1;j<=nn;j++)
if(g[i][j])add_(i,j+nn);
solve(2*nn);
printf("Case #%d:\n",cas++);
int ans[1000];
for(int i=1;i<=n;i++)
{
int en=0;
for(int j=1;j<=m;j++)
if(g[i][j]&&belong[j+nn]==belong[i])ans[en++]=j;
printf("%d",en);
for(int i=0;i<en;i++)
printf(" %d",ans[i]);
puts("");
}
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDU4685 Prince and Princess 完美搭配+良好的沟通的更多相关文章

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

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

  2. HDU4685 Prince and Princess【强连通】

    题意: 有n个王子和m个公主,每个王子都会喜欢若干个公主,也就是王子只跟自己喜欢的公主结婚,公主就比较悲惨, 跟谁结婚都行.然后输出王子可能的结婚对象,必须保证王子与任意这些对象中的一个结婚,都不会影 ...

  3. HDU4685:Prince and Princess(二分图匹配+tarjan)

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

  4. LESS-Middleware:Node.js 和 LESS 的完美搭配

    LESS 是一个编写 CSS 的很好的方式 ,让你可以使用变量,嵌套规则,混入以及其它许多有用的功能,它可以帮助您更好地组织你的 CSS 代码. 最近我一直在研究 Node.js ,并想用 less- ...

  5. 10635 - Prince and Princess

    Problem D Prince and Princess Input: Standard Input Output: Standard Output Time Limit: 3 Seconds In ...

  6. UVa10653.Prince and Princess

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. uva 10635 - Prince and Princess(LCS)

    题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...

  8. Prince and Princess HDU - 4685(匹配 + 强连通)

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

  9. UVA - 10635 Prince and Princess LCS转LIS

    题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...

随机推荐

  1. word2vec 中的数学原理具体解释(一)文件夹和前言

      word2vec 是 Google 于 2013 年开源推出的一个用于获取 word vector 的工具包.它简单.高效.因此引起了非常多人的关注. 因为 word2vec 的作者 Tomas ...

  2. Windows下实战Apache+PHP [转]

        一.Apache 1.下载登陆Apache Lougne(http://www.apachelounge.com/download/),找到最新版本的Apache.笔者下载的是带IPv6和Cr ...

  3. Kendo UI开发教程(20): Kendo MVVM 数据绑定(九) Text

    Text绑定可以使用ViewModel来设置DOM元素的文本属性,如果需要设置input,textarea,或select的显示,需要使用value属性. 1 <span data-bind=& ...

  4. 为什么java源文件中只允许一个public类存在

    1.提出问题 为什么java源文件中只允许一个public类存在? 2.分析问题 问题涉及到的条件:源文件的名字    public类     main方法 一般我们在编写一个源文件的时候: 一个pu ...

  5. Python集成开发环境(Eclipse+Pydev)

    刚開始学习python,就用Editplus, Notepad++来写小程序, 后来接触了Sublime Text2.认为很不错,没事写写代码.就用编辑器Sublime Text2,最好再配搭一个ap ...

  6. 499 - What's The Frequency, Kenneth?

     What's The Frequency, Kenneth?  #include <stdio.h> main() { int i; char *suffix[]= { "st ...

  7. Python 学习入门(21)—— 线程

    本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础以及Python两个线程标准库的完整介绍及使用示例. 1. 线程基础 1.1. 线程状态 线程有5种状态,状态转换的过程如下图 ...

  8. 自绘XP风格菜单

    这是以前写的代码,自绘XP风格的菜单,硬盘坏了后以为没了,最后写的一个软件要自定义风格,“翻箱倒柜”的终于在我可爱的古董机^_^上找到了一个应用的例子.还是把它放到Blog上来,即可共享又可作为备用 ...

  9. Boost Thread学习笔记三

    下面先对condition_impl进行简要分析.condition_impl在其构造函数中会创建两个Semaphore(信号量):m_gate.m_queue,及一个Mutex(互斥体,跟boost ...

  10. 四种方法解决DIV高度自适应问题

    本文和大家重点讨论一下解决DIV高度自适应的方法,这里主要从四个方面来向大家介绍,相信通过本文学习你对DIV高度自适应问题会有更加深刻的认识. DIV高度自适应 关于DIV高度的自适应,一直是个让人头 ...