Codeforces Round #810 (Div. 2)B. Party

B. Party

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

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

There are m pairs of friends among the n 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 t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains two integers n and m (1≤n≤105, 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 n integers a1,a2,…,an (0≤ai≤104) — the unhappiness value array.

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

It is guaranteed that both the sum of n and the sum of m over all test cases do not exceed 105.

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

Example
inputCopy

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
outputCopy
0
2
3
2
Note
In the first test case, all members can be invited. So the unhappiness value is 0.

In the second test case, the following options are possible:

invite 1 and 2 (0 cakes eaten, unhappiness value equal to 3);
invite 2 and 3 (0 cakes eaten, unhappiness value equal to 2);
invite only 1 (0 cakes eaten, unhappiness value equal to 4);
invite only 2 (0 cakes eaten, unhappiness value equal to 5);
invite only 3 (0 cakes eaten, unhappiness value equal to 3);
invite nobody (0 cakes eaten, unhappiness value equal to 6).
The minimum unhappiness value is achieved by inviting 2 and 3.
In the third test case, inviting members 3,4,5 generates a valid party with the minimum possible unhappiness value.

原题链接:B. Party

题目大意:
厨师会做偶数块蛋糕,派对需要邀请偶数个人(可以是零),总共有n个人,n个人有m对朋友,第i个人如果未被邀请,有s[i]的不开心值,求不开心值最小。

分析:记录一下每个人的朋友关系,再记录一下每个人的朋友个数。
情况1:如果总共朋友的关系为偶数个,则每个人都被邀请,不开心值为:0
情况2:

删去朋友个数为奇数的人:使得找到最小不开心值的人删去
删去朋友个数为偶数并且互为朋友的两个人:删去这两个人之后,这两个人的“另外朋友”的个数也为奇数,
	奇数加奇数还是偶数,所以删去的还是奇数个人
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1E5 + 100;

int n,m,T,t;

signed main(){
    cin>>t;
    while(t--){
        cin >> n >> m;
        vector<int> g[n+1];
        vector<int> in(n+1),s(n+1);
        for(int i = 1 ; i <= n ; i ++)
            cin >> s[i];
        for(int i = 0 ; i < m ; i ++){
            int a,b;cin >> a >> b;
            g[a].push_back(b);      //每个人之间的关系
            g[b].push_back(a);  
            in[a]++;        //每个人朋友的个数
            in[b]++;
        }
        if(m&1){
            int ans = 1e17;     //define long long 了,所以这里直接往大了开
            for(int i = 1 ; i <= n ; i ++){ //情况2
                if(in[i]&1){                //当这个人朋友的个数为奇数时:最小值就是不开心值的最小值
                    ans = min(ans,s[i]);
                }else{
                    for(auto j : g[i]){
                        if(in[j]%2==0){     //朋友个数为偶数时,删去这对朋友,使得其他朋友的朋友个数还是偶数,成立
                                            //这里只需要删去一对朋友关系即可以使得m朋友的对数为偶数
                            ans = min(ans,s[i]+s[j]);
                        }
                    }
                }
            }
            cout << ans << endl;
        }else{
            puts("0");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值