Codeforces Round #635C Linova and Kingdom 思维
Linova and Kingdom
题意
现在有一颗n个节点的树,每个节点是一个城市,现在要选出k个城市作为工业城市,其他城市作为旅游城市,现在每个工业城市要派出一名特使前往根节点,每个特使的幸福度为经过的旅游城市的数量,求最大的幸福度总和。
思路
对于某个节点u,如果u是工业城市,那么它的子节点肯定是工业城市。
如果它的某个子节点不是,我们完全可以把它的子节点作为工业城市,而不是u。
我们再看如果选择了u作为工业城市,幸福度发生的变化。
假如本来总幸福度是ans
定义根节点的深度为1,u是不是工业城市只会对它的子树有影响
ans=ans-(sum[u]-1)*dep[u]先减去u为旅游城市时,子树的幸福度
ans=ans+sum[u]*(dep[u]-1)加上u变为工业城市时,子树的幸福度
化简一下:
ans=ans+dep[u]-sum[u]
如果u节点作为工业城市,它的贡献为dep[u]-sum[u],即深度-子树大小
我们按照每个节点的深度-子树大小从大到小排序,取前k个。
代码
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N=1e6+10;
vector<int>xiao[N],vec;
int dep[N],sum[N];
void dfs(int u,int pre)
{
sum[u]=1;
for(int v:xiao[u])
{
if(v==pre) continue;
dep[v]=dep[u]+1;
dfs(v,u);
sum[u]+=sum[v];
}
}
bool cmp(int a,int b)
{
return (dep[a]-sum[a])>(dep[b]-sum[b]);
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
xiao[u].pb(v);
xiao[v].pb(u);
}
for(int i=1;i<=n;i++)
vec.pb(i);
dep[1]=1;
dfs(1,1);
sort(vec.begin(),vec.end(),cmp);
ll maxn=0;
for(int i=0;i<k;i++)
{
int u=vec[i];
maxn+=dep[u]-sum[u];
}
printf("%lld\n",maxn);
return 0;
}
Codeforces Round #635C Linova and Kingdom 思维的更多相关文章
- Educational Codeforces Round 40 C. Matrix Walk( 思维)
Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memor ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #143 (Div. 2) (ABCD 思维场)
题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...
- Codeforces Round #395 (Div. 2)(A.思维,B,水)
A. Taymyr is calling you time limit per test:1 second memory limit per test:256 megabytes input:stan ...
- Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Codeforces Round #539 (Div. 2) D 思维
https://codeforces.com/contest/1113/problem/D 题意 将一个回文串切成一段一段,重新拼接,组成一个新的回文串,问最少切几刀 题解 首先无论奇偶串,最多只会切 ...
- Codeforces Round #542(Div. 2) CDE 思维场
C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...
- Codeforces Round #438 C - Qualification Rounds 思维
C. Qualification Rounds time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- B - Bash and a Tough Math Puzzle CodeForces - 914D (线段树的巧妙应用)
题目大意:当输入2时,将p处的点的值修改为x, 当输入1时,判断区间[L,R]的gcd是否几乎正确,几乎正确的定义是最多修改一个数,使得区间[L,R]的gcd为x. 题解:用线段树维护一个gcd数组, ...
- 在vue中使用ztree树插件
插件资源及api:树官网 本事例是在vue3.0+中演示,事例是实际项目中正在用的组件所以部分打了马赛克. 1.插件准备(提前准备好插件文件) 可以直接在官网下载,搭建好脚手架后将准备好的文件放在li ...
- 5. class--extends
ES5: function article(x,y){ this.x = x; this.y = y; } article.prototype.say = function() { return (t ...
- [php]微信测试号调取acces_token,自定义菜单以及被动响应消息
<?php /**自己写的 */ $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid(); $wechatObj-> ...
- echarts迁徙图
前段时间在echarts社区,看见别人写的echarts迁徙图,学习并也写了一个 预览地址: https://gallery.echartsjs.com/editor.html?c=xYS-YtzOa ...
- 使用Idea当中的快捷键快速查看继承关系或其图表的两种方法
一.Idea当中有两种方法可以查看继承关系 在Idea当中选中一个类,然后按Ctrl+H,可以快速查看当前所选类的继承关系,如下图: 同样选中一个类,按CTRL+ALT+U,即可生成当前类的继承关 ...
- SweetAlert - 演示6种不同的提示框效果
http://www.sucaihuo.com/js/190.html http://www.cnblogs.com/beiz/p/5238124.html
- Inno Setup 大师 Tlama
https://stackoverflow.com/users/960757/tlama
- java并发中的Synchronized关键词
文章目录 为什么要同步 Synchronized关键词 Synchronized Instance Methods Synchronized Static Methods Synchronized B ...
- Libra教程之:move语言的特点和例子
文章目录 move语言的特点 资源优先 灵活性 安全性 可验证性 Move语句初探 点对点支付交易脚本 Currency Module move语言的特点 Libra的目标是打造一个全球话的金融和货币 ...