#include <bits/stdc++.h>
using namespace std ;
const int MAX = 2e5 + 10 , MAXM = 502 ;
int arr[MAX] , in[MAX] , out[MAX] , id[MAX] ;
int n , m ;
vector< vector<int> >adj(MAX) ;
int tim = 0 ;
int dp[MAX][MAXM] ;
void dfs(int node)
{
in[node] = ++tim ;
id[tim] = node ;
for(auto &child : adj[node])
dfs(child) ;
out[node] = tim ;
}
int solve(int idx , int cnt)
{
if(cnt == m)
return 0 ;
if(idx == n+1)
return -1e7 ;
int &ret = dp[idx][cnt] ;
if(ret != -1)
return ret ;
ret = solve(idx+1 , cnt) ;
ret = max(ret , solve(out[id[idx]]+1 , cnt+1) + arr[id[idx]]) ;
return ret ;
}
int main()
{
memset(dp , -1 , sizeof(dp)) ;
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
int root ;
for(int i = 1 ; i <= n ; ++i)
{
int par ;
cin>>par>>arr[i] ;
if(!par)
root = i ;
else
adj[par].push_back(i) ;
}
dfs(root) ;
return cout<<solve(1 , 0)<<"\n" , 0 ;
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:55:5: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
dfs(root) ;
~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
398060 KB |
Output is correct |
2 |
Correct |
196 ms |
398072 KB |
Output is correct |
3 |
Correct |
200 ms |
398072 KB |
Output is correct |
4 |
Correct |
209 ms |
398712 KB |
Output is correct |
5 |
Correct |
214 ms |
398664 KB |
Output is correct |
6 |
Correct |
225 ms |
398840 KB |
Output is correct |
7 |
Correct |
1135 ms |
407672 KB |
Output is correct |
8 |
Correct |
1133 ms |
407568 KB |
Output is correct |
9 |
Correct |
1342 ms |
408952 KB |
Output is correct |
10 |
Correct |
1773 ms |
410104 KB |
Output is correct |