当前位置:网站首页>Codeforces Round #810 (Div. 2), problem: (B) Party
Codeforces Round #810 (Div. 2), problem: (B) Party
2022-07-26 22:49:00 【ZaneBobo】
一.题意
你有n位同学,你想邀请他们中的一些人(可以都邀请上,也可以都不邀请)参加你的派对,他们之间有m对朋友关系(每个人可能有多个朋友)每对朋友会吃掉你的一块cake,但是你的厨师只能做出来偶数块cake,所以你的派对上只能出现偶数对朋友关系,所以你邀请到的人构成的朋友关系个数肯定是个偶数,这时候你可能就会不邀请班上的某些同学了,每位同学不被邀请的不开心值是ai,让你找到何种方案不开心值的和最小,求出这个不开心值的和,并输出。
输入:
T
n,m
a1~an
(然后下面是m对朋友关系)
输出:
最小的不开心值之和
二.思路
这个题里面含有一个小思维,首先我下了个结论,那就是作者要么排除0人要么排除1人要么排除2人不去邀请。
①排除0人:本来m就是偶数
②排除1人:m是奇数,这一个人所占有的朋友关系一定是奇数,这样m-这个人的朋友关系个数和 才是偶数。
③排除2人:m是奇数,这2个人每个人所占有的朋友关系一定是偶数(因为如果是奇数,那么他1个人就够了,不满足贪心思想),不但两人都是偶数,并且这两个人也要是朋友。这样去掉两人,去掉的朋友关系和才会是奇数(这里可以自己思考一下为什么)。
如果m是奇数,我们把①和②两种情况算出来的最小不开心值和再求个最小值就是最终答案,
具体实现方式看代码,有注释。
要是有更好的实现方式可以评论区call我OWO学习学习!
三.代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
const int N = 1e5+10;
typedef pair<int, int> PII;
vector<int> v[N];
map<int,bool> st;
int a[N],cnt[N];
void solve()
{
st.clear();
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
cnt[i]=0;
v[i].clear();//清空vector O(n)时间复杂度
}
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);//朋友关系
v[b].push_back(a);
cnt[a]++;//cnt[i] i有几个朋友
cnt[b]++;
}
if(m%2==0)
{
puts("0");
return;
}
int minone=0x3f3f3f3f;//情况①的最小不开心值
for(int i=1;i<=n;i++)
{
if(cnt[i]&1)
{
st[i]=1;
minone=min(a[i],minone);
}
}
int mintwo=0x3f3f3f3f;//情况①的最小不开心值和
for(int i=1;i<=n;i++)
{
st[i]=1;
if(cnt[i]&1) continue;
int siz=v[i].size();
for(int j=0;j<siz;j++)//枚举当前枚举的i的所有朋友满足(情况②的第二个限制条件)
{
if(st[v[i][j]]) continue;
if(cnt[v[i][j]]%2==0)
{
mintwo=min(a[i]+a[v[i][j]],mintwo);
}
}
}
cout<<min(mintwo,minone)<<endl;//①②取min
}
int main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}
边栏推荐
- 2022 open source work of the latest text generated image research (papers with code)
- 2022zui新抖音24小时循环值守直播监控(一)直播间开播监控
- TIM输出比较——PWM
- STM32入门教程第二讲
- [MySQL] MySQL startup and shutdown commands and some error reports to solve problems
- Autojs learning - realize the display of date and lunar time
- Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan
- OSPF static experiment
- C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
- 7.7 SHEIN希音笔试
猜你喜欢

ACM mode input and output exercise

Text to image intensive reading df-gan:a simple and effective baseline for text to image synthesis

静态路由缺省路由vlan实验

HCIA(网络初级综合实验练习)

Text to image intensive reading of paper gr-gan: gradually refine text to image generation

超出隐藏显示省略号

7.13 Weilai approved the written examination in advance

Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (II)

VLAN原理简述、具体实验配置

Flink1.13.6详细部署方式
随机推荐
OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)
[详解C语言]一文带你认识C语言,让你醍醐灌顶
6.28同花顺笔试
Static routing default routing VLAN experiment
The basic configuration of static routing (planning of IP address and configuration of static routing) realizes the accessibility of the whole network.
Fastjson handles string escape characters
Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (I)
The gradient descent method and Newton method are used to calculate the open radical
[explain C language in detail] this article takes you to know C language and makes you impressed
OSPF在MGRE环境下配置及LSA的优化---减少LSA更新量(汇总、特殊区域)
2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room
[paddleseg source code reading] paddleseg export static graph export Trace in py file
Solution to high collapse
ensp中的简单静态路由
C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
机械硬盘选购指南——从选购经历谈起
2022最新直播监控24小时监控(三)直播间弹幕解析
Text to image论文精读GR-GAN:逐步细化文本到图像生成 GRADUAL REFINEMENT TEXT-TO-IMAGE GENERATION
Unity Huatuo revolutionary hot update series [1]
HCIA(网络初级综合实验练习)