답안 #913946

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
913946 2024-01-20T15:54:08 Z daoquanglinh2007 바이오칩 (IZhO12_biochips) C++17
90 / 100
239 ms 405332 KB
#include <bits/stdc++.h>
using namespace std;

const int NM = 2e5, MM = 500, inf = INT_MAX;

int N, M, root, p[NM+5], x[NM+5], timer = 0, tin[NM+5], tout[NM+5], tour[NM+5];
vector <int> son[NM+5];
int dp[NM+5][MM+5];

void dfs_euler_tour(int u){
	tin[u] = ++timer;
	tour[timer] = u;
	for (int v : son[u]){
		dfs_euler_tour(v);
	}
	tout[u] = timer;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin >> N >> M;
	for (int i = 1; i <= N; i++){
		cin >> p[i] >> x[i];
		if (p[i] == 0) root = i;
		son[p[i]].push_back(i);
	}
	dfs_euler_tour(root);
	for (int i = 1; i <= M; i++) dp[N+1][i] = -inf;
	for (int i = N; i >= 1; i--){
		int u = tour[i];
		for (int j = 1; j <= M; j++){
			dp[i][j] = max(dp[tout[u]+1][j-1]+x[u], dp[i+1][j]);
			dp[i][j] = max(dp[i][j], -inf);
		}
	}
	cout << max(dp[1][M], 0);
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Incorrect 2 ms 8540 KB Output isn't correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 7 ms 27324 KB Output is correct
5 Correct 8 ms 29532 KB Output is correct
6 Correct 9 ms 29532 KB Output is correct
7 Correct 185 ms 302376 KB Output is correct
8 Correct 161 ms 302252 KB Output is correct
9 Correct 211 ms 368108 KB Output is correct
10 Correct 239 ms 405332 KB Output is correct