博客
关于我
CF 1436D Bandit in a City 树上贪心
阅读量:622 次
发布时间:2019-03-14

本文共 1196 字,大约阅读时间需要 3 分钟。

文章目录

题意

给定一棵树形结构,每个点上都有一定的人数,人只能往叶子节点跑,根节点有一个土匪,问土匪最少能抓到多少人。

分析

我们只需要让所有的人均匀地跑到叶子节点就可以了,因此对于每个节点都有一个最优方案,也就是当前子树人数/当前子树叶子节点数+(人数%叶子节点 ? 1 : 0),最后取一个max就可以了,比较水的一道题。

AC Code

#include 
#define ACM_LOCAL#define fi first#define se second#define pb push_backusing namespace std;typedef long long ll;typedef pair
PII;const int N = 5e5 + 10, M = 5e5 + 10, INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;int n, m, k, cnt;ll sz[N], ans[N], a[N], res;vector
g[N];void dfs(int u) { ans[u] = a[u]; if (g[u].empty()) sz[u] = 1; for (auto v : g[u]) { dfs(v); sz[u] += sz[v]; ans[u] += ans[v]; }}void count(int u) { res = max(res, ans[u] / sz[u] + (ans[u] % sz[u] ? 1 : 0)); for (auto v : g[u]) { count(v); }}void solve() { int n; cin >> n; for (int i = 2; i <= n; i++) { int x; cin >> x; g[x].push_back(i); } for (int i = 1; i <= n; i++) cin >> a[i]; dfs(1); count(1); cout << res << endl;}int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);#ifdef ACM_LOCAL freopen("input", "r", stdin); freopen("output", "w", stdout);#endif solve(); return 0;}

转载地址:http://qlcoz.baihongyu.com/

你可能感兴趣的文章
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>