hdu 3277 二分+拆点最大流
2014-01-18 03:11
309 查看
Marriage Match III
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1080 Accepted Submission(s): 311
[align=left]Problem Description[/align]
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the ever game of play-house . What a happy time as so many friends play 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. As 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. On the other hand, in order to
play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept her unconditionally whether they had quarreled before or not.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?
[align=left]Input[/align]
There are several test cases. First is an integer T, means the number of test cases.
Each test case starts with three integer n, m, K and f in a line (3<=n<=250, 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.
[align=left]Output[/align]
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
[align=left]Sample Input[/align]
1
4 5 1 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
[align=left]Sample Output[/align]
3
题意:与hdu 3081 差不多,只是多了一个每一个女孩可以选择k个不喜欢的男孩的条件。
解题思路:将女孩拆点G1,G2,对每一个女孩,G1与喜欢的男孩连边,G2与她不喜欢的男孩连边,源点连G1流量为二分值,男孩与汇点连边流量二分值,跑最大流。
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014/1/18 0:58:25
File Name :E.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=100000;
int head[10000],tol,dep[10000],n;
struct node
{
int next,to,from,cap;
}edge[300000];
void add(int u,int v,int cap)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].next=head[v];
head[v]=tol++;
}
int bfs(int s,int t)
{
int que[maxn],front=0,rear=0;
memset(dep,-1,sizeof(dep));
dep[s]=0;que[rear++]=s;
while(front!=rear)
{
int u=que[front++];front%=maxn;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>0&&dep[v]==-1)
{
dep[v]=dep[u]+1;
que[rear++]=v;
rear%=maxn;
if(v==t)return 1;
}
}
}
return 0;
}
int dinic(int s,int t)
{
int i,res=0,top;
int Stack[maxn],cur[maxn];
while(bfs(s,t))
{
memcpy(cur,head,sizeof(head));
int u=s;top=0;
while(1)
{
if(u==t)
{
int min=inf,loc;
for(int i=0;i<top;i++)
if(min>edge[Stack[i]].cap)
{
min=edge[Stack[i]].cap;
loc=i;
}
for(int i=0;i<top;i++)
{
edge[Stack[i]].cap-=min;
edge[Stack[i]^1].cap+=min;
}
res+=min;
top=loc;
u=edge[Stack[top]].from;
}
for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
if(edge[i].cap&&dep[u]+1==dep[edge[i].to])break;
if(cur[u]!=-1)
{
Stack[top++]=cur[u];
u=edge[cur[u]].to;
}
else
{
if(top==0)break;
dep[u]=-1;
u=edge[Stack[--top]].from;
}
}
}
return res;
}
int fa[10000];
int find(int x)
{
if(fa[x]!=x)
fa[x]=find(fa[x]);
return fa[x];
}
int vis[330][333],K;
bool judge(int mid)
{
memset(head,-1,sizeof(head));tol=0;
for(int i=1;i<=n;i++)
{
add(0,i,mid);
add(i,i+n,K);
add(i+2*n,3*n+1,mid);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(vis[i][j])
add(i,j+2*n,1);
else
add(i+n,j+2*n,1);
}
if(dinic(0,3*n+1)==mid*n)
return 1;
return 0;
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int T,i,j,k,m,f;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&K,&f);
memset(head,-1,sizeof(head));tol=0;
memset(vis,0,sizeof(vis));
vector<int> ss[500];
while(m--)
{
scanf("%d%d",&i,&j);
vis[i][j]=1;
ss[i].push_back(j);
}
for(i=1;i<=n;i++)fa[i]=i;
while(f--)
{
scanf("%d%d",&i,&j);
int fx=find(i);
int fy=find(j);
if(fx==fy)continue;
fa[fx]=fy;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(find(i)!=find(j))continue;
int cnt=ss[i].size();
for(k=0;k<cnt;k++)
vis[j][ss[i][k]]=1;
cnt=ss[j].size();
for(k=0;k<cnt;k++)
vis[i][ss[j][k]]=1;
}
}
int left=0,right=n,mid;
while(left<right)
{
mid=(left+right+1)>>1;
if(judge(mid))left=mid;
else right=mid-1;
}
printf("%d\n",left);
}
return 0;
}
相关文章推荐
- HDU 3277 Marriage Match III(二分+最大流)
- HDU 3277 Marriage Match III(中等,好题)[二分最大流]+拆点
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- HDU 3277 最大流+二分
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- HDU 3277 Marriage Match III【并查集+二分+最大流】
- hdu 3277 Marriage Match III【最大流+并查集+二分枚举】
- hdu 3277(二分+最大流+拆点+离线处理+模板问题...)
- hdu 3277 Marriage Match III(二分最大流+并查集+判断满流+拆点)
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- hdu 3277 (二分+并查+最大流)
- hdu 3277 Marriage Match III(最大流,二分,并查集)
- HDU 3277 Marriage Match III(二分+最大流)
- hdu 2444 The Accomodation of Students(判二分+最大匹配)
- HDU 3277 Marriage Match III (最大流+拆点+并查集)
- HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)
- HDU - 3585 maximum shortest distance(二分+最大团)
- [HDU] 1025 Constructing Roads In JGShining's Kingdom - 二分的求最大递增非连续子序列
- hdu 3081(二分+最大流+并查集)
- HDU 2819 Swap (最大二分匹配+输出路径)