QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#123491#6107. One PathverjunWA 9ms3664kbC++144.1kb2023-07-12 17:37:582023-07-12 17:38:00

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 17:38:00]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:3664kb
  • [2023-07-12 17:37:58]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int Read() {
    int x = 0, f = 1; char ch = getchar();
    while(!isdigit(ch) && ch != '-')  ch = getchar();
    if(ch == '-')  f = -1, ch = getchar();
    while(isdigit(ch))  x = x * 10 + ch - '0', ch = getchar();
    return x * f;  
}
int first[5005], nxt[5005], to[5005], vis[5005], w[5005], tot = 1, dd[5005];
void Add(int x, int y, int z) {
    nxt[++tot] = first[x]; first[x] = tot; to[tot] = y, w[tot] = z, dd[tot] = x;
    nxt[++tot] = first[y]; first[y] = tot; to[tot] = x, w[tot] = z, dd[tot] = y;
}
int n, k, mx[2005][4], mpos[2005][4], chans[2005][3], chmpos[2005][3], ans[2005], zj[2005];
void upd(int u, int val, int pos) {
    // cout << u << " " << pos << endl;
    if(mx[u][1] < val || mpos[u][1] == -1)
        mx[u][3] = mx[u][2], mpos[u][3] = mpos[u][2], 
        mx[u][2] = mx[u][1], mpos[u][2] = mpos[u][1], 
        mx[u][1] = val, mpos[u][1] = pos;
    else if(mx[u][2] < val || mpos[u][2] == -1)
        mx[u][3] = mx[u][2], mpos[u][3] = mpos[u][2],
        mx[u][2] = val, mpos[u][2] = pos;
    else if(mx[u][3] < val || mpos[u][3] == -1)
        mx[u][3] = val, mpos[u][3] = pos;
}
void updans(int u, int val, int pos) {
    if(chans[u][1] < val || chmpos[u][1] == -1)
        chans[u][2] = chans[u][1], chmpos[u][2] = chmpos[u][1], 
        chans[u][1] = val, chmpos[u][1] = pos;
    else if(chans[u][2] < val || chmpos[u][2] == -1)
        chans[u][2] = val, chmpos[u][2] = pos;
}
void dfs(int u, int fa) {
    for(int e = first[u]; e; e = nxt[e]) {
        int v = to[e];
        if(v == fa || vis[e])  continue ;
        dfs(v, u);
        if(mpos[v][1] == -1)  upd(u, w[e], v);
        else  upd(u, mx[v][1] + w[e], v);
        updans(u, ans[v], v);
    }
    ans[u] = max(chans[u][1], mx[u][1] + mx[u][2]);
}
int mxedg = -1, mxval;
void dfs2(int u, int fa, int ww) {
    if(u != fa) {
        int s1 = ans[u];
        int chansf = (chmpos[fa][1] == u) ? chans[fa][2] : chans[fa][1];
        int num = 0, mxf = 0;
        for(int i = 1; i <= 3; i++) {
            if(mpos[fa][i] != u && num < 2)
                ++num, mxf += mx[fa][i];
        }
        int s2 = max(mxf, chansf);
        if(s2 + s1 + w[ww] > mxval)
            mxval = s2 + s1 + w[ww], mxedg = ww, zj[u] = s1, zj[fa] = s2;
        int s3 = (mpos[fa][1] == u) ? mx[fa][2] + w[ww] : mx[fa][1] + w[ww];
        upd(u, s3, fa); updans(u, s2, fa);
        // cout << u << " " << s1 << " " << s2 << " " << s3 << " " << w[ww] << endl;
    }
    for(int e = first[u]; e; e = nxt[e]) {
        int v = to[e];
        if(vis[e] || v == fa)  continue ;
        dfs2(v, u, e);
    }
}
signed main() {
    n = Read(), k = Read();
    for(int i = 1; i < n; i++) {
        int x = Read(), y = Read(), z = Read();
        Add(x, y, z);
    }
    memset(mpos, -1, sizeof(mpos));
    memset(chmpos, -1, sizeof(chmpos));
    dfs(1, 0);
    int Ans = ans[1];
    cout << Ans << " ";
    priority_queue<pair<int, int> > q;
    dfs2(1, 1, 0);
    q.push(make_pair(mxval - Ans, mxedg));
    for(int i = 1; i <= k; i++) {
        pair<int, int> u = q.top(); q.pop();
        Ans = Ans + u.first;
        cout << Ans << " ";
        int ww = u.second;
        vis[ww] = vis[ww ^ 1] = 1;
        int z1 = zj[dd[ww]], z2 = zj[dd[ww ^ 1]];
//        cout << dd[ww] << " " << dd[ww ^ 1] << endl;
        memset(mpos, -1, sizeof(mpos));
        memset(chmpos, -1, sizeof(chmpos));
        memset(mx, 0, sizeof(mx));
        memset(chans, 0, sizeof(chans));
        memset(ans, 0, sizeof(ans));
        mxedg = -1, mxval = 0;
        dfs(dd[ww], 0);
        dfs2(dd[ww], dd[ww], 0);
        if(mxedg != -1)  q.push(make_pair(mxval - z1, mxedg));
        ww ^= 1;
        memset(mpos, -1, sizeof(mpos));
        memset(chmpos, -1, sizeof(chmpos));
        memset(mx, 0, sizeof(mx));
        memset(chans, 0, sizeof(chans));
        memset(ans, 0, sizeof(ans));
        mxedg = -1, mxval = 0;
        dfs(dd[ww], 0);
        dfs2(dd[ww], dd[ww], 0);
        if(mxedg != -1)  q.push(make_pair(mxval - z2, mxedg));
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3516kb

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

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: 9ms
memory: 3592kb

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: 0ms
memory: 3576kb

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

input:

2 0
1 2 340241821

output:

340241821 

result:

ok 1 number(s): "340241821"

Test #6:

score: -100
Wrong Answer
time: 1ms
memory: 3664kb

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:

2142430936 

result:

wrong answer 1st numbers differ - expected: '83343544566', found: '2142430936'