题目链接:点击查看

题目大意:给出一棵树表示一个国家,点1表示首都,现在需要分配 k 个城市为工业城市,其余 n - k 个城市为旅游城市,这个国家会定时在首都召开会议,换句话说,所有工业城市都必须派出一个使者到达首都(路径唯一),使者经过的旅游城市的个数为幸福指数,问如何分配 k 个工业城市,能使得每次召开会议时,所有使者的幸福指数之和最大,输出这个最大值

题目分析:首先不难看出,如果一个节点 x 想要变成工业城市时,当且仅当 x 的子树中都已经是工业城市时最优,否则选择子树中的结点变为工业城市肯定更优,所以一开始可以从点1开始dfs,跑出每个节点的子树大小 num[ i ],以及深度 deep[ i ] (deep[ 1 ] = 0 ),这样选择一个节点 x 变成工业城市时的贡献就是 deep[ i ] - ( num[ i ]  - 1 ) ,画个图不难看出来,排个序选前 k 个就行了

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;vector<int>node[N];int deep[N];LL num[N],temp[N];void dfs(int u,int fa,int dep)
{deep[u]=dep;num[u]=1;for(auto v:node[u]){if(v==fa)continue;dfs(v,u,dep+1);num[u]+=num[v];}temp[u]=deep[u]-num[u]+1;
}int main()
{
#ifndef ONLINE_JUDGE
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);int n,k;scanf("%d%d",&n,&k);for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);node[u].push_back(v);node[v].push_back(u);}dfs(1,-1,0);sort(temp+1,temp+1+n,greater<LL>());LL ans=0;for(int i=1;i<=k;i++)ans+=temp[i];printf("%lld\n",ans);return 0;
}

CodeForces - 1337C Linova and Kingdom(贪心)相关推荐

  1. Linova and Kingdom

    Linova and Kingdom 题目来源:Codeforces Round #635 (Div. 2) C题 Writing light novels is the most important ...

  2. CF1336 A. Linova and Kingdom

    A. Linova and Kingdom 题意 给你一颗 n n n个节点根为1号节点的树,选 k k k个城市作为工业城市,其余为旅游城市,工业城市的使节每经过旅游城市开心值+1,求所有工业城市的 ...

  3. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  4. C. Linova and Kingdom

    链接:https://codeforces.ml/contest/1337/problem/C Writing light novels is the most important thing in ...

  5. Codeforces 671E Organizing a Race (贪心、线段树)

    题目链接 https://codeforces.com/contest/671/problem/E 题解 完全不会做--基本是抄lk的代码 ruogu的题解: https://www.luogu.co ...

  6. CodeForces - 1529E Trees of Tranquillity(贪心+线段树)

    题目链接:https://vjudge.net/problem/CodeForces-1529E 题目大意:给出两棵根节点为 111 的树,分别称为 AAA 树和 BBB 树,现在通过两棵树可以构造出 ...

  7. [CodeForces 1603C] Extreme Extension(贪心 + 数论分块优化dp)

    problem CodeForces solution observation1:\text{observation1}:observation1: 对于一个非空子段 [l,r][l,r][l,r], ...

  8. codeforces 416C C. Booking System(贪心)

    题目链接: codeforces 416C 题目大意: 给出n个请求,每个请求包括客人数量和支付金额,再给出m个桌子,包括桌子大小,问如何安排才能最大盈利.给出最大盈利和一个能够最大盈利的方案. 题目 ...

  9. Codeforces 540B School Marks 【贪心构造】

    题目链接:Codeforces 540B School Marks Little Vova studies programming in an elite school. Vova and his c ...

最新文章

  1. 为什么不推荐使用 stop、suspend 方法中断线程?
  2. android fastjson漏洞_亲手带你 Debug Fastjson 的安全漏洞
  3. 最小延迟调度问题算法c语言,【算法概论】贪心算法:最小延迟调度问题
  4. MySQL中如何关闭事务的自动提交
  5. 聊聊JavaScript和Scala的表达式 Expression
  6. 用HttpReports快速搭建API分析平台
  7. 字符搜索正则表达式语法详解
  8. java2实用教程第五版耿祥义电子版_java2实用教程第五版
  9. Java读写NFC标签Ntag2x芯片源码
  10. 回忆杀:那些年,山寨机,暗号*#220807#,和玩过的mrp游戏
  11. 华为转正答辩ppt范文_华为新员工转正答辩
  12. Lorenz系统的吸引子相图
  13. html中bottom的作用,css bottom属性怎么用
  14. Python语言程序设计课程论文--飞机大战
  15. qq空间留言板删除 php,QQ空间刷留言php代码
  16. Linux内核延迟写机制学习
  17. ei检索的cpss_我想查EI会议论文检索情况,请问是如何查询的呢?
  18. HEXOFontmin
  19. excel表格xlsx密码强制解除手机,忘记excel表格xlsx密码如何找回?
  20. python爬虫--小说爬取

热门文章

  1. oracle某些服务在有其他服务,oracle 监听无法启动,出现某些服务再未由其他服务或程序使用时自动停止。...
  2. ext js ajax表单校验,Extjs之grid单元格编辑校验(示例代码)
  3. MySQL服务的启动与停止-使用命令行工具
  4. Oracle vs MySQL
  5. 设计模式在Netty中的应用-责任链模式源码举例
  6. RocketMQ的发展历史
  7. 统一返回的json时间格式
  8. JUL执行原理和流程
  9. 依赖注入_set方法注入_构造器注入
  10. 自定义工具类:工具类介绍