hdu6667 Roundgod and Milk Tea(Hall定理)

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=6667

题目描述

Problem Description

Roundgod is a famous milk tea lover at Nanjing University second to none. This year, he plans to conduct a milk tea festival. There will be n classes participating in this festival, where the ith class has ai students and will make bi cups of milk tea.
Roundgod wants more students to savor milk tea, so he stipulates that every student can taste at most one cup of milk tea. Moreover, a student can't drink a cup of milk tea made by his class. The problem is, what is the maximum number of students who can drink milk tea?

Input

The first line of input consists of a single integer T (1≤T≤25), denoting the number of test cases.
Each test case starts with a line of a single integer n (1≤n≤10^6), the number of classes. For the next n lines, each containing two integers a,b (0≤a,b≤10^9), denoting the number of students of the class and the number of cups of milk tea made by this class, respectively.
It is guaranteed that the sum of n over all test cases does not exceed 6×10^6.

Output

For each test case, print the answer as a single integer in one line.

Sample Input

1

2

3 4

2 1

Sample Output

3

题意

有n个班级,每个班级有ai个人,可以做出bi杯奶茶,一个班级的人不能喝本班做的奶茶,可以喝别的班做的奶茶,问最多有多少人喝到奶茶。

思路

首先要了解Hall定理以及它的一个推论:

https://www.cnblogs.com/dummyummy/p/10311769.html

Hall定理我们离散数学学到过,想不到在这里用到了。。。

将人与奶茶分别看作二分图两边的顶点,班级则是所有顶点的子集,同一个班级的人与别的班级的奶茶之间有边相连,且任意两个不同班级的人可连接全部的奶茶。这样问题转化为了求二分图的最大匹配。利用Hall定理的推论很容易就能求出结果。

代码

#include<bits/stdc++.h>
#define ll long long
#define maxn 1000010
using namespace std;
ll a[maxn],b[maxn],n;

int main(){
	int t,i;
	cin>>t;
	while(t--){
		ll y=0,x=0,ans=0;
		scanf("%lld",&n);
		for(i=1;i<=n;i++){
			scanf("%lld%lld",&a[i],&b[i]);
			x+=a[i];
			y+=b[i];
		}
		if(x<y){
			for(i=1;i<=n;i++){
				ans=max(ans,a[i]-(y-b[i]));
			}
			printf("%lld\n",x-ans);
		}
		else{
			for(i=1;i<=n;i++){
				ans=max(ans,b[i]-(x-a[i]));
			}
			printf("%lld\n",y-ans);			
		}
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值