CF414D Mashmokh and Water Tanks

CF414D Mashmokh and Water Tanks

Luo Gu evaluation portal

Title Description

Mashmokh is playing a new game. In the beginning he has kk liters of water and pp coins. Additionally he has a rooted tree (an undirected connected acyclic graph) that consists of mm vertices. Each vertex of the tree contains a water tank that is empty in the beginning.

The game begins with the fact that Mashmokh chooses some (no more than kk ) of these tanks (except the root) and pours into each of them exactly 11 liter of water. Then the following process is performed until there is no water remained in tanks.

  • The process consists of several steps.
  • At the beginning of each step Mashmokh opens doors of all tanks. Then Mashmokh closes doors of some tanks (he is not allowed to close door of tank in the root) for the duration of this move. Let's denote the number of liters in some tank with closed door as ww , Mashmokh pays ww coins for the closing of that tank during this move.
  • Let's denote by x_{1},x_{2},...,x_{m}x1,x2,...,*x**m* as the list of vertices of the tree sorted (nondecreasing) by their depth. The vertices from this list should be considered one by one in the order. Firstly vertex x_{1}x1 (which is the root itself) is emptied. Then for each vertex x_{i}*x**i* (i>1) , if its door is closed then skip the vertex else move all the water from the tank of vertex x_{i}*x**i* to the tank of its father (even if the tank of the father is closed).

Suppose ll moves were made until the tree became empty. Let's denote the amount of water inside the tank of the root after the ii -th move by w_{i}*w**i* then Mashmokh will win max(w_{1},w_{2},...,w_{l})max(w1,w2,...,*w**l*) dollars. Mashmokh wanted to know what is the maximum amount of dollars he can win by playing the above game. He asked you to find this value for him.

Input Format

The first line of the input contains three space-separated integers m,k,p (2<=m<=10^{5}; 0<=k,p<=10^{9})m,k,p (2<=m<=105; 0<=k,p<=109) .

Each of the following m-1m−1 lines contains two space-separated integers a_{i},b_{i} (1<=a_{i},b_{i}<=m; a_{i}≠b_{i})ai,bi (1<=ai,bi<=m; ai=bi) — the edges of the tree.

Consider that the vertices of the tree are numbered from 1 to mm . The root of the tree has number 1.

Output Format

Output a single integer, the number Mashmokh asked you to find.

Translation of the meaning of problems

Give you a tree, k liters of water, p dollars, once the game. Before the game, may be placed on any node 1 liter of water (a total of not more than k)
play several rounds, each round open to all the nodes, the node may select several closed, the cost for the nodes of the amount of water. Then the water is not closed all nodes (not continuously moved) to its father.

Finally, the root of the water is removed, the amount of water for profit round game.
Seeking the most profitable profit a round of the game.

Sample input and output

Input # 1 copy

Output # 1 copy

Input # 2 Copy

Output # 2 Copy

Description / Tips

The tree in the first sample is shown on the picture below. The black, red, blue colors correspond to vertices with 0, 1, 2 liters of water.

imgOne way to achieve the maximum amount of money is to put 1 liter of water in each of vertices 3 and 4. The beginning state is shown on the picture below.

imgThen in the first move Mashmokh will pay one token to close the door of the third vertex tank. The tree after the first move is shown on the picture below.

imgAfter the second move there are 2 liters of water in the root as shown on the picture below.

img

answer:

2019.11.13 analog breakout season T3 40

In the same session, the blowout chiefs @iamrjj , examination room burst cut black problem, simply Chinese OI pioneer, IOI future!

(Due to the exam and questions we face are not the same, I briefly summarize the meaning of problems :)

Subject to the effect

给一棵树,一开始\(k\)个人可以被放在树上除根节点之外的节点,但是每个节点只能放\(1\)个人。每一秒钟你可以往一些地方放路障拦住这里的所有人,花费的价值为当前点的人数。如果这个时刻这个点没有路障,这个点的所有人会向上爬一个点,这样的话,一些时刻会有人抵达根节点,现在需要你分配放路障的地方和时间,求所有时刻最多的抵达根节点的人数是多少。

题解:

在考场上连暴力都不会写。所以我仔细观察了一下样例,我发现,如果通过技术手段把人都拦在根节点的儿子节点上,直到所有人都到达根节点的儿子节点们,这个时候同时放开拦截,这些人就会同时抵达终点。这种方法就是最优秀的(刨除其他的一切条件)

所以我就乱搞(骗分),如果\(k<n\),输出\(k\),否则输出\(n-1\)。成功40分。

正经题解:

这回考虑其他的因素,我们发现最优的决策一定是越来越多的人都在到达根节点之前到达同一深度,同理,那么两个人一开始被放置的位置深度差越大,把一个人拦下使得另一个人追上他的难度(花费)就越大。所以我们要尽可能地让人们的初始深度差值最小。也就是说,我们要将其放在:按深度排序后深度相邻的点上

So we put these points in depth order of \ (LR \) . Suppose depth \ (depth \) there is at a point \ (cnt [depth] \) a person put. Release all nodes in a maximum depth of \ (D \) . So in order for them to achieve unity depth, need to pay the money:
\ [\ sum_ {i = L} ^ {i = r} {(D-Deep [i])} \]
then when \ (r \) increases when the cost is also increased. \ (l \) increases when the price will be smaller. To control this \ (LR \) range, of course, we selected two-hand method (emulated feet)

About two-pointer and scale borrowing, less likely if the students can look at this blog of Tacca:

Detailed ruler emulated

AC Code:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
int n,k,p;
int tot,head[maxn],nxt[maxn<<1],to[maxn<<1];
int deep[maxn],cnt[maxn];
void add(int x,int y)
{
    to[++tot]=y;
    nxt[tot]=head[x];
    head[x]=tot;
}
void dfs(int x,int f)
{
    deep[x]=deep[f]+1;
    cnt[deep[x]]++;
    for(int i=head[x];i;i=nxt[i])
    {
        int y=to[i];
        if(y==f)
            continue;
        dfs(y,x);
    }
}
int main()
{
    scanf("%d%d%d",&n,&k,&p);
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dfs(1,0);
    sort(deep+1,deep+n+1);
    int l=2,r=2,ans=1,tmp=0;
    while(1)
    {
        if(l>=n || r>=n)
            break;
        r++;
        if(deep[r]!=deep[r-1])
            tmp+=(r-l);
        while(tmp>p || r-l+1>k)
        {
            tmp-=(deep[r]-deep[l]);
            l++;
        }
        ans=max(ans,(r-l+1));
    }
    printf("%d",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11852367.html