强连通+二分匹配(hdu4685 Prince and Princess)
Prince and Princess
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1267 Accepted Submission(s): 358
For all princes,give all the princesses that they love. So, there is a maximum number of pairs of prince and princess that can marry.
Now for each prince, your task is to output all the princesses he can marry. Of course if a prince wants to marry one of those princesses,the maximum number of marriage pairs of the rest princes and princesses cannot change.
For each test case, the first line contains two integers n and m (1<=n,m<=500), means the number of prince and princess.
Then n lines for each prince contain the list of the princess he loves. Each line starts with a integer ki(0<=ki<=m), and then ki different integers, ranging from 1 to m denoting the princesses.
Then output n lines. For each prince, first print li, the number of different princess he can marry so that the rest princes and princesses can still get the maximum marriage number.
After that print li different integers denoting those princesses,in ascending order.
2 1 2
2 1 2
1 3
1 4
Case #2:
2 1 2
题意:一个王国里有n个王子和m个公主,公主可以跟任何王子结婚,但是王子可以选择和他喜欢的公主结婚,喜欢的公主可以有多个,但是最终只能选择其中一个结婚,公主一样也必须只能和一个王子结婚,现在给出n和m,接下来n行,第i行首先有个数ki,然后k个数,代表第i个王子喜欢的公主编号,接下来输出n行,表示每个王子可以有多少公主可以选择,并从小大输出公主标号,并保证这些选择不会有冲突,且满足最大匹配
分析:首先建立二分图,左列图的点为王子,右列图的点事公主,然后把王子与器喜欢的公主建立单向边然后跑一遍最大匹配,并记录每个公主和哪个王子的匹配策略,然后公主和其结婚的王子建立一条边,之后跑一遍tarjan强连通的算法,最后对每个王子进行遍历,所有和该王子在一个联通块中的公主都是可选对象
程序:
#include"cstdio"
#include"cstring"
#include"cstdlib"
#include"cmath"
#include"string"
#include"map"
#include"cstring"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#define inf 0x3f3f3f3f
#define M 10009
#define eps 1e-8
#define INT int
#define LL __int64
using namespace std;
struct node
{
int u,v,next;
}edge[];
int cmp(int a,int b)
{
return a<b;
}
int h[M];
int x[M];
int y[M];
int head[M];
int dfn[M];
int belong[M];
int low[M];
int use[M];
int sum[M];
int in[M];
int out[M];
int t,indx,num,m,n;
stack<int>q;
void init()
{
t=;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
int finde(int u)
{
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(!use[v])
{
use[v]=;
if(!y[v]||finde(y[v]))
{
use[v]=;
y[v]=u;
x[u]=v;
return ;
}
}
}
return ;
}
void tarjan(int u,int id)
{
dfn[u]=low[u]=++indx;
q.push(u);
use[u]=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v,i);
low[u]=min(low[u],low[v]);
}
else if(use[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
int p;
num++;
do
{
p=q.top();
q.pop();
use[p]=;
belong[p]=num;
sum[num]++; }while(p!=u);
}
}
int match(int nl)
{
memset(x,,sizeof(x));
memset(y,,sizeof(y));
int ans=;
for(int i=;i<=nl;i++)
{
if(!x[i])
{
memset(use,,sizeof(use));
ans+=finde(i);
}
}
return ans;
}
void slove()
{
int cnt=match(n);
int all=m+n-cnt;
for(int i=n+;i<=all;i++)
{
for(int j=;j<=all;j++)
add(i,j+);
}
for(int i=;i<=all;i++)
{
for(int j=m+;j<=all;j++)
{
//if(x[i]==0)
add(i,j+);
}
}
match(all);
for(int i=;i<=all;i++)
{
if(x[i])
add(x[i],i);
}
num=indx=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(use,,sizeof(use));
memset(sum,,sizeof(sum));
for(int i=;i<=all;i++)
{
if(!dfn[i])
{
tarjan(i,-);
}
}
for(int u=;u<=n;u++)
{
int fuck=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(v>n&&belong[u]==belong[v]&&v<=+m)
h[fuck++]=v-;
}
sort(h,h+fuck,cmp);
printf("%d",fuck);
for(int i=;i<fuck;i++)
printf(" %d",h[i]);
printf("\n");
}
}
int main()
{
int T,kk=;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<=n;i++)
{
int k,j;
scanf("%d",&k);
while(k--)
{
scanf("%d",&j);
add(i,+j);
}
}
printf("Case #%d:\n",kk++);
slove();
}
}
强连通+二分匹配(hdu4685 Prince and Princess)的更多相关文章
- HDU4685 Prince and Princess【强连通】
题意: 有n个王子和m个公主,每个王子都会喜欢若干个公主,也就是王子只跟自己喜欢的公主结婚,公主就比较悲惨, 跟谁结婚都行.然后输出王子可能的结婚对象,必须保证王子与任意这些对象中的一个结婚,都不会影 ...
- HDU4685 Prince and Princess 完美搭配+良好的沟通
意甲冠军:今天,有n王子,m公主.现在给他们配对,与王子会嫁给一个男人,他喜欢.公主无法做出选择. 这标题去咬硬,还有一类似的题目poj1904.那个题目也是给王子与公主配对,但那个是王子公主各n个, ...
- 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 ...
- Prince and Princess HDU - 4685(匹配 + 强连通)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU4685:Prince and Princess(二分图匹配+tarjan)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4685 Prince and Princess 二分图匹配+tarjan
Prince and Princess 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 Description There are n pri ...
- UVa10653.Prince and Princess
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA - 10635 Prince and Princess LCS转LIS
题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...
随机推荐
- Python - KMP算法
def kmp_match(tex, pat): n = len(tex) m = len(pat) tex = '0' + tex pat = '0' + pat pi = [] pi.append ...
- 【转】Problems with HTTPS, HttpWebRequest, and iOS?
We're using HttpWebRequest to hit HTTPS urls, on iOS. In Debug, local builds, etc. everything works ...
- Region的预分区
1.预分区的方式 共有四种方式 2.帮助信息 help 'create' 3.第一种方式 4.在页面上查看效果(端口号:60010) 5.第二种方式 )创建文件,并在文件中书写分区的值 )创建表 6. ...
- C/C++ 判断主机字节存储序列
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0oAAADFCAIAAADltpUqAAAgAElEQVR4nOyd65XyvA6FqYASKIEWqI
- win2008主机IIS7.x 关于web.config设置301重定向
win2008主机IIS7.x 关于web.config设置301重定向 要求:windows主机是IIS7.0或以上的版本 方法如下: 在网站的根目录下新建web.config文件并将一下代码加入到 ...
- iOS 冒泡排序
//冒泡排序 -(NSArray*)Bubble_Sort:(NSArray*)oldArray { NSMutableArray * newArray = [NSMutableArray array ...
- iOS发送短信
iPhone开发,发送短信的方法: iPhone开发中,发送短信方法有二: 1.URL Scheme,这样不可以设置短信内容 1 2 3 [[UIApplication sharedApplicati ...
- Xcode编译WebApps找不到js的错误解决办法<转>
使用Xcode做WebApps时,使用UIWebview来调用一个页面,有时会遇到问题,其一就是编译的时候出现黄色感叹号的Warning,js文件都报错:warning: no rule to pro ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- Android 自定义控件之app标题栏的封装
在app的开发中,每一个页面都有上面的标题栏,总不能在开发的过程中没个界面都写一个标题栏的布局,所以为了开发的方便,将该标题栏进行的封装,以后在实际的开发工作中,也可以将该封装好的标题栏控件直接拿来使 ...