# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
992582 | tch1cherin | 바이오칩 (IZhO12_biochips) | C++17 | 496 ms | 395600 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000;
vector<int> graph[MAX_N];
int W[MAX_N];
int tin[MAX_N], tout[MAX_N], pos[MAX_N];
int cur = 0;
void dfs(int u) {
tin[u] = cur;
pos[cur++] = u;
for (int v : graph[u]) {
dfs(v);
}
tout[u] = cur;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
int root = -1;
for (int i = 0; i < N; i++) {
int p;
cin >> p >> W[i];
p--;
if (p != -1) {
graph[p].push_back(i);
} else {
root = i;
}
}
dfs(root);
vector<vector<int>> dp(N + 1, vector<int>(M + 1, -1e9));
for (int i = 0; i <= N; i++) {
dp[i][0] = 0;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
dp[tout[pos[i]]][j + 1] = max(dp[tout[pos[i]]][j + 1], dp[i][j] + W[pos[i]]);
}
for (int j = 0; j <= M; j++) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
}
}
cout << dp[N][M] << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |