2019 ACM/ICPC Asia Regional shanxia D Miku and Generals (二分图黑白染色+01背包)
Miku is matchless in the world!” As everyone knows, Nakano Miku is interested in Japanese generals, so Fuutaro always plays a kind of card game about generals with her. In this game, the players pick up cards with generals, but some generals have contradictions and cannot be in the same side. Every general has a certain value of attack power (can be exactly divided by 100100 ), and the player with higher sum of values will win. In this game all the cards should be picked up.
This day Miku wants to play this game again. However, Fuutaro is busy preparing an exam, so he decides to secretly control the game and decide each card's owner. He wants Miku to win this game so he won't always be bothered, and the difference between their value should be as small as possible. To make Miku happy, if they have the same sum of values, Miku will win. He must get a plan immediately and calculate it to meet the above requirements, how much attack value will Miku have?
As we all know, when Miku shows her loveliness, Fuutaro's IQ will become 00 . So please help him figure out the answer right now!
Input
Each test file contains several test cases. In each test file:
The first line contains a single integer T(1 \le T \le 10)T(1≤T≤10) which is the number of test cases.
For each test case, the first line contains two integers: the number of generals N(2 \le N \le 200)N(2≤N≤200) and thenumber of pairs of generals that have contradictions M(0 \le M \le 200)M(0≤M≤200).
The second line contains NN integers, and the ii-th integer is c_ici, which is the attack power value of the ii-th general (0 \le c_i \le 5\times 10^4)(0≤ci≤5×104).
The following MM lines describe the contradictions among generals. Each line contains two integers AA and BB , which means general AA and BB cannot be on the same side (1 \le A , B \le N)(1≤A,B≤N).
The input data guarantees that the solution exists.
Output
For each test case, you should print one line with your answer.
Hint
In sample test case, Miku will get general 22 and 33 .
样例输入复制
1
4 2
1400 700 2100 900
1 3
3 4
样例输出复制
2800 题意:给你n个数,让你分成两堆,堆与堆之间的和的差值最小,中间值与值之间可能存在矛盾,不能分在同一组,问你分配后最大的那一组的值是多少
思路:首先我们知道很多矛盾对,但是有可能一个人与多个人都有矛盾,所以为了避免分组的时候发生矛盾,我们很容易就想到二分图的黑白染色,这样我们首先就可以
解决矛盾问题,然后我们要每个联通块,我们首先可以求所有的和2x,那么x肯定是最优情况,我们又再每个连通块里面选最小的值,和就是y,(一个数为连通块时,另一个就是0,设计巧妙的地方),
然后我们要使y更接近x,所以我们就求x-y的容量,在每个连通块的差值里面选多少交换来求最大,然后这里就相当于转换为一个01背包问题
#include<bits/stdc++.h>
#define maxn 100005
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll n,m;
ll a[maxn],b[maxn];
vector<int> mp[maxn];
ll s,dp[maxn];
int vis[maxn];
void dfs(int x,int num){
vis[x]=s+num;
for(int i=;i<mp[x].size();i++){
if(vis[mp[x][i]]==){
if(num==){
dfs(mp[x][i],);
}
else{
dfs(mp[x][i],);
}
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
for(int i=;i<maxn;i++) mp[i].clear();
scanf("%lld%lld",&n,&m);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
a[i]/=;
}
int x,y;
for(int i=;i<m;i++){
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
s=;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
if(vis[i]==){
dfs(i,);
s+=;
}
}
s--;
ll sum=,num=;
memset(b,,sizeof(b));
for(int i=;i<=n;i++){
b[vis[i]]+=a[i];
sum+=a[i];
}
int q=; for(int i=;i<=s;i+=){
num+=min(b[i],b[i+]);
b[q++]=abs(b[i]-b[i+]);
}
ll z=sum/+sum%;
z-=num;
memset(dp,,sizeof(dp));
for(int i=;i<q;i++){
for(int j=z;j>=b[i];j--){
dp[j]=max(dp[j],dp[j-b[i]]+b[i]);
}
}
ll z1=num;
if(dp[z] != -) z1+=dp[z];
ll z2=sum-z1;
printf("%lld\n",max(z1,z2)*);
}
}
/*
1
4 2
1400 700 2100 900
1 3
3 4
*/
2019 ACM/ICPC Asia Regional shanxia D Miku and Generals (二分图黑白染色+01背包)的更多相关文章
- hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...
- hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...
- hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...
- hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup
hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...
- hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others) Memory ...
- hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元
hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分
I Count Two Three Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- 阿里云入选Gartner 2019 WAF魔力象限,唯一亚太厂商!
近期,在全球权威咨询机构Gartner发布的2019 Web应用防火墙魔力象限中,阿里云Web应用防火墙成功入围,是亚太地区唯一一家进入该魔力象限的厂商! Web应用防火墙,简称WAF.在保护Web应 ...
- UNP学习第六章(二)
一.描述符就绪条件 对于引起select返回套接字“就绪”的条件我们必须讨论得更明确: (1)满足一下塞个条件中的仍和一个时,一个套接字准备好读. a)该套接字接收缓冲区中的数据字节数不大于等于套接字 ...
- STM32点LED灯
一.建立项目模板 这里的user中重复引用了system_stm32f10x.c Output中选择Create HEXFile,并且可以选择输出路径. Listing中可以选择输出路径. 然后在C/ ...
- 匹配Luhn算法:可用于检测银行卡卡号
匹配Luhn算法:可用于检测银行卡卡号 /** * http://www.cnblogs.com/JnKindle/p/5798974.html * * 匹配Luhn算法:可用于检测银行卡卡号 * * ...
- AcWing 215. 破译密码 (莫比乌斯反演)打卡
达达正在破解一段密码,他需要回答很多类似的问题: 对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. 作为达达的同学,达达希望得到你的帮助. ...
- [CSP-S模拟测试41]题解
中间咕的几次考试就先咕着吧…… A.夜莺与玫瑰 枚举斜率.只考虑斜率为正且不平行于坐标轴的直线,最后把$ans\times 2$再$+1$即可. 首先肯定需要用$gcd(i,j)==1$确保斜率的唯一 ...
- 【Spring Boot】Spring Boot项目设置多个配置文件,并在生产环境中的Tomcat设置对应的配置文件
1.修改Spring Boot项目配置文件 除了主配置文件,另外创建2个配置文件,注意命名要用application-*.properties 主配置文件中写入,使用dev作为开发配置 spring. ...
- Toast 使用方法大全
原文地址:http://daikainan.iteye.com/blog/1405575 Toast 是一个 View 视图,快速的为用户显示少量的信息. Toast 在应用程序上浮动显示信息给用户, ...
- linux c开发项目过程总结
软件工程有瀑布模型,迭代模型等. 使用linux c语言来开发项目,当然也是遵循这样的思想,先要问题定义-->需求分析--->原型设计---->编码及单元测试--->集成测试及 ...
- 262K Color
262K色=2^18=262144色. 320*240是指屏幕分辨率. 你可以理解为一块黑板,这款黑板宽是3.2M,长是2.4米,以1cm为最小单位,整个黑板被分为320*240个小格子,这个小格子里 ...