Codeforces Beta Round #69 (Div. 1 Only) C. Beavermuncher-0xFF 树上贪心

时间:2022-05-08 02:57:59

题目链接:

http://codeforces.com/problemset/problem/77/C

C. Beavermuncher-0xFF

time limit per test:3 secondsmemory limit per test:256 megabytes
#### 问题描述
> "Eat a beaver, save a tree!" — That will be the motto of ecologists' urgent meeting in Beaverley Hills.
>
> And the whole point is that the population of beavers on the Earth has reached incredible sizes! Each day their number increases in several times and they don't even realize how much their unhealthy obsession with trees harms the nature and the humankind. The amount of oxygen in the atmosphere has dropped to 17 per cent and, as the best minds of the world think, that is not the end.
>
> In the middle of the 50-s of the previous century a group of soviet scientists succeed in foreseeing the situation with beavers and worked out a secret technology to clean territory. The technology bears a mysterious title "Beavermuncher-0xFF". Now the fate of the planet lies on the fragile shoulders of a small group of people who has dedicated their lives to science.
>
> The prototype is ready, you now need to urgently carry out its experiments in practice.
>
> You are given a tree, completely occupied by beavers. A tree is a connected undirected graph without cycles. The tree consists of n vertices, the i-th vertex contains ki beavers.
>
> "Beavermuncher-0xFF" works by the following principle: being at some vertex u, it can go to the vertex v, if they are connected by an edge, and eat exactly one beaver located at the vertex v. It is impossible to move to the vertex v if there are no beavers left in v. "Beavermuncher-0xFF" cannot just stand at some vertex and eat beavers in it. "Beavermuncher-0xFF" must move without stops.
>
> Why does the "Beavermuncher-0xFF" works like this? Because the developers have not provided place for the battery in it and eating beavers is necessary for converting their mass into pure energy.
>
> It is guaranteed that the beavers will be shocked by what is happening, which is why they will not be able to move from a vertex of the tree to another one. As for the "Beavermuncher-0xFF", it can move along each edge in both directions while conditions described above are fulfilled.
>
> The root of the tree is located at the vertex s. This means that the "Beavermuncher-0xFF" begins its mission at the vertex s and it must return there at the end of experiment, because no one is going to take it down from a high place.
>
> Determine the maximum number of beavers "Beavermuncher-0xFF" can eat and return to the starting vertex.
#### 输入
> The first line contains integer n — the number of vertices in the tree (1 ≤ n ≤ 105). The second line contains n integers ki (1 ≤ ki ≤ 105) — amounts of beavers on corresponding vertices. Following n - 1 lines describe the tree. Each line contains two integers separated by space. These integers represent two vertices connected by an edge. Vertices are numbered from 1 to n. The last line contains integer s — the number of the starting vertex (1 ≤ s ≤ n).
#### 输出
> Print the maximum number of beavers munched by the "Beavermuncher-0xFF".
>
> Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).
#### 样例
> **sample input**
> 5
> 1 3 1 3 2
> 2 5
> 3 4
> 4 5
> 1 5
> 4
>
> **sample output**
> 6

题意

给你一颗树,树上每个点有ai个小球,现在你有一个机器人位于根节点,这个机器人每走到一个节点,就会吃掉这个节点上的一个小球(初始位置一开始不会吃),如果一个位置没有小球,这个机器人是过不去的,而且这个机器人不会停下来,现在问你从根节点开始吃,最后要求能回到根节点,能吃到的最多小球。

题解

贪心:

策略1:从u进入一个子节点v的时候,优先考虑吃掉v的子树,再考虑u,v之间来回吃。

策略2:对于u的所有子节点vi,定义sumv[vi]为吃掉最多的以vi为根的子树的球(并且回到vi)。那么我们对于u,优先考虑先吃sumv[vi]比较大的子树。

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=1e5+10;
typedef __int64 LL; //cntv[v]:表示v节点的小球个数
//sumv[v]:表示吃完以v为根的子树并回到v能吃到的最多小球。
//remv[v]:表示吃完以v为根的子树并回到v能吃到的最多小球之后,v这个节点还剩下的小球个数。
LL cntv[maxn],sumv[maxn],remv[maxn];
int n,rt; void init(){
memset(remv,0,sizeof(remv));
memset(sumv,0,sizeof(sumv));
} vector<int> G[maxn];
void dfs(int u,int fa){
if(G[u].size()==1&&fa!=-1){
remv[u]=cntv[u];
sumv[u]=0;
}else{
priority_queue<LL> pq;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
dfs(v,u);
pq.push(sumv[v]);
}
if(fa!=-1){
sumv[u]++; cntv[u]--;
}
while(cntv[u]&&!pq.empty()&&pq.top()){
LL t=pq.top(); pq.pop();
sumv[u]+=t;
cntv[u]--;
sumv[u]++;
}
for(int i=0;i<G[u].size()&&cntv[u];i++){
int v=G[u][i];
if(v==fa) continue;
if(remv[v]==0) continue;
if(cntv[u]>=remv[v]){
sumv[u]+=2*remv[v];
cntv[u]-=remv[v];
}else{
sumv[u]+=2*cntv[u];
cntv[u]=0;
}
}
remv[u]=cntv[u];
}
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&cntv[i]);
}
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
scanf("%d",&rt);
dfs(rt,-1);
printf("%I64d\n",sumv[rt]);
return 0;
} /*
5
1 1 1 1 1
2 5
3 4
4 5
1 5
4 6
3 3 1 2 3 5
2 6
1 6
4 5
5 1
3 4
5
*/

Codeforces Beta Round #69 (Div. 1 Only) C. Beavermuncher-0xFF 树上贪心的更多相关文章

  1. Codeforces Beta Round &num;69 &lpar;Div&period; 2 Only&rpar;

    Codeforces Beta Round #69 (Div. 2 Only) http://codeforces.com/contest/80 A #include<bits/stdc++.h ...

  2. Codeforces Beta Round &num;85 &lpar;Div&period; 1 Only&rpar; A&period; Petya and Inequiations 贪心

    A. Petya and Inequiations Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  3. Codeforces Beta Round &num;63 &lpar;Div&period; 2&rpar;

    Codeforces Beta Round #63 (Div. 2) http://codeforces.com/contest/69 A #include<bits/stdc++.h> ...

  4. Codeforces Beta Round &num;80 &lpar;Div&period; 2 Only&rpar;【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  5. Codeforces Beta Round &num;83 &lpar;Div&period; 1 Only&rpar;题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  6. Codeforces Beta Round &num;79 &lpar;Div&period; 2 Only&rpar;

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  7. Codeforces Beta Round &num;77 &lpar;Div&period; 2 Only&rpar;

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round &num;76 &lpar;Div&period; 2 Only&rpar;

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round &num;75 &lpar;Div&period; 2 Only&rpar;

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

随机推荐

  1. Zabbix监控Windows事件日志

    1.zabbix_agentd.win文件修改: LogFile=c:\zabbix\zabbix_agentd.log Server=1.16.2.4 ServerActive=1.16.2.4 H ...

  2. asp&period;net 操作excel的一系列问题(未完待续)

    最近在处理exel的一些东西,遇到了很多问题,现在就在此将问题和网上找到的解决办法 1.外部表不是预期格式错误 错误经过:在读取Excel时,出现外部表不是预期的格式 错误原因1: 由于Excel 9 ...

  3. How does a relational database work

    http://blog.jobbole.com/100349/ http://coding-geek.com/how-databases-work/

  4. jQuery 滚动动画简单版

    动画的思路很简单,点击页面上一个元素,页面滚动到指定位置.下面介绍一下我3个小时百度的研究成果: 首先是html部分: <html> <body> <a>顶部&lt ...

  5. &lbrack;字符串&rsqb; AppMessage--字符串返回帮助类 (转载)

    点击下载 AppMessage.rar 这个类是关于AppMessage的帮助类,主要是格式化返回字符串的里面会有很多的提供字符的格式以及预设置内容,大家可以直接使用的看下面代码吧 /// <s ...

  6. 再说Postgres中的高速缓存&lpar;cache&rpar;

    表的模式信息存放在系统表中,因此要访问表,就需要首先在系统表中取得表的模式信息.对于一个PostgreSQL系统来说,对于系统表和普通表模式的访问是非常频繁的.为了提高这些访问的效率,PostgreS ...

  7. SAP MM 明明已经扩展供应商到采购组织下,采购订单里还是报错?

    SAP MM 明明已经扩展供应商到采购组织下,采购订单里还是报错? 如下的PO 4400000069,处于HELD状态.ME22N试图保存它为一个正式的采购订单,报错说供应商没有在采购组织下维护, 使 ...

  8. 【译】1&period; Java反射——引言

    原文地址:http://tutorials.jenkov.com/java-reflection/index.html *By Jakob Jenkov Java的反射机制使得它可以在运行时检查类.接 ...

  9. 关于cc&period;easesinexxx 与 cc&period;easeexponentiallxxx 的几种效果简单描述

    代码样例: var biggerEase = cc.scaleBy(0.7,1.2,1.2).easing(cc.easeSineInOut()) 呈正弦变化 1)CCEaseSineIn       ...

  10. Java中的各种bean对应的意义&lpar;VO&comma;PO&comma;BO&comma;QO&comma; DAO&comma;POJO&comma;DTO&rpar;

    VO(value object) 值对象 通常用于业务层之间的数据传递,用 new 关键字创建,由 GC 回收的,和 PO 一样也是仅仅包含数据而已.但应是抽象出的业务对象 , 可以和表对应 , 也可 ...