# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
90157 | mirbek01 | 바이오칩 (IZhO12_biochips) | C++17 | 2061 ms | 12508 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
int n, m, rt, a[N], tin[N], tout[N], timer, cur, pre;
long long dp[2][N], x[N];
vector <int> g[N];
void dfs1(int v){
tin[v] = ++ timer;
for(int to : g[v]){
dfs1(to);
}
tout[v] = timer;
}
void dfs(int v){
for(int to : g[v]){
dfs(to);
}
dp[cur][tout[v]] = max(dp[cur][tout[v]], dp[pre][tin[v] - 1] + x[v]);
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i ++){
int p;
cin >> p >> x[i];
if(!p)
rt = i;
else {
g[p].push_back(i);
}
}
dfs1(rt);
for(int i = 1; i <= n; i ++){
dp[1][tout[i]] = max(x[i], dp[1][tout[i]]);
}
for(int i = 2; i <= m; i ++){
cur = i % 2;
pre = cur ^ 1;
for(int j = 1; j <= n; j ++)
dp[pre][j] = max(dp[pre][j - 1], dp[pre][j]);
for(int j = 0; j <= n + 1; j ++)
dp[cur][j] = -1e18;
dfs(rt);
}
long long ans = 0;
for(int i = 1; i <= n; i ++)
ans = max(ans, dp[m % 2][i]);
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |