Repository navigation
Expand file tree
/
Copy pathbiochips.cpp
More file actions
70 lines (58 loc) · 1.88 KB
/
Copy pathbiochips.cpp
File metadata and controls
70 lines (58 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
Problem description :
given a tree with numbers written on nodes choose exactly m nodes
such that no two chosen nodes(u ,v) and u is ancestor of v.
maximize sum of values of chosen nodes.
we can represent answer of a subtree as vector where element at index i[0,M] represent the
maximum answer of choosing exactly i nodes.
ex : answer[1] = maximum node in the subtree
we can see that the answer for any node depends on the answers of its children subtrees
so we want to merge the answers of children to get the best answer for the current subtree.
now it's a knapsack problem we have some items with weights(represented by index) and values(elements)
and we want to maximize the vaues at each index of the new answer vector.
implemetation details :
in function knap we merge two vectors into new vector where the value of k-th item is max of all i,j
where i+j = k and a[i]+b[j] is max
In worst case this function works in O(M^2) which can only happen at most N/M = (N/M)*M^2 = NM
on average it works in O(M) which gives us complexity N*M
time complexity : O(NM)
*/
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 200005
#define MAX_M 502
int N ,M;
int A ,B;
int Mem[MAX_N];
vector <int> Adj[MAX_N];
vector <int> knap(vector<int>&a ,vector<int> b)
{
vector <int> c(min(M+1 ,(int)(a.size()+b.size())) ,0);
for(int i=0; i<a.size(); i++)
for(int j=0; j<b.size() && i+j<=M; j++)
c[i+j] = max(c[i+j] ,a[i]+b[j]);
return c;
}
vector <int> Solve(int v)
{
vector <int> ans = {0 ,0};
for(int i : Adj[v])
ans = knap(ans ,Solve(i));
ans[1] = max(ans[1] ,Mem[v]);
return ans;
}
int main()
{
scanf("%d%d",&N,&M);
int root;
for(int i=1; i<=N; i++)
{
scanf("%d%d",&A,&B);
Mem[i] = B;
if(A == 0)
root = i;
else
Adj[A].push_back(i);
}
printf("%d\n",Solve(root)[M]);
}