QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#123516#6107. One PathSpaceJellyfishWA 30ms4092kbC++174.4kb2023-07-12 19:22:542023-07-12 19:22:54

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-12 19:22:54]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:4092kb
  • [2023-07-12 19:22:54]
  • 提交

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]];
                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: 100
Accepted
time: 1ms
memory: 3868kb

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: 3872kb

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: 0
Accepted
time: 3ms
memory: 3748kb

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:

ok 2001 numbers

Test #4:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

10 5
1 4 10
3 7 138
1 9 162
4 10 113
4 6 12
5 6 171
2 10 31
7 8 12
7 10 132

output:

566 769 781 781 781 781 

result:

ok 6 numbers

Test #5:

score: 0
Accepted
time: 1ms
memory: 3860kb

input:

2 0
1 2 340241821

output:

340241821 

result:

ok 1 number(s): "340241821"

Test #6:

score: 0
Accepted
time: 0ms
memory: 4092kb

input:

2000 0
450 1620 747893383
103 1602 466748018
746 1996 468886757
430 764 455748265
1201 1275 111041658
244 802 715844971
611 899 125646661
1105 1633 478144612
180 573 823370712
161 1018 67225549
1512 1915 538711443
829 1871 761057722
1297 1499 576790037
1492 1832 983172784
1486 1902 78076400
1206 121...

output:

83343544566 

result:

ok 1 number(s): "83343544566"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3640kb

input:

2 2000
1 2 128840350

output:

128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 ...

result:

ok 2001 numbers

Test #8:

score: -100
Wrong Answer
time: 30ms
memory: 3904kb

input:

1139 1252
115 643 947372561
358 529 121247876
22 177 680088711
70 692 912515870
602 1032 172209848
908 1064 871258945
1052 1085 268011860
88 405 978751838
65 913 486052321
75 496 113634888
654 841 834034656
209 409 142094069
674 700 147589677
828 885 666263686
486 685 480409259
111 839 151780996
634...

output:

59768151302 150369908057 217208170534 277646631598 335406361937 391281299014 438561212303 484839363207 521242482705 554270904479 586402637496 616540067590 643905495969 671057179864 697920067485 724029665783 749929737654 775717879810 798475111093 821149622570 842913318477 864395818542 885440909335 90...

result:

wrong answer 2nd numbers differ - expected: '90240365913', found: '150369908057'