QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#117988 | #6107. One Path | Alphaban | WA | 1ms | 3960kb | C++14 | 2.1kb | 2023-07-02 19:42:07 | 2023-07-02 19:42:08 |
Judging History
answer
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define LL long long
const int N = 2000;
using namespace std;
#define L(i, s, t) for(int i = s; i <= t; ++i)
#define R(i, t, s) for(int i = t; i >= s; --i)
void read(int &x) {
x = 0; int w = 1; char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) if (c == '-') w = -1;
for(; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + c - '0'; x *= w;
}
int n, k, h[N + 5], nxt[2 * N + 5], e[2 * N + 5], tot;
LL f[N + 5][N + 5][3]; int sz[N + 5];
LL g[N + 5][3], w[2 * N + 5];
void add(int x, int y, int ww) {
nxt[++tot] = h[x]; e[tot] = y; w[tot] = ww; h[x] = tot;
}
const LL inf = 1ll << 60;
void dfs(int x, int ff) {
// cou
sz[x] = 1; // f[x][0] 代表, f[x][1] 代表两个儿子, f[x][2] 代表已经成形
for(int i = h[x]; i; i = nxt[i]) {
int v = e[i];
if (v == ff) continue;
dfs(v, x);
for(int j = 0; j <= sz[x] + sz[v]; ++j)
{g[j][0] = g[j][1] = g[j][2] = -inf;}
for(int j = 0; j <= sz[x]; ++j)
for(int k = 0; k <= sz[v]; ++k) {
g[j + k + 1][0] = max(g[j + k + 1][0], f[x][j][0] + w[i] + f[v][k][2]);
g[j + k + 1][1] = max(g[j + k + 1][1], f[x][j][1] + w[i] + f[v][k][2]);
g[j + k + 1][2] = max(g[j + k + 1][2], f[x][j][2] + w[i] + f[v][k][2]);
g[j + k][0] = max(g[j + k][0], f[x][j][0] + f[v][k][0]);
g[j + k][1] = max(g[j + k][1], f[x][j][1] + f[v][k][0]);
g[j + k][1] = max(g[j + k][1], f[x][j][0] + w[i] + f[v][k][1]);
g[j + k][2] = max(g[j + k][2], f[x][j][1] + f[v][k][1] + w[i]);
g[j + k][2] = max(g[j + k][2], f[x][j][2] + f[v][k][0]);
g[j + k][2] = max(g[j + k][2], f[x][j][0] + f[v][k][2]);
}
sz[x] += sz[v];
for(int j = 0; j < sz[x]; ++j) {
f[x][j][0] = g[j][0];
f[x][j][1] = max(g[j][1], f[x][j][0]);
f[x][j][2] = max(g[j][2], f[x][j][1]);
}
}
}
int main() {
// freopen("t.in", "r", stdin);
read(n); read(k);
for(int i = 1; i < n; ++i) {
int x, y, w; read(x); read(y); read(w);
add(x, y, w); add(y, x, w);
}
dfs(1, 0);
for(int i = 0; i <= k; ++i)
printf("%lld ", f[1][i][2]);
return 0;
}
// dp u, sz, k
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3600kb
input:
5 1 1 3 2 4 5 4 3 4 3 2 3 7
output:
14 16
result:
ok 2 number(s): "14 16"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3844kb
input:
7 2 1 2 4 2 3 6 2 4 2 4 5 5 2 6 1 4 7 3
output:
13 20 21
result:
ok 3 number(s): "13 20 21"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3960kb
input:
50 2000 3 34 1 37 39 58720256 17 24 14680064 28 39 1 25 38 1 21 29 1 3 30 1 26 36 1 5 48 14336 4 22 1 26 41 1 41 44 1 5 14 1 23 25 28672 40 41 224 27 39 1 4 20 7340032 7 47 939524096 11 46 114688 30 49 3584 34 44 1 7 35 1 10 29 1 27 33 29360128 16 36 56 8 28 1 13 38 57344 34 45 896 15 35 469762048 1...
output:
1409286145 1761607683 1849688069 1871708167 1877213193 1878589451 1878933517 1879019535 1879041041 1879046419 1879047765 1879048103 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 187...
result:
wrong answer 51st numbers differ - expected: '1879048160', found: '0'