当前位置:网站首页>B. Party(图论/暴力/顶点的度数)
B. Party(图论/暴力/顶点的度数)
2022-07-29 21:24:00 【罗gkv】
题意
有n个人,m对朋友关系。
举办聚会,邀请若干人,使得被邀请的人中,是朋友关系的有偶数对。
每个人有个开心值a[i],如果第i个人没有被邀请,会获得a[i]的不开心值。
问怎么邀请,使总得不开心值最小。
思路
如果m是偶数,此时全部邀请即可,不开心值为0
以下考虑m是奇数的场景。
我们将每个人看做顶点,朋友关系看作边。
问题转化成,从这图中,删除若干顶点,使得剩余的图,边数为偶数。
有两种情况
- 删除度数为奇数的顶点,此时只需删一个。
- 删除度数为偶数的顶点,此时需要删除两个相邻顶点。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 200010;
int n, m;
int a[maxn], degree[maxn];
int x[maxn], y[maxn];
void solve() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
degree[i] = 0;
}
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &x[i], &y[i]);
++degree[x[i]];
++degree[y[i]];
}
int ans = inf;
// 1. m is even
if (m % 2 == 0) {
ans = 0;
}
// 2. m is odd
// 2.1 delete i where degree[i] is odd.
for (int i = 1; i <= n; ++i) {
if (degree[i] & 1) {
ans = min(ans, a[i]);
}
}
// 2.2 delete neighbor x, y where degree[x],degree[y] is even
for (int i = 1; i <= m; ++i) {
if (degree[x[i]] % 2 == 0 && degree[y[i]] % 2 == 0) {
ans = min(ans, a[x[i]] + a[y[i]]);
}
}
printf("%d\n", ans);
}
int main() {
int t;
scanf("%d", &t);
// t = 1;
while (t--) {
solve();
}
}
最后
weixin gongzhonghao搜 对方正在debug,关注下,一起快乐刷题吧
边栏推荐
- 给图片左上角加logo标识、左下角加时间和地址、地址到达指定长度换行
- GBASE 8s 数据库复合索引
- UDP协议详解
- Xshell 7 prompts "To continue using this program, you must apply the latest update or use a new version"
- IDEA 快捷键
- The cornerstone of distributed: reliability - What a tangled web we weave
- GBASE 8s 如何并行执行update statistics
- WeChat Mini Program 30 Customizing Templates and Obtaining User Login Credentials
- 容器网络硬核技术内幕 (小结-下)
- 系列(jupyter自动保存失败)
猜你喜欢
随机推荐
leetcode122. Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II(简单)
OneNote tutorial, how to take notes in OneNote?
"Introduction to nlp + actual combat: Chapter 7: Dataset loading in pytorch and the use of its own datasets"
VSCode 插件大全
《nlp入门+实战:第七章:pytorch中数据集加载和自带数据集的使用》
PointPillars 工程复现
Bug fix: Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]
网络通信编程基础,BIO,NIO
APM电机输出逻辑(Motors类详解)
在Ferora35中安装oracle-database-xe-21c
940. Different subsequences II
惠普服务器硬盘指示灯不亮或显示蓝色
关于云计算的海量数据存储模型[通俗易懂]
How to implement your personal knowledge base?
小程序微信定位不准
容器网络硬核技术内幕 (22) 安全与自由兼顾
[Database] mysql date format conversion
Analysis of Crypto in Pi 2
【点云】M3DeTR: Multi-representation, Multi-scale, Mutual-relation 3D Object Detection with Transformers
Xshell 7 prompts "To continue using this program, you must apply the latest update or use a new version"








![LeetCode 593 Valid Squares [Math] HERODING's Road to LeetCode](/img/c2/34624c9c7693ba40d0b3724c0db611.png)
