Submission #320340

# Submission time Handle Problem Language Result Execution time Memory
320340 2020-11-08T10:33:52 Z prarie Biochips (IZhO12_biochips) C++14
0 / 100
420 ms 411108 KB
#include <bits/stdc++.h>
 
#define fastio ios_base::sync_with_stdio(0), cin.tie(0)
#define fi first
#define se second
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define compress(v) sort(all(v)), (v).erase(unique(all((v))), v.end())

using namespace std;
 
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;

const int N = 2e5 + 5;
const int M = 500 + 5;

// INFO
int n, m;
int root;
int a[N];
vector<int> adj[N];

// DFS
int out[N], cnt1 = 1;
int invout[N];
int arr[N], cnt2 = 1;
int freq[N], match[N];

// DP
int dp[N][M];

void dfs(int cur, int pv) {
	arr[cnt2++] = cur;
	for (auto &next : adj[cur]) {
		if (next == pv) continue;
		dfs(next, cur);
	}
	arr[cnt2++] = cur;
	out[cnt1] = cur;
	invout[cur] = cnt1;
	cnt1++;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		int u;
		cin >> u >> a[i];
		if (u == 0) root = i;
		adj[i].push_back(u);
		adj[u].push_back(i);
	}
	dfs(root, 0);
	for (int i = 1, p = 0; i <= 2 * n; i++) {
		freq[arr[i]]++;
		if (freq[arr[i]] == 2) p = arr[i];
		else match[arr[i]] = p;
	}
	memset(dp, -1, sizeof dp);
	for (int i = 0; i <= n; i++) dp[i][0] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			dp[i][j] = dp[i - 1][j];
			int node = match[out[i]];
			int idx = invout[node];
			if (dp[idx][j - 1] != -1) {
				dp[i][j] = max(dp[i][j], dp[idx][j - 1] + a[out[i]]);
			}
		}
	}
	cout << dp[n][m] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 199 ms 400384 KB Output is correct
2 Correct 187 ms 400356 KB Output is correct
3 Correct 195 ms 400356 KB Output is correct
4 Correct 208 ms 400960 KB Output is correct
5 Correct 201 ms 400996 KB Output is correct
6 Correct 212 ms 401124 KB Output is correct
7 Incorrect 420 ms 411108 KB Output isn't correct
8 Halted 0 ms 0 KB -