답안 #320341

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
320341 2020-11-08T10:35:34 Z prarie 바이오칩 (IZhO12_biochips) C++14
0 / 100
365 ms 409848 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() {
	fastio;
	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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 187 ms 400356 KB Output is correct
2 Correct 186 ms 400356 KB Output is correct
3 Correct 186 ms 400356 KB Output is correct
4 Correct 192 ms 400868 KB Output is correct
5 Correct 193 ms 400988 KB Output is correct
6 Correct 193 ms 400996 KB Output is correct
7 Incorrect 365 ms 409848 KB Output isn't correct
8 Halted 0 ms 0 KB -