cf77C(树形DP)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qkoqhh/article/details/82710523

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

考虑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

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/82710523
今日推荐