"Codevs1421" "The Tree dp" "Game theory" Autumn static leaf & Autumn-rang son

Source: Internet
Author: User

The Oriental Fantasy Township series of simulation game th04 problem.
It's not so hard ...

Title Description Description

In Gensokyo, Autumn Sisters are in charge of the fall of the gods, as the autumn leaves of the God's sister Jingyu and as a harvest God's sister, the son of Joi. If you associate the red leaves with the fruit, you will naturally think of baked sweet potatoes. Baked sweet potatoes need a lot of leaves, to the sweet potato baked very fragrant, so autumn sisters decided to compare who can collect the most red leaves. Jingyu the autumn leaves into n heaps (number 1). N), and specifies the order in which they are selected, just forming a direction tree. In the course of the game, the two people from the root node, taking turns to take the red leaves, when a person takes the leaves of the node I, the other person can only select from the node I son node. When a leaf is taken, the game is over, and the two will compare the number of leaves they get. It is known that the two people adopt a different strategy, the static leaf to consider in the case of the child to get as little as possible under the premise of their own to take the most; In the case of both of you taking the best strategy, please calculate the number of leaves for the two at the end of the game.
The game always leaves first, ensuring that only one set of solutions exists.
Enter a description input Description

Line 1th: 1 positive integer n, which indicates the number of leaves in a heap
Line 2nd: n integers, number I indicates the amount of red leaves in heap num[i]
3rd.. N+1:2 positive integer u,v, indicating node u as the father of Node V
Outputs description Output Description

Line 1th: 2 integers representing the number of leaves taken by the static leaf and the number of leaves taken by the rang child.
Sample input to sample

6
4 16 16 5 3 1
1 2
2 4
1 3
3 5
3 6
Sample output Sample Outputs

7 16
Data Size & Hint

Data range
For 30% data: 1≤n≤100,1≤num[i]≤100
For 60% data: 1≤n≤10,000,1≤num[i]≤10,000
For 100% data: 1≤n≤100,000,1≤num[i]≤10,000
Tips
Sample explanation:
First of all, the static leaf must be able to obtain 1 of the 4 leaves of the node, the son of the child is the node 2 and 3, are 16 leaves.
If the node 2 is selected, the next time the static leaves can be up to 5 pieces of red leaves, and the choice of 3 static leaves can only get 3 pieces of red leaves,
So at this time, the rang son will choose node 3, so the last leaves of the static leaf number is 7, the rang of 16.

Attention:
The number of leaves guaranteed by the two men in [0, 2^31-1].

It's all clear in the notes.
From bottom to top DP
Huang Blog said to write a manual stack
It could be the game when the Windows evaluation system stack is relatively small
Anyway, I didn't write the manual stack, Codevs's A.

//ac Code by Creationaugust#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#define MAXINT 0x7fffffffusing namespace STD;intf[100010][2];//The optimal value of a subtrees tree in autumn or autumn-rang. DP process from bottom to top update//0 for Autumn leaves 1 for the autumn-rang sonintnum[100010];intdeep[100010];//Record the depth of each node in the tree//If the odd is the turn to autumn Juliet otherwise autumn-rang son electionintU,v;intfa[100010];intRootintNstructedge{Edge *next;intto;} line[100010],*prev[100010];intAnsvoidDfsintx) {intmaxn=0, minn=maxint,t=0;if(deep[x]%2==1)//Here is the autumn static leaf selection{ for(Edge *i=prev[x];i;i=i->next) {inta=i->to; deep[a]=deep[x]+1; DFS (a);if((f[a][0]&GT;MAXN) | | (f[a][0]==maxn&&f[a][1]<minn))//In this case, the most of the autumn-rang child{maxn=f[a][0]; minn=f[a][1];            T=a; }//Find the optimal node T} f[x][0]=f[t][1]+NUM[X]; f[x][1]=f[t][0]; }Else//Here is the autumn static leaf selection{ for(Edge *i=prev[x];i;i=i->next) {inta=i->to; deep[a]=deep[x]+1; DFS (a);if((f[a][1]<minn) | | (f[a][1]==minn&&f[a][0]&GT;MAXN)) {maxn=f[a][0]; minn=f[a][1];            T=a; }} f[x][0]=f[t][1]+NUM[X]; f[x][1]=f[t][0]; }}intMain () {scanf("%d", &n); for(intI=1; i<=n;i++)scanf("%d", &num[i]); for(intI=1; i<n;i++) {scanf("%d%d", &u,&v);        Line[i].to=v;        Line[i].next=prev[u];        prev[u]=&line[i];    Fa[v]=u; } for(intI=1; i<=n;i++)if(!fa[i]) {root=i; Break; } deep[root]=1; DFS (root);printf("%d%d", f[root][0],f[root][1]);}

"Codevs1421" "The Tree dp" "Game theory" Autumn static leaf & Autumn-rang son

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.