QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#123517 | #6107. One Path | SpaceJellyfish | WA | 1ms | 3828kb | C++17 | 4.4kb | 2023-07-12 19:27:42 | 2023-07-12 19:27:43 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <array>
#include <iostream>
using namespace std;
const int N = 2001;
int n, k, u[N], v[N], w[N], head[N], nxt[N << 1], to[N << 1], val[N << 1], eid = 2;
bool del[N];
void addedge(int u, int v, int w) {
to[eid] = v;
val[eid] = w;
nxt[eid] = head[u];
head[u] = eid++;
}
struct info{
int u;
long long val;
info(){}
info(int u, long long val) : u(u), val(val){}
};
typedef array<info, 3> tupl;
int fa[N];
long long ansup[N], ansdn[N];
void getfa(int i){
// printf("fa[%d] = %d\n", i, to[fa[i]]);
for (int e = head[i]; e; e = nxt[e])
if(e != fa[i]) {
fa[to[e]] = e ^ 1;
getfa(to[e]);
}
}
tupl uptu[N], dntu[N], upson[N];
void merge(tupl &tu, info inf) {
if(inf.val >= tu[0].val) {
tu[2] = tu[1];
tu[1] = tu[0];
tu[0] = inf;
}
else if(inf.val >= tu[1].val) {
tu[2] = tu[1];
tu[1] = inf;
}
else if(inf.val >= tu[2].val)
tu[2] = inf;
}
void up(int i) {
ansup[i] = 0;
uptu[i] = tupl();
upson[i] = tupl();
for (int e = head[i]; e; e = nxt[e])
if(e != fa[i] && !del[e >> 1]) {
up(to[e]);
ansup[i] = max(ansup[i], ansup[to[e]]);
info inf = uptu[to[e]][0];
inf.u = to[e];
inf.val += val[e];
merge(uptu[i], inf);
merge(upson[i], info(to[e], ansup[to[e]]));
}
ansup[i] = max(ansup[i], uptu[i][0].val + uptu[i][1].val);
// printf("ansup[%d] = %d\n", i, ansup[i]);
}
void down(int i) {
ansdn[i] = 0;
dntu[i] = uptu[i];
if(fa[i] && !del[fa[i] >> 1]) {
int f = to[fa[i]];
info inf = dntu[f][0].u != i ? dntu[f][0] : dntu[f][1];
inf.val += val[fa[i]];
merge(dntu[i], inf);
ansdn[i] = max(ansdn[f], upson[f][0].u != i ? upson[f][0].val : upson[f][1].val);
if(dntu[f][0].u == i)
ansdn[i] = max(ansdn[i], dntu[f][1].val + dntu[f][2].val);
else if(dntu[f][1].u == i)
ansdn[i] = max(ansdn[i], dntu[f][0].val + dntu[f][2].val);
else
ansdn[i] = max(ansdn[i], dntu[f][0].val + dntu[f][1].val);
// ansdn[i] = max(ansdn[i], val[fa[i]] + (dntu[f][0].u != i ? dntu[f][0].val : dntu[f][1].val));
// printf("ansdn[%d] = %d + %d\n", i, val[fa[i]], dntu[f][0].u != i ? dntu[f][0].val : dntu[f][1].val);
}
for (int e = head[i]; e; e = nxt[e])
if(e != fa[i] && !del[e >> 1])
down(to[e]);
}
int findroot(int i){
while(fa[i] && !del[fa[i] >> 1])
i = to[fa[i]];
return i;
}
void solve(int i) {
up(i);
down(i);
}
int ans[N];
void getans(int u) {
for (int e = head[u]; e; e = nxt[e])
if(e != fa[u] && !del[e >> 1]) {
ans[to[e]] = ans[u];
getans(to[e]);
}
}
int main(){
scanf("%d%d", &n, &k);
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &u[i], &v[i], &w[i]);
addedge(u[i], v[i], w[i]);
addedge(v[i], u[i], w[i]);
}
getfa(1);
for (int i = 1; i < n; i++)
if(to[fa[v[i]]] == u[i])
swap(u[i], v[i]);
solve(1);
long long now = ansup[1];
printf("%lld ", now);
for (int i = 1; i <= k; i++) {
memset(ans + 1, -1, sizeof(int) * n);
for (int j = 1; j <= n; j++) {
if(ans[j] == -1) {
// printf("init %d\n", j);
int u = findroot(j);
solve(u);
ans[u] = ansup[u];
getans(u);
// puts("init success");
}
}
int candi = 1;
long long candians = 0;
for (int j = 1; j < n; j++)
if(!del[j]) {
// printf("j %d ansup[%d] %lld ansdn[%d] %lld\n", j, u[j], ansup[u[j]], u[j], ansdn[u[j]]);
// printf("j %d u %d v %d\n", j, u[j], v[j]);
long long nowans = ansup[u[j]] + ansdn[u[j]] + w[j] - ans[u[j]];
printf("%d %d ", u[j], v[j]);
if(nowans > candians) {
candians = nowans;
candi = j;
}
}
// printf("candi %d %d %d, candians %lld\n", candi, u[candi], v[candi], candians);
now += candians;
del[candi] = true;
printf("%lld ", now);
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3828kb
input:
5 1 1 3 2 4 5 4 3 4 3 2 3 7
output:
14 3 1 5 4 4 3 2 3 16
result:
wrong answer 2nd numbers differ - expected: '16', found: '3'