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 ...
随机推荐
- PrestoSPI安全扩展
由于Presto官方文档和谷歌搜索都没有相关的内容,git项目中也没有支持sentry的安全插件扩展,因此只能从源码中寻找答案,在梳理完SPI包的安全相关源码结构后,已实现了一个自定义的安全插件,经验 ...
- 深入浅出node.js游戏服务器开发1——基础架构与框架介绍
2013年04月19日 14:09:37 MJiao 阅读数:4614 深入浅出node.js游戏服务器开发1——基础架构与框架介绍 游戏服务器概述 没开发过游戏的人会觉得游戏服务器是很神秘的 ...
- python操作数据库-SQLSERVER-pyodbc
刚开始学python时,大家都习惯用pymssql去读写SQLSERVER.但是实际使用过程中,pymssql的读写性能以及可靠性的确不如pyodbc来的好. 正如微软官方推荐使用pyodbc库,作为 ...
- 容易忽略的CSS3属性
flex布局模型 1. flex-direction: row | row-reverse | column | column-reverse flex-direction 属性决定主轴的方向(即项 ...
- alfred workflow 开发
alfred python demo
- 彻底解决Python编码问题
1. 基本概念 字符集(Character set) 解释:文字和符合的总称 常见字符集: Unicode字符集 ASCII字符集(Unicode子集) GB2312字符集 编码方法(Encoding ...
- git工具上传项目到码云
首先,你需要在本地安装git客户端,此处简单易懂,略过然后,在本地建好文件夹,以本人为例,我的路径为 E:\git_project,此时需要通过鼠标右键选择:git bush here 如图所示然后会 ...
- 讲讲python中函数的参数
python中函数的参数 形参:定义函数时代表函数的形式参数 实参:调用函数时传入的实际参数 列如: def f(x,y): # x,y形参 print(x, y) f(1, 2) # 1, 2 实参 ...
- JVM调优:GC 参数
2019独角兽企业重金招聘Python工程师标准>>> JVM调优:GC 参数 博客分类: java jvm 参考: <Memory Management in the Jav ...
- Codeforces Round #509 (Div. 2) A. Heist 贪心
There was an electronic store heist last night. All keyboards which were in the store yesterday were ...