답안 #92448

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
92448 2019-01-02T22:38:00 Z luciocf 바이오칩 (IZhO12_biochips) C++14
100 / 100
546 ms 420940 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int maxn = 2e5+10;
 
const int maxm = 5e2+10;
 
const int inf = 1e9+10;
 
int n, m, root;
 
int num[maxn], l;
 
int dp[maxn][maxm], lft[maxn], rgt[maxn];
 
vector<int> grafo[maxn], rr[maxn];
 
void dfs(int u, int p)
{
	if (grafo[u].size() == 1 && u != root)
	{
		lft[u] = rgt[u] = ++l;
		rr[l].push_back(u);
		return;
	}
 
	for (auto v: grafo[u])
		if (v != p)
			dfs(v, u);

	lft[u] = inf;
	for (auto v: grafo[u])
	{
		if (v == p) continue;
		lft[u] = min(lft[u], lft[v]);
		rgt[u] = max(rgt[u], rgt[v]);
	}

	rr[rgt[u]].push_back(u);
}
 
int main(void)
{
	ios::sync_with_stdio(false); cin.tie(0);
 
	cin >> n >> m;
 
	for (int i = 1; i <= n; i++)
	{
		int p;
		cin >> p >> num[i];
 
		if (p)
		{
			grafo[i].push_back(p);
			grafo[p].push_back(i);
		}
 
		if (!p) root = i;
	}
 
	dfs(root, 0);
 
	for (int i = 0; i <= l; i++)
	{
		dp[i][0] = 0;
		for (int j = 1; j <= m; j++)
			dp[i][j] = -inf;
	}

	for (int i = 1; i <= l; i++)
	{
		for (auto u: rr[i])
			for (int j = 1; j <= m; j++)
				dp[i][j] = max({dp[i][j], dp[i-1][j], num[u]+dp[lft[u]-1][j-1]});

		if (rr[i].size() == 0)
			for (int j = 1; j <= m; j++)
				dp[i][j] = dp[i-1][j];
	}

	int ans = 0;
	for (int i = 1; i <= n; i++)
		ans = max(ans, dp[i][m]);
	cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9772 KB Output is correct
2 Correct 10 ms 9720 KB Output is correct
3 Correct 10 ms 9976 KB Output is correct
4 Correct 24 ms 19192 KB Output is correct
5 Correct 28 ms 23800 KB Output is correct
6 Correct 29 ms 23672 KB Output is correct
7 Correct 389 ms 315128 KB Output is correct
8 Correct 450 ms 315128 KB Output is correct
9 Correct 511 ms 381568 KB Output is correct
10 Correct 546 ms 420940 KB Output is correct