cf77C(树形DP)

这个和上次那个结论+状压的题挺像的。。

考虑2个点,如果2个点的点权都很大那么在这2个点之间来回跑这个决策是绝对不能放过的。。

可是会发现如果一开始来回跑会比较吃亏,亏的是一个子树,所以得先跑完子树在和儿子来回跑,那么又有一个问题,儿子和其子节点来回跑和自己跟儿子来回跑谁会更优。其实显然是前者,因为一个点的父亲只有一个而其儿子却可以有多个。。

所以最终方案就是,先跑完子树,再从下到上进行来回跑。。。当然这2个可以同时转移着做,具体看代码。。

 

 

 

 /**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 100005
#define nm 200005
#define pi 3.1415926535897931
const int inf=1e9;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 
 
 






struct edge{int t;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
int n,a[NM],_x,_y,f[NM],c[NM],tot;
bool v[NM];
ll d[NM];

bool cmp(int x,int y){return d[x]>d[y];}

void dfs(int x){
    v[x]++;
    link(x)if(!v[j->t]){
	f[j->t]=x;
	if(!a[j->t])continue;
	a[j->t]--;
	dfs(j->t);
    }
    tot=0;link(x)if(f[x]!=j->t)c[++tot]=j->t;
    sort(c+1,c+1+tot,cmp);
    inc(i,1,tot)if(a[x]&&v[c[i]])d[x]+=d[c[i]]+2,a[x]--;
    link(x)if(f[x]!=j->t)d[x]+=2*min(a[x],a[j->t]),a[x]-=min(a[x],a[j->t]);
}


int main(){
    n=read();
    inc(i,1,n)a[i]=read();
    inc(i,1,n-1){_x=read();_y=read();add(_x,_y);add(_y,_x);}
    _x=read();
    dfs(_x);
    return 0*printf("%lld\n",d[_x]);
}

 

 

 

 

 

C. Beavermuncher-0xFF

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

"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.

Input

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).

Output

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).

Examples

Input

Copy

5
1 3 1 3 2
2 5
3 4
4 5
1 5
4

Output

Copy

6

Input

Copy

3
2 1 1
3 2
1 2
3

Output

Copy

2
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
H3C CF8800是一款高性能的企业级交换机设备,用户手册是为了帮助用户正确配置和使用这款设备而编写的详细指南。 H3C CF8800用户手册主要包括以下内容: 1. 产品介绍:介绍了CF8800交换机的主要特点和硬件结构,包括端口配置、指示灯功能等。用户可以通过阅读这部分内容了解设备的基本情况。 2. 硬件安装:详细介绍了CF8800的安装步骤,包括机柜安装、电源连接、网线连接、风扇插入等。用户可以按照手册中给出的指引一步步完成设备的安装。 3. 系统配置:介绍了CF8800的系统配置方法,包括设备开机、登录认证、管理口配置等。用户可以根据手册中的说明设置设备的基本参数。 4. VLAN配置:详细介绍了CF8800的VLAN配置方法,包括新建VLAN、端口划分、端口模式设置等。用户可以根据手册中的指引配置设备的VLAN。 5. 路由配置:介绍了CF8800的路由配置方法,包括静态路由、动态路由、路由汇总等。用户可以按照手册中给出的步骤配置设备的路由。 6. 安全配置:详细介绍了CF8800的安全配置方法,包括访问控制列表(ACL)、端口安全、防护组等。用户可以根据手册中的说明加强设备的安全性。 总之,H3C CF8800用户手册是一本重要的工具,可以帮助用户快速熟悉设备的各项功能和配置方法。用户在使用CF8800交换机时,可以随时参考手册来解决设备配置和使用过程中的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值