Marriage Match II HDU - 3081(最大流+并查集+二分)

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<nn,0<=f<n). n means there are 2n 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
给出n个男生和n个女生,m条没有吵架的男女关系,f个女生之间的朋友关系
有n个男生和n个女生配对,由女生挑选男生,女生只跟自己没有吵架的男生配对,或者和自己朋友的没有吵架的男生配对(朋友的朋友也算朋友),求最多能配对几轮?

这题需要二分答案来跑最大流,将女生和能配对的男生建边,容量为1,将源点与女生建边,容量为二分的值,将汇点与男生建边,容量也为二分的值,剩下只剩女生和朋友的没吵过架的男生,这里采用并查集存女生的朋友关系,当两个女生是朋友关系时,将一个女生与另一个女生的没有吵架的男生建边,容量为1,注意要防止重边,所以要加一个数组判断。

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int n,m,S,T,ne,head[maxn],cur[maxn],depth[maxn],maxflow;
int F,f[maxn],vis[105][105];
queue<int> q;
struct edge
{
	int next,to,cost;
}e[maxm*2];
void init()
{
	memset(head,-1,sizeof(head));
	ne=0;
	maxflow=0;
}
void init2()
{
	for(int i = 0; i <= n; i++) f[i] = i;
	memset(vis,0,sizeof(vis));
}
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=0;
	e[ne].next=head[v];
	head[v]=ne++;
}
int bfs(int s,int t)
{
	while(!q.empty()) q.pop();
	memset(depth,-1,sizeof(depth));
	for(int i=0;i<=T;i++) cur[i]=head[i];
	depth[s]=0;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(depth[e[i].to]==-1&&e[i].cost)
			{
				depth[e[i].to]=depth[u]+1;
				q.push(e[i].to);
			}
		}
	}
	if(depth[t]==-1) return -1;
	else return 1;
}
int dfs(int s,int t,int limit)
{
	if(!limit||s==t) return limit;
	int f,flow=0;
	for(int i=cur[s];i!=-1;i=e[i].next)
	{
		cur[s]=i;
		if(depth[e[i].to]==depth[s]+1&&(f=dfs(e[i].to,t,min(limit,e[i].cost))))
		{
			flow+=f;
			limit-=f;
			e[i].cost-=f;
			e[i^1].cost+=f;
			if(!limit) break;
		}
	}
	return flow;
}
void dinic(int s,int t)
{
	while(bfs(s,t)!=-1)
	{
		maxflow+=dfs(s,t,INF);
	}
}
int find(int x)
{
  return f[x] == x ? x : f[x] = find(f[x]);
}
void unit(int x, int y)
{
  int fx = find(x);
  int fy = find(y);
  f[fx] = fy;
}
bool same(int x, int y)
{
  return find(x) == find(y);
}
void build(int x)
{
	init();
	for(int i=1;i<=n;i++)
	{
		add(S,i,x);
		add(i+n,T,x);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(vis[i][j]) add(i,j+n,1);
			if(same(i,j))
			{
				for(int k=1;k<=n;k++)
				{
					if(vis[i][k]&&!vis[j][k])
					{
						vis[j][k]=1;
						add(j,k+n,1);
					}
				}
			}
		}
	}
	dinic(S,T);
}
int main()
{
	int tt;
	scanf("%d",&tt);
	while(tt--)
	{
		scanf("%d %d %d",&n,&m,&F);
		S=0;
		T=2*n+1;
		init2();
		for(int i=0;i<m;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			vis[x][y]=1;
		}
		for(int i=0;i<F;i++)
		{
			int x,y;
			scanf("%d %d",&x,&y);
			unit(x,y);
		}
		int st=0,ed=n+1;
		while(ed-st>1)
		{
			int mid=(ed+st)/2;
			build(mid);
			if(maxflow==n*mid) st=mid;
			else ed=mid;
		}
		printf("%d\n",st);
	}
}
关于“Marriage Matching 3190”,经过网络搜索发现这可能是一个特定编号的问题或者算法题目,通常出现在编程竞赛、学术研究或计算机科学领域中的匹配问题讨论中。以下是对此主题的详细解答: --- ### 方法一:理解婚姻匹配的基本概念 婚姻匹配问题是图论和组合优化领域的经典问题之一,主要涉及如何在一组男性和女性之间找到稳定的配对关系。稳定婚配问题(Stable Marriage Problem, SMP)由Gale和Shapley于1962年提出,其核心目标是在给定偏好列表的情况下避免不稳定配对。 对于编号为3190的具体问题,可能是某个平台上的变种版本,例如增加约束条件或调整输入输出形式。 --- ### 方法二:实现 Gale-Shapley 算法解决基本问题 经典的解决方案是使用 Gale-Shapley 算法来求解稳定婚配问题。该算法的核心思想如下: 1. 每位单身男子向他尚未求婚过的最喜欢女子求婚。 2. 女子从当前收到的所有求婚者中选择她最喜欢的未婚男子,并暂时接受他的求婚。 3. 如果某女子已经订婚但收到了更优的选择,则解除现有订婚并将原伴侣重新归入单身状态。 4. 重复以上过程直到所有人都成功配对为止。 下面是 Gale-Shapley 算法的一个简单 Python 实现: ```python def gale_shapley(men_prefs, women_prefs): n = len(men_prefs) free_men = list(range(n)) engaged = [-1] * n proposals = [0] * n while free_men: man = free_men.pop(0) woman = men_prefs[man][proposals[man]] proposals[man] += 1 if engaged[woman] == -1: engaged[woman] = man else: current_man = engaged[woman] if women_prefs[woman].index(man) < women_prefs[woman].index(current_man): free_men.append(current_man) engaged[woman] = man else: free_men.append(man) return {w: m for w, m in enumerate(engaged)} # 示例数据 men_prefs = [[0, 1, 2], [1, 2, 0], [2, 0, 1]] women_prefs = [[1, 2, 0], [0, 1, 2], [2, 1, 0]] result = gale_shapley(men_prefs, women_prefs) print(result) ``` --- ### 方法三:针对特殊编号问题的扩展思考 如果“3190”代表某种特殊的限制条件或复杂场景,可以考虑以下方向进行改进: - **加权匹配**:引入权重表示每对之间的亲密度或其他指标,寻找总权重最大的匹配方案。 - **多对多匹配**:允许一个人同时与多人建立联系,适用于社交网络分析等实际应用。 - **动态更新机制**:当参与者的偏好随时间变化时,设计实时调整策略以保持稳定性。 具体实现需要结合问题描述进一步明确规则及边界情况。 --- ### 方法四:查找在线资源获取完整信息 由于“Marriage Matching 3190”可能来源于某些特定网站或书籍章节,建议访问以下渠道了解详情: - 编程竞赛平台(如LeetCode、Codeforces、AtCoder),搜索关键词“marriage matching”查看是否有对应编号的任务; - 学术论文数据库(Google Scholar、ResearchGate),查阅有关SMP及其衍生模型的研究成果; - 开源社区论坛(Stack Overflow、GitHub Issues),与其他开发者交流经验心得。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值