SYSU 6356 Dispatching

Dispatching

Time Limit: 3000ms
Memory Limit: 262144KB
This problem will be judged on SYSU. Original ID: 6356
64-bit integer IO format: %lld      Java class name: (No Java Yet)

In a sect of ninja, ninjas are dispatched to a client, and they are rewarded according to their work. In this sect, there is one ninja called the Master. Every ninja except the Master has one and only one boss. In order to preserve the confidentiality and to encourage leadership, any instructions concerning their work are always sent by a boss to his/her subordinates. It is forbidden to send instructions by other methods.
      You are gathering a number of ninjas and dispatch them to a client. You have to pay salaries to dispatched ninjas. For each ninja, the amount of salary for him/her is fixed. The total amount of salaries paid to them should be within a budget. Moreover, in order to send instructions, you have to choose a ninja as a manager who can send instructions to all dispatched ninjas. When instructions are sent, a ninja who is not dispatched may mediate the transmission. The manager may or may not be dispatched. If the manager is not dispatched, he will not be paid.
      You would like to maximize the satisfaction level of the client as much as possible within a budget. The satisfaction level of the client is calculated as the product of the total number of dispatched ninjas and the leadership level of the manager. For each ninja, his/her leadership level is fixed.

      Write a program that, given the boss Bi, the amount of salary Ci, the leadership level Li of each ninja i (1 <= i <= N), and the budget for salaries M, outputs the maximum value of the satisfaction level of the client when the manager and dispatched ninjas are chosen so that all the conditions are fulfilled.

 

Input

The first line of input contains two space separated integers N(1<=N<=100000), M(1<=M<=1000000000), where N is the number of ninjas and M is the budget. The following N lines describe the boss, salary, leadership level of each ninja. The (i + 1)-th line contains three space separated integers Bi(0<=Bi<i),Ci(1<=Ci<=M), Li(1<=Li<=1000000000), describing that the boss of ninja i is ninja Bi, the amount of his/her salary is Ci, and his/her leadership level is Li. The ninja i is the Master if Bi = 0. Since the inequality Bi < i is always satisfied, for each ninja, the number of his/her boss is always smaller than the number of himself/herself.

 

Output

Output the maximum value of the satisfaction level of the client.

 

Sample Input

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

Sample Output

6

Source

 
解题:左偏树啊。学了好久。。。
 
这道题目,就是用左偏树维护一些人,使得这些人的所付的薪水不超过M,如果超过了,那么删除薪水最大的,直到这些人的薪水和不超过M
 
等下试试 持久化的Treap
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 100005;
 5 int L[maxn],R[maxn],V[maxn],D[maxn],Le[maxn],C[maxn],cnt[maxn],n,m;
 6 LL sum[maxn],ret;
 7 vector<int>g[maxn];
 8 int Merge(int x,int y){
 9     if(!x || !y) return x|y;
10     if(V[x] < V[y]) swap(x,y);
11     R[x] = Merge(R[x],y);
12     if(D[L[x]] < D[R[x]]) swap(L[x],R[x]);
13     D[x] = D[R[x]] + 1;
14     sum[x] = sum[L[x]] + sum[R[x]] + C[x];
15     cnt[x] = cnt[L[x]] + cnt[R[x]] + 1;
16     return x;
17 }
18 int pop(int root){
19     int newRoot = Merge(L[root],R[root]);
20     L[root] = R[root] = D[root] = 0;
21     return newRoot;
22 }
23 int dfs(int u){
24     int root = u;
25     cnt[u] = 1;
26     V[u] = sum[u] = C[u];
27     for(int i = g[u].size()-1; i >= 0; --i)
28         root = Merge(root,dfs(g[u][i]));
29     while(sum[root] > m) root = pop(root);
30     ret = max(ret,(LL)cnt[root]*Le[u]);
31     return root;
32 }
33 int main(){
34     scanf("%d%d",&n,&m);
35     for(int i = 1,B; i <= n; ++i){
36         scanf("%d%d%d",&B,C+i,Le+i);
37         g[B].push_back(i);
38     }
39     dfs(1);
40     printf("%lld
",ret);
41     return 0;
42 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4522732.html