“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 100), 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 0. 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≤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≤N≤200) and thenumber of pairs of generals that have contradictions⁡ M(0≤M≤200).

The second line contains N integers, and the i-th integer is ci, which is the attack power value of the i-th general (0≤ci≤5×104).

The following M lines describe the contradictions among generals. Each line contains two integers A and B , which means general A and B cannot be on the same side (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 2 and 3.

样例输入

  1. 1
  2. 4 2
  3. 1400 700 2100 900
  4. 1 3
  5. 3 4

样例输出

  1. 2800

题意

N个数选任意个给A,其余的给B,M对冲突,A和B里的数都不能冲突,问A和>=B和,并且使A-B差值最小,输出A的值。保证有解。

题解

由于A集合里的数不能冲突,考虑二分图01染色,对于每个连通块。

差值为|0的个数-1的个数|,dp[i]表示A-B差值为i是否可行。

知道差值i,总和sum,那么A=(sum+i)/2。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int a[],color[];
  5. bool dp[],dp1[];
  6. vector<int>G[];
  7. int sumc[];
  8. void dfs(int u,int col)
  9. {
  10. color[u]=col;
  11. sumc[col]+=a[u];
  12. for(int i=;i<G[u].size();i++)
  13. {
  14. int v=G[u][i];
  15. if(color[v]==-)
  16. dfs(v,col^);
  17. }
  18. }
  19. int main()
  20. {
  21. int t;
  22. scanf("%d",&t);
  23. while(t--)
  24. {
  25. int n,m,u,v,sum=;
  26. scanf("%d%d",&n,&m);
  27. for(int i=;i<=n;i++)G[i].clear(),color[i]=-;
  28. for(int i=;i<=n;i++)scanf("%d",&a[i]),a[i]/=,sum+=a[i];
  29. for(int i=;i<=m;i++)
  30. {
  31. scanf("%d%d",&u,&v);
  32. G[u].push_back(v);
  33. G[v].push_back(u);
  34. }
  35. for(int i=;i<=sum;i++)dp[i]=;
  36. dp[]=;
  37. for(int i=;i<=n;i++)
  38. if(color[i]==-)
  39. {
  40. sumc[]=sumc[]=;
  41. dfs(i,);
  42. int ca=abs(sumc[]-sumc[]);
  43. for(int j=sum;j>=;j--)
  44. {
  45. if(dp[j])
  46. {
  47. if(abs(j+ca)<=sum)dp1[abs(j+ca)]=;
  48. dp1[abs(j-ca)]=;
  49. }
  50. }
  51. for(int j=sum;j>=;j--)
  52. dp[j]=dp1[j],dp1[j]=;
  53. }
  54. for(int i=;i<=sum;i++)
  55. if(dp[i])
  56. {
  57. printf("%d\n",(sum+i)/*);
  58. break;
  59. }
  60. }
  61. return ;
  62. }

ACM-ICPC 2019 西安邀请赛 D.Miku and Generals(二分图+可行性背包)的更多相关文章

  1. 2019 ICPC 陕西西安邀请赛 D. Miku and Generals

    传送门:https://nanti.jisuanke.com/t/39271 题意: 给你n个人,每个人有一个权值 a_i ​,(a_i​是可以被100整除的))现在需要你将n个人分成两组,有m个关系 ...

  2. 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, ...

  3. 西安区域赛 D.Miku and Generals 二分图+背包

    Miku and Generals Describe "Miku is matchless in the world!" As everyone knows, Nakano Mik ...

  4. 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛

    比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...

  5. [多校联考2019(Round 5 T2)]蓝精灵的请求(二分图染色+背包)

    [多校联考2019(Round 5)]蓝精灵的请求(二分图染色+背包) 题面 在山的那边海的那边住着 n 个蓝精灵,这 n 个蓝精灵之间有 m 对好友关系,现在蓝精灵们想要玩一个团队竞技游戏,需要分为 ...

  6. 2017 ACM/ICPC(西安)赛后总结

    早上8:00的高铁,所以不得不6点前起床,向火车站赶……到达西安后已经是中午,西工大距离西安北站大概3小时车程的距离,只好先解决午饭再赶路了……下午3.30的热身赛,一行人在3.35左右赶到了赛场,坐 ...

  7. XTU OJ 1209 Alice and Bob 2014(嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛)

    Problem Description The famous "Alice and Bob" are playing a game again. So now comes the ...

  8. 2019 西安邀请赛 D

    //n件物品,m种关系,(有关系的2个不能在同一组) //把所有物品分为2组,希望最后2组的差值尽可能小,输出较大者 /* 二分图涂色+可行性(01)背包 dp[i] =1表示 最后差值为i可行 建图 ...

  9. 2019 西安邀请赛 M

    Problem Description There are n planets ∼n. Each planet is connected to other planets through some t ...

随机推荐

  1. spss-数据抽取-拆分与合并

    spss-数据抽取-拆分与合并 数据抽取也成为数据拆分,是指保留.抽取原数据表中某些字段.记录的部分信息,形成一个新字段.新纪录.分为:字段拆分和随机抽样两种方法. 一:字段拆分 如何提取" ...

  2. 09_springmvc异常处理

    一.异常处理思路 系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生. 系统的dao ...

  3. 【LGP4705】玩游戏

    题目 显然这个题的期望就是逗你玩的,我们算出来总贡献除以\(nm\)就好了 设\(ans_t=\sum_{i=1}^n\sum_{j=1}^n(a_i+b_j)^t\) 二项式定理展开一下 \[ans ...

  4. @RestControllerAdvice作用及原理

    原文:Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理 在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@Exceptio ...

  5. Could not resolve placeholder 'CUST_INDUSTORY' in string value "${CUST_INDUSTORY}"

    问题描述 项目中的资源文件中写了个properties文件,内容这样的 CUST_FROM=002 CUST_INDUSTORY=001 CUST_LEVEL=006 在springmvc配置文件中加 ...

  6. Open CASCADE Technology: IGES Support

    2015/6/4 Open CASCADE Technology: IGES Support http://www.opencascade.org/doc/occt­6.7.0/overview/ht ...

  7. NoSQL与关系数据库的比较

  8. [转]Visual Studio 2010单元测试(1)--运行和定义普通单元测试

    Visual Studio 2010 运行和定义单元测试 在VS2010中,单元测试的功能很强大,使得建立单元测试和编写单元测试代码,以及管理和运行单元测试都变得简单起来,通过私有访问器可以对私有方法 ...

  9. 页面自动执行(加载)js的几种方法

    https://www.cnblogs.com/2huos/p/js-autorun.html 一.JS方法1.最简单的调用方式,直接写到html的body标签里面: <html> < ...

  10. 【DM642学习笔记八】色度重采样

    TI文档"TMS320C64x DSP Video Port_VCXO Interpolated Control (VIC)Port.pdf"第3.5.2 Chrominance ...