首页 技术 正文
技术 2022年11月10日
0 收藏 836 点赞 4,176 浏览 717 个字

正题

题目链接:https://www.luogu.com.cn/problem/P4643


题目大意

给出\(n\)个点\(m\)条边的无向图,两个人轮流选择一个未被选择的点加入点集。

然后每个人的权值为选出的点的导出子图点权加边权和。

两个人都希望自己的权值减去对方的权值最大

求先手的权值减去后手的权值

\(1\leq n\leq 10^4,1\leq m\leq 10^5\)


解题思路

结论就是把边权均分到点权处。

证明的话假设两个点之间的点权为\(w\)。

那么如果两边颜色不同那么这个均分出来的权值会统计一个\(\frac{w}{2}-\frac{w}{2}=0\)的权值

如果两边颜色相同那么就会统计上这个权值。排序然后一个一个选就好了

时间复杂度\(O(n\log n+m)\)


code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int n,m,w[N],v[N],p[N],x[N],y[N],e[N],ans;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]),v[i]=w[i]*2,p[i]=i;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x[i],&y[i],&e[i]);
v[x[i]]+=e[i];v[y[i]]+=e[i];
}
sort(v+1,v+1+n);
for(int i=n;i>=1;i-=2)
ans+=v[i]-v[i-1];
printf("%d\n",ans/2);
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,107
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,583
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,430
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,201
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919