[思维]Party Codeforces1711B

A club plans to hold a party and will invite some of its nn members. The nn members are identified by the numbers 1,2,…,n1,2,…,n. If member ii is not invited, the party will gain an unhappiness value of aiai.

There are mm pairs of friends among the nn members. As per tradition, if both people from a friend pair are invited, they will share a cake at the party. The total number of cakes eaten will be equal to the number of pairs of friends such that both members have been invited.

However, the club's oven can only cook two cakes at a time. So, the club demands that the total number of cakes eaten is an even number.

What is the minimum possible total unhappiness value of the party, respecting the constraint that the total number of cakes eaten is even?

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n≤1051≤n≤105, 0≤m≤min(105,n(n−1)2)0≤m≤min(105,n(n−1)2)) — the number of club members and pairs of friends.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1040≤ai≤104) — the unhappiness value array.

Each of the next mm lines contains two integers xx and yy (1≤x,y≤n1≤x,y≤n, x≠yx≠y) indicating that xx and yy are friends. Each unordered pair (x,y)(x,y) appears at most once in each test case.

It is guaranteed that both the sum of nn and the sum of mm over all test cases do not exceed 105105.

Output

For each test case, print a line containing a single integer – the minimum possible unhappiness value of a valid party.

Example

input

4

1 0

1

3 1

2 1 3

1 3

5 5

1 2 3 4 5

1 2

1 3

1 4

1 5

2 3

5 5

1 1 1 1 1

1 2

2 3

3 4

4 5

5 1

output

0

2

3

2

题意: 你可以邀请n个人参加聚会,如果第i个人不被邀请那就会产生一个不开心值ai,另外有m对朋友关系,要求邀请的人中具有偶数对朋友关系,问在这个条件的限制下能够得到的最小不开心值。

分析: 如果朋友对数为偶数,那邀请全部人过来就好了,此时答案为0。如果朋友对数为奇数,那么就需要删掉某些人,可以发现最多删掉两个人就能使关系对数成为偶数,对于度为奇数的点可以直接删掉它,对于度为偶数的点可以删掉它以及它的一个度为偶数的相邻点,此时总关系数同样为偶数,这样就可以遍历一遍所有点来求出最小不开心值了。

关于为什么最多删两个人有一个证明,假设最小不开心值来自于删除了两个以上的人的情况,如果其中某个人的度为奇数,那么直接删除那个人来得到的答案一定更优,如果删除的人度数都是偶数,那么他们中一定存在某对相邻点,否则不可能得到偶数对朋友关系,那这时直接删除相邻点对也能得到更优的答案,这就证明出最多删除两个人了。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#define int long long
using namespace std;

int w[100005];
vector<int> to[100005];

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int n, m;
		scanf("%lld%lld", &n, &m);
		for(int i = 1; i <= n; i++){
			scanf("%lld", &w[i]);
			to[i].clear();
		}
		for(int i = 1; i <= m; i++){
			int u, v;
			scanf("%lld%lld", &u, &v);
			to[u].push_back(v);
			to[v].push_back(u);
		}
		if(m%2 == 0){
			puts("0");
			continue;
		}
		int ans = 0x3f3f3f3f;
		for(int i = 1; i <= n; i++){
			if(to[i].size()&1)
				ans = min(ans, w[i]);
			else{
				for(int j = 0; j < to[i].size(); j++){
					int v = to[i][j];
					if(to[v].size()%2 == 0)
						ans = min(w[v]+w[i], ans);
				}
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值