【codeforces #300】EF题解

E. Demiurges Play Again

time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.

There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf.

Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.

Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers.

Input The first line contains a single integer n — the number of nodes in the tree (1≤n≤2·105).

Each of the next n-1 lines contains two integers ui and vi (1≤ui,vi≤n) — the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root is the node 1.

Output Print two space-separated integers — the maximum possible and the minimum possible result of the game.

Sample test(s) input 5 1 2 1 3 2 4 2 5 output 3 2 input 6 1 2 1 3 3 4 1 5 5 6 output 3 3 Note Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.

In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3.

dp

对于以为根的子树的叶子,我们只考虑他在这棵子树的叶子中的rank。

小的叶子上。

一.想让答案尽可能小: ①若当前是先手,他想让答案尽可能大,那么只要把所有儿子表示rank,,一个rank只能属于一个叶子,所以自然是相加了

②如果当前是后手,他想让答案尽可能小,那么答案就是

二.想让答案尽可能大: 把大。 ①先手让答案尽可能大,就是

②后手让答案尽可能小,就是相加

把两个合在一起写就好了。

;struct edge{int y,ne;}e[500005];int tot,h[200005],n;void Addedge(int x,int y){e[++tot].y=y;e[tot].ne=h[x];h[x]=tot;}int dfs(int x,int k){if (!h[x]) return 1;int t1=500000,t2=0;for (int i=h[x];i;i=e[i].ne)if (k) t2+=dfs(e[i].y,k^1);else t1=min(t1,dfs(e[i].y,k^1));if (k) return t2;return t1;}int main(){scanf(“%d”,&n);for (int i=1;i<n;i++){int x,y;scanf(“%d%d”,&x,&y);Addedge(x,y);}int l=0;for (int i=1;i<=n;i++)if (!h[i]) l++;cout<<l+1-dfs(1,0)<<” “<<dfs(1,1)<<endl;return 0;}F. A Heap of Heaps

time limit per test3 seconds memory limit per test512 megabytes inputstandard input outputstandard output Andrew skipped lessons on the subject ‘Algorithms and Data Structures’ for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment.

The teacher gave Andrew an array of n numbers a1, …, an. After that he asked Andrew for each k from 1 to n-1 to build a k-ary heap on the array and count the number of elements for which the property of the minimum-rooted heap is violated, i.e. the value of an element is less than the value of its parent.

为了一些琐事吵架,然后冷战,疯狂思念对方,最后和好。

【codeforces #300】EF题解

相关文章:

你感兴趣的文章:

标签云: