链接:

https://codeforces.com/problemset/problem/1336/A

题意:

有一个n节点树,根为1,现在要设置k个工业城市,剩下n-k个旅游城市,让每个工业城市前往根,每经过一个旅游城市值加一,求最大值多少

解:

思考一下结果就能知道,肯定先选离根节点最远的点,然后把叶子选完了怎么办呢,再思考下

。。。。。。

能发现选了一个非叶子结点的点前,他的叶节点一定是被选择了(为什么捏

然后选的这个点会让他所有的叶节点路径上多一个工业城市,所以他叶方向有几个点就要减去多少

一个节点就是Xi=离根的距离-叶方向的点的数量

从最大选k个就行(思维,建图)

PS:做的时候return的js没+1,案例居然也过了,一直WA到案例6=-=查了半天

实际代码:

#include<iostream>
#include<bits/stdc++.h>
#define csh(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long int ll;
const int Size=2E5+10;
int sz[Size];
bool book[Size];
vector<int>tree[Size];
void add(int t,int w)
{tree[t].push_back(w);tree[w].push_back(t);
}
int Find(int G,int RD)
{int js=0;book[G]=1;if(G!=1 && tree[G].size()==1){for(auto i:tree[G]){if(book[i]==1) {sz[G]=RD;return 1;}}}for(auto ye:tree[G]){if(book[ye]==1) continue;js+=Find(ye,RD+1);}sz[G]=RD-js;return js+1;
}
int main()
{ll ans=0;int n,k;cin>>n>>k;for(int i=2;i<=n;i++){int t,w;cin>>t>>w;add(t,w);}Find(1,0);/*for(int i=1;i<=n;i++){cout<<sz[i]<<endl;}*/sort(sz+1,sz+n+1);int mao=n;for(int i=1;i<=k;i++){ans+=sz[mao];mao--;}cout<<ans<<endl;
}

限制:

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

1336A - 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. C. Linova and Kingdom

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

  4. CodeForces - 1337C Linova and Kingdom(贪心)

    题目链接:点击查看 题目大意:给出一棵树表示一个国家,点1表示首都,现在需要分配 k 个城市为工业城市,其余 n - k 个城市为旅游城市,这个国家会定时在首都召开会议,换句话说,所有工业城市都必须派 ...

  5. CF1336A Linova and Kingdom

    看这里 大意: 有一个有 n 个点的树,以 1 为根,你可以选择 k 个节点,使得这 k 个节点到 1 节点的最短路径中经过的非选择的点最多. 思路: 简单树形dp 我们肯定会优先最好的叶子结点,这个 ...

  6. bottom sheets_Excel 2013中的SHEET和SHEETS函数

    bottom sheets I've been experimenting with the new SHEET and SHEETS functions in Excel 2013, to see ...

  7. bottom sheets_使用Sheetson建立由Google Sheets支持的免费电子邮件列表

    bottom sheets When building an initial MVP for your product, it's wise to avoid unnecessary expenses ...

  8. Codeforces补题记录(1)

    文章目录 Codeforces补题记录(1) 1.Codeforces Round #632 (Div. 2)(2020.4.11) A.Little Artem B.Kind Anton *C.Eu ...

  9. Codeforces Round #635 (Div. 2)(A~D)题解

    Codeforces #635 A~D A.Ichihime and Triangle B.Kana and Dragon Quest game C.Linova and Kingdom D.Xeni ...

最新文章

  1. linux创建ftp指定目录命令行,Linux-命令行建立FTP
  2. 主板uefi和传统引导方式区别_反吊膜与传统污水池加盖方式有什么区别
  3. DockerSwarm 微服务部署
  4. GAE、SAE与BAE的对比分析
  5. 《信息学奥赛一本通》 高精度加法。输入两个正整数,求它们的和。
  6. android飞翔的小鸟游戏素材包_开心消消乐×愤怒的小鸟:为开心而战
  7. 深度干货|云原生分布式数据库 PolarDB-X 的技术演进
  8. LeetCode —— 1554. 只有一个不同字符的字符串(Python)
  9. 2021年最新的Java面试题,精选100题,大厂必备
  10. 【移动GIS】室内导航算法设计
  11. Java—图书管理系统
  12. 【论文笔记】MOBA类游戏中的强化学习论文5篇
  13. CSDN Markdown 图片排版显示
  14. 阿里巴巴连接池mysql_阿里巴巴连接池(Druid)
  15. Linux7安装oracle11g报错 Error in invoking target ‘agent nmhs‘ of makefile “ins_emagent.mk”
  16. leetcode 812. Largest Triangle Area(python)
  17. 小红书 x DorisDB:实现数据服务平台统一化,简化数据链路,提升高并发极速查询能力...
  18. 学微积,用手机,人人“知识共享”学真知
  19. LeetCode刷题EASY篇Linked List Cycle
  20. Spring Data JPA-动态查询

热门文章

  1. python画图配色_python语言,文章绘图配色高级又简单!
  2. 英科学家用扫描技术成功读取大脑记忆
  3. 实战:HPA(Pod 水平自动伸缩)-2021.11.23
  4. 探索数藏未来 秦储数字藏品座谈会成功召开
  5. Winform UI界面设计例程——自定义comboBox
  6. 台式机安装Windows11正式版(跳过TPM)
  7. PHP + elasticsearch的使用方法,PHP使用ES的超级简单方法
  8. java map 需要回收吗_weakMap 和 map 的垃圾回收对比
  9. 为了探究妹纸对内衣的喜好,我爬了淘宝内衣店的数据!
  10. 毛毛雨 2007-11