Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4021    Accepted Submission(s): 1309

Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And it is normal that a fight or a quarrel breaks out, but we will still play together after that, because we are kids. 
Now, there are 2n kids, n boys numbered from 1 to n, and n girls numbered from 1 to n. you know, ladies first. So, every girl can choose a boy first, with whom she has not quarreled, to make up a family. Besides, the girl X can also choose boy Z to be her boyfriend when her friend, girl Y has not quarreled with him. Furthermore, the friendship is mutual, which means a and c are friends provided that a and b are friends and b and c are friend. 
Once every girl finds their boyfriends they will start a new round of this game—marriage match. At the end of each round, every girl will start to find a new boyfriend, who she has not chosen before. So the game goes on and on.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?
 
Input
There are several test cases. First is a integer T, means the number of test cases. 
Each test case starts with three integer n, m and f in a line (3<=n<=100,0<m<n*n,0<=f<n). n means there are 2*n children, n girls(number from 1 to n) and n boys(number from 1 to n).
Then m lines follow. Each line contains two numbers a and b, means girl a and boy b had never quarreled with each other. 
Then f lines follow. Each line contains two numbers c and d, means girl c and girl d are good friends.
 
Output
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
 
Sample Input
1
4 5 2
1 1
2 3
3 2
4 2
4 4 
1 4
2 3
 
Sample Output
2
 
Author
starvae
 
Source
 题意:
有n个女生n个男生女生可以和她喜欢的男生配对也可以和她的朋友喜欢的男生配对,当所有的人都配对了时游戏结束,要求每一轮游戏中互相已经配对的两个人以后就不能再配对了问可以进行多少轮游戏。
输入t组数据
输入n,m,f,对人,m个喜欢关系,f个朋友关系
输入m行a b 表示女生a喜欢男生b
输入f行a b 表示女生a和女生b是朋友
代码:
//并查集处理配对关系,然后二分轮数,源点连向女生容量为轮数(每人玩这些次),女生连向可以配对的男生,
//容量为1(只能配对一次),男生连向汇点容量也是轮数。看最大流是否等于n*轮数。
//今下午脑子坏掉了,二分写挫了wa到死。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
int mp[maxn][maxn],fat[maxn];
int find(int x){
return fat[x]==x?x:fat[x]=find(fat[x]);
}
void connect(int x,int y){
int xx=find(x),yy=find(y);
if(xx!=yy) fat[yy]=xx;
}
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
int n,m,s,t;
vector<Edge>edges;
vector<int>g[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n){
this->n=n;
for(int i=;i<n;i++) g[i].clear();
edges.clear();
}
void Addedge(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));//反向弧
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool Bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<(int)g[x].size();i++){
Edge &e=edges[g[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=;
d[e.to]=d[x]+;
q.push(e.to);
}
}
}
return vis[t];
}
int Dfs(int x,int a){
if(x==t||a==) return a;
int flow=,f;
for(int&i=cur[x];i<(int)g[x].size();i++){
Edge &e=edges[g[x][i]];
if(d[x]+==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>){
e.flow+=f;
edges[g[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
}
int Maxflow(int s,int t){
this->s=s;this->t=t;
int flow=;
while(Bfs()){
memset(cur,,sizeof(cur));
flow+=Dfs(s,inf);
}
return flow;
}
}dc;
bool solve(int n,int mid){
dc.init(*n+);
for(int i=;i<=n;i++){
dc.Addedge(,i,mid);
for(int j=n+;j<=*n;j++)if(mp[i][j])
dc.Addedge(i,j,);
dc.Addedge(i+n,*n+,mid);
}
return n*mid==dc.Maxflow(,*n+);
}
int main()
{
int t,n,m,f;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&f);
int a,b;
memset(mp,,sizeof(mp));
for(int i=;i<=*n;i++) fat[i]=i;
for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
mp[a][b+n]=;
}
for(int i=;i<=f;i++){
scanf("%d%d",&a,&b);
connect(a,b);
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(find(i)==find(j))
for(int k=n+;k<=*n;k++)
mp[i][k]=mp[j][k]=(mp[i][k]||mp[j][k]);
}
}
int l=,r=n,mid,ans=;
while(l<=r){
mid=(l+r)/;
if(solve(n,mid)){
ans=mid;
l=mid+;
}
else r=mid-;
}
printf("%d\n",ans);
}
return ;
}

HDU 3081 最大流+二分的更多相关文章

  1. HDU 3081 最大流+并查集

    题意:有n个男生和n个女生,玩结婚游戏,由女生选择男生:女生可以选择不会和她吵架的男生以及不会和她闺蜜吵架的男生,闺蜜的闺蜜也是闺蜜.问你最多可以进行多少轮,每一轮每个女生只能选择一个之前她没选过的男 ...

  2. hdu 3228 (最大流+二分)

    题意:一共有N个城市,一些城市里有金矿,一些城市里有仓库,金矿和仓库都有一个容量,有M条边,每条边是双向的,有一个权值,求将所有金矿里的储量都运送到仓库中,所需要经过的道路中,使最大的权值最小 思路: ...

  3. HDU 3277 最大流+二分

    Marriage Match III Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  5. HDU 3081 Marriage Match II(二分法+最大流量)

    HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...

  6. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

  7. HDU 1532 最大流入门

    1.HDU 1532 最大流入门,n个n条边,求第1点到第m点的最大流.只用EK做了一下. #include<bits/stdc++.h> using namespace std; #pr ...

  8. Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。

    /** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...

  9. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. [转载]启动tomcat时,一直卡在Deploying web application directory这块的解决方案

    转载:https://www.cnblogs.com/mycifeng/p/6972446.html 本来今天正常往服务器上扔一个tomcat 部署一个项目的, 最后再启动tomcat 的时候 发现项 ...

  2. Too many open files错误与解决方法

    致前辈:该问题的解决思路给了我很大的启发,文章作者Lis, Linux资深技术专家. 问题现象:这是一个基于Java的web应用系统,在后台添加数据时提示无法添加,于是登陆服务器查看Tomcat 日志 ...

  3. 算法模板の数学&数论

    1.求逆元 int inv(int a) { ) ; return (MOD - MOD / a) * inv(MOD % a); } 2.线性筛法 bool isPrime[MAXN]; int l ...

  4. Python中__name__属性的妙用

    在Python中,每一个module文件都有一个built-in属性:__name__,这个__name__有如下特点: 1 如果这个module文件是被别的文件导入的,那么,该__name__属性的 ...

  5. LintCode-366.斐波纳契数

    斐波纳契数列 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是:0, 1, 1 ...

  6. lintcode-32-最小子串覆盖

    最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 注意事项 如果在source中没有这样的子串,返回"" ...

  7. TCP系列29—窗口管理&流控—3、Nagle算法

    一.Nagle算法概述 之前我们介绍过,有一些交互式应用会传递大量的小包(称呼为tinygrams),这些小包的负载可能只有几个bytes,但是TCP和IP的基本头就有40bytes,如果大量传递这种 ...

  8. SVM之核函数

    SVM之问题形式化 SVM之对偶问题 >>>SVM之核函数 SVM之解决线性不可分 写在SVM之前——凸优化与对偶问题 上一篇SVM之对偶问题中讨论到,SVM最终形式化为以下优化问题 ...

  9. 【.NET】- Task.Run 和 Task.Factory.StartNew 区别

    Task.Run 是在 dotnet framework 4.5 之后才可以使用, Task.Factory.StartNew 可以使用比 Task.Run 更多的参数,可以做到更多的定制. 可以认为 ...

  10. chrome扩展程序中以编程方式插入内容脚本不生效的问题

    chrome扩展程序中内容脚本有两种插入方式:(https://crxdoc-zh.appspot.com/extensions/content_scripts) 1. 清单文件: 这种方式会在打开每 ...