QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#133184#6629. Travelling Tradertatyam2 147ms43428kbC++174.3kb2023-08-01 17:03:192023-08-01 17:03:28

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-01 17:03:28]
  • 评测
  • 测评结果:2
  • 用时:147ms
  • 内存:43428kb
  • [2023-08-01 17:03:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool chmax(ll& a, ll b) { if(a >= b) return 0; a = b; return 1; }
#define all(a) begin(a), end(a)

/*
    K = 1 => longest path
    K = 2 => DP
     aru chouten wo funde ue ni modoru toki:
      1 tu no child he susunde, modottekuru:
       gyakusaisei sureba child ni tuitemo onajikoto
      hoka no child ha itiban ue wo sawarudake de susumenai
     aru chouten wo funde ue ni modoranai toki:
      1 tu no child he susunde, modottekuru
      1 tu no child he susunde, modottekonai
      hoka no child ha itiban ue wo sawarudake de susumenai
      
*/
vector<ll> solve1(ll N, vector<vector<ll>> child, vector<ll> P) {
    struct T{
        ll val = 0, argmax = -1;
    };
    vector<T> dp(N);
    auto dfs = [&](ll i, auto dfs) -> void {
        for(ll j : child[i]) {
            dfs(j, dfs);
            if(chmax(dp[i].val, dp[j].val)) dp[i].argmax = j;
        }
        dp[i].val += P[i];
    };
    dfs(0, dfs);
    vector<ll> ans = {0};
    while(1) {
        const ll i = ans.back();
        if(child[i].empty()) return ans;
        ans.push_back(dp[i].argmax);
    }
}
vector<ll> solve2(ll N, vector<vector<ll>> child, vector<ll> P) {
    struct T{
        ll v0 = 0, v1 = 0, v2 = 0, i11 = -1, i21 = -1, i22 = -1;
    };
    vector<T> dp(N);
    auto dfs = [&](ll i, auto dfs) -> void {
        auto& [v0, v1, v2, i11, i21, i22] = dp[i];
        v0 = P[i];
        v1 = P[i];
        v2 = P[i];
        if(child[i].empty()) return;
        vector<pair<ll, ll>> add1, add2;
        for(ll j : child[i]) {
            dfs(j, dfs);
            const T ch = dp[j];
            v1 += ch.v0;
            v2 += ch.v0;
            add1.emplace_back(ch.v1 - ch.v0, j);
            add2.emplace_back(ch.v2 - ch.v0, j);
        }
        if(child[i].size() == 1) {
            auto [a1, i1] = add1[0];
            v1 += a1;
            i11 = i1;
            auto [a2, i2] = add2[0];
            v2 += a2;
            i22 = i2;
            return;
        }
        sort(all(add1), greater<>{});
        sort(all(add2), greater<>{});
        add1.resize(2);
        add2.resize(2);
        {
            ll diff = -1;
            for(auto [a1, i1] : add1) for(auto [a2, i2] : add2) if(i1 != i2 && chmax(diff, a1 + a2)) {
                i21 = i1;
                i22 = i2;
            }
            v2 += diff;
        }
        auto [a1, i1] = add1[0];
        v1 += a1;
        i11 = i1;
    };
    dfs(0, dfs);
    {
        auto dfs = [&](ll i, ll t, auto dfs) -> vector<ll> {
            auto [v0, v1, v2, i11, i21, i22] = dp[i];
            if(child[i].empty()) return {i};
            if(t == 0) return {i};
            if(t == 1) {
                vector<ll> ans = dfs(i11, 1, dfs);
                reverse(all(ans));
                ans.insert(ans.begin(), i);
                for(ll j : child[i]) if(j != i11) ans.push_back(j);
                return ans;
            }
            vector<ll> ans = i21 != -1 ? dfs(i21, 1, dfs) : vector<ll>{};
            reverse(all(ans));
            ans.insert(ans.begin(), i);
            for(ll j : child[i]) if(j != i21 && j != i22) ans.push_back(j);
            auto ans2 = dfs(i22, 2, dfs);
            ans.insert(ans.end(), all(ans2));
            return ans;
        };
        return dfs(0, 2, dfs);
    }
}
mt19937 rnd;
int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll N, K;
    cin >> N >> K;
    #ifdef DEBUG
    while(1){
    #endif
    vector<vector<ll>> g(N);
    for(ll i = 1; i < N; i++) {
        ll A, B;
    #ifdef DEBUG
        A = i; B = rnd() % i;
    #else
        cin >> A >> B;
        A--; B--;
    #endif
        g[A].push_back(B);
        g[B].push_back(A);
    }
    // erase parent
    auto dfs = [&](ll i, auto dfs) -> void {
        for(ll j : g[i]) {
            g[j].erase(find(all(g[j]), i));
            dfs(j, dfs);
        }
    };
    dfs(0, dfs);
    vector<ll> P(N);
    #ifdef DEBUG
    for(ll& p : P) p = rnd();
    #else
    for(ll& p : P) cin >> p;
    #endif
    auto ans = K == 1 ? solve1(N, g, P) : solve2(N, g, P);
    ll value = 0;
    for(ll& i : ans) value += P[i];
    cout << value << endl;
    cout << ans.size() << endl;
    for(ll i : ans) cout << i + 1 << ' ';
    cout << endl;
    #ifdef DEBUG
    }
    #endif
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 2
Accepted

Test #1:

score: 2
Accepted
time: 0ms
memory: 3480kb

input:

2 1
1 2
255959470 961356354

output:

1217315824
2
1 2 

result:

ok correct!

Test #2:

score: 0
Accepted
time: 2ms
memory: 3632kb

input:

1000 1
730 89
762 280
923 523
740 22
679 350
448 769
102 712
154 965
219 32
238 289
484 502
183 311
999 682
806 450
275 101
131 197
749 720
131 937
960 202
503 320
95 262
595 133
809 560
659 451
843 218
258 842
564 316
729 727
413 237
606 531
469 258
612 8
707 539
359 680
957 639
381 708
104 490
234...

output:

95535
17
1 173 449 472 396 545 668 270 981 489 852 836 763 6 218 843 758 

result:

ok correct!

Test #3:

score: 0
Accepted
time: 83ms
memory: 29628kb

input:

200000 1
111811 133538
179217 151840
171974 117009
187613 169656
64662 13136
132434 89348
52802 175565
194821 191312
196047 99457
40339 152969
29669 180026
147677 57771
119598 58048
80707 146623
72232 101624
48109 11800
71536 69
31573 129
24409 17263
1033 148614
66977 149926
138624 87653
141889 1178...

output:

221
35
1 145832 90178 52464 3517 55709 39776 67451 59386 143855 102872 38865 13093 177086 7366 190908 160039 69864 196809 13257 612 171083 182883 14221 93359 52156 27994 103745 151704 138607 5346 14735 29598 89600 128747 

result:

ok correct!

Test #4:

score: 0
Accepted
time: 85ms
memory: 29760kb

input:

200000 1
102636 78442
179388 84484
161437 35179
102313 154299
62439 71542
176361 125315
174129 186376
180886 54947
154823 114239
174647 112385
136495 187134
157035 96260
101101 192444
58209 23947
55494 191600
168007 162648
140149 73180
130665 180633
129328 67380
90262 134914
185905 104220
111321 154...

output:

21795891322
36
1 13557 199188 104317 71891 69787 1221 63258 191536 179446 83510 187880 124824 43888 83237 194602 59080 196038 195977 18490 43421 110298 60011 137785 140692 48345 68279 128780 198550 29394 56331 112092 192199 177180 16418 142142 

result:

ok correct!

Test #5:

score: 0
Accepted
time: 87ms
memory: 30048kb

input:

200000 1
682 75953
92444 160568
113369 93705
154622 193304
149619 128186
104784 48913
131684 161196
25886 151867
89191 19511
99233 137377
104650 120096
64347 93526
111350 71598
7568 120116
123497 76821
25436 190715
99884 33654
109438 69462
165464 2475
163215 34551
33926 85078
101208 193355
50705 828...

output:

99327575017
197
1 178606 82034 53029 10508 21404 203 109187 121716 142023 3901 36728 9916 192913 18250 170199 113960 179753 163922 179588 31797 31645 183321 83207 13973 128176 38001 160968 9055 62809 168173 43933 187373 123795 114656 2192 193151 25062 141855 133596 155793 64049 57320 93903 33957 139...

result:

ok correct!

Test #6:

score: 0
Accepted
time: 64ms
memory: 29028kb

input:

200000 1
91999 92900
195726 58991
132067 99937
168188 152017
188495 19779
105961 45241
53406 75757
85118 170259
46250 47585
132248 8609
195110 32777
164307 95643
133411 739
170952 145623
19297 14414
171045 97619
74663 193421
139543 189434
36319 56453
77520 91164
91469 30021
128798 62259
183807 15271...

output:

9098893435
13
1 164355 56677 150505 174723 115829 88068 105453 199874 190131 165416 182628 114943 

result:

ok correct!

Test #7:

score: 0
Accepted
time: 54ms
memory: 28088kb

input:

200000 1
189797 1
1 148138
1 95067
141831 1
168151 1
1 25692
95612 1
1 135979
1 141581
119622 1
1 131946
86508 1
98799 1
1 189104
1 117526
46338 1
1 166375
1 28026
165221 1
54204 1
1 98743
1 181414
1 34313
1 71302
1 161200
1 146339
1 47014
1 137258
1 57857
1 196493
1 99105
54487 1
104725 1
1 45203
1...

output:

1175349557
2
1 153544 

result:

ok correct!

Test #8:

score: 0
Accepted
time: 85ms
memory: 29748kb

input:

199999 1
56367 178046
1 156890
170478 1
111308 177326
1 188427
1 90169
126610 1
161930 167698
96500 126424
118330 158517
186608 1
95505 107863
1 142887
72279 27494
1 114700
118535 1
68584 63156
97555 19966
39239 1
128030 1
1 86200
66974 1
34616 47100
173578 1
1 117279
89769 43412
1 89670
103573 1
13...

output:

2999144210
3
1 52552 129910 

result:

ok correct!

Test #9:

score: 0
Accepted
time: 147ms
memory: 43428kb

input:

200000 1
95601 67789
174512 65854
198542 123861
153390 92355
141969 168754
177054 101194
25665 15524
131869 168080
171051 30732
97293 119758
103002 59019
141990 124310
161550 116618
2585 170410
132999 38200
99202 98733
73949 155033
144918 64086
1594 34916
37138 165382
13452 170885
136973 62178
15250...

output:

200000000000000
200000
1 47213 179780 132180 145558 41335 179095 156350 24912 104386 94658 54370 97034 108043 73905 141018 157563 199841 176455 147422 87545 190562 135095 24903 62484 36389 156106 45144 120321 4911 173474 102976 13602 68252 7332 141515 59337 182112 124040 38089 15458 161370 41901 144...

result:

ok correct!

Test #10:

score: 0
Accepted
time: 83ms
memory: 35868kb

input:

200000 1
122636 169264
76896 89915
72116 125306
186356 74852
84394 177419
21725 144848
106395 111991
189102 195804
6151 170169
185460 146764
6304 149801
147880 99539
6202 175326
104277 26515
39402 33436
116555 185545
44341 92305
197925 125286
28215 102176
182103 160554
105237 169007
105618 75618
190...

output:

49951940813408
100001
1 88700 18534 14218 21693 84470 150823 121376 192964 139616 11067 93019 188349 55336 13628 87630 31553 28945 29827 140175 179655 10038 38915 99968 89953 72978 102045 45280 176852 171879 100086 93399 183932 84482 111738 112608 136016 101850 19371 96135 54333 95939 2865 140685 13...

result:

ok correct!

Test #11:

score: 0
Accepted
time: 28ms
memory: 29768kb

input:

200000 1
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26 53
27 54...

output:

13954593167
18
1 2 5 10 20 40 80 161 323 647 1295 2590 5181 10363 20727 41454 82908 165817 

result:

ok correct!

Subtask #2:

score: 0
Wrong Answer

Test #12:

score: 7
Accepted
time: 0ms
memory: 3552kb

input:

2 2
2 1
243296356 635616793

output:

878913149
2
1 2 

result:

ok correct!

Test #13:

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

input:

10 2
6 4
3 7
5 10
6 10
8 2
3 9
3 5
4 2
1 4
2 4 2 5 5 4 2 3 4 2

output:

33
10
1 4 10 3 9 7 5 6 2 8 

result:

ok correct!

Test #14:

score: -7
Wrong Answer
time: 1ms
memory: 3600kb

input:

200 2
150 170
21 33
96 152
143 26
136 70
92 159
34 164
163 182
74 115
93 61
151 83
81 119
10 146
114 170
39 83
139 4
173 41
193 96
87 57
14 164
107 51
45 15
157 17
43 183
96 30
11 137
55 18
138 81
87 12
112 122
159 82
195 185
75 71
16 191
33 88
70 195
149 114
106 160
96 118
92 44
9 141
107 143
151 2...

output:

54153356810
91
1 151 179 194 89 83 135 27 122 117 120 180 125 112 40 45 138 94 162 88 21 193 59 170 149 110 28 171 114 96 105 131 33 15 99 72 12 76 70 53 159 17 178 24 44 41 67 173 186 42 116 92 82 197 101 5 32 121 87 29 198 64 93 19 126 8 141 37 100 3 9 108 52 61 54 14 137 49 26 168 107 79 51 143 1...

result:

wrong answer your profit is 54153356810 but jury's plan is 57921623677, your plan is not optimal!

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #83:

score: 4
Accepted
time: 2ms
memory: 3932kb

input:

2000 3
1359 90
1703 163
158 188
360 1501
195 664
1414 215
1546 1756
536 1096
1726 1223
1150 104
1757 703
1982 282
1023 998
1180 419
576 1759
1496 1993
44 670
1703 952
855 849
1998 1399
1280 980
1533 1090
1270 678
1680 387
469 1734
1799 263
473 588
303 226
5 295
1489 1471
1094 1667
1912 210
1368 1360...

output:

1008611451196
2000
1 1091 961 605 1613 454 1237 1823 1101 1617 1369 1840 562 1256 901 1040 709 1291 1238 1526 129 523 816 1919 674 1961 452 297 1903 1656 560 739 183 1522 951 863 877 1973 1548 191 1265 1344 33 1679 565 774 276 139 926 1397 46 36 1019 1427 289 1376 545 16 1880 1076 1684 1968 1504 81 ...

result:

ok correct!

Test #84:

score: 0
Accepted
time: 2ms
memory: 4064kb

input:

2000 3
1727 567
1783 1850
205 985
323 1094
1153 821
1756 117
377 1928
1026 1303
1343 1814
268 745
242 948
1140 1218
7 1675
101 1798
1403 1752
1184 671
87 248
1953 30
1580 1441
507 1438
525 419
901 421
1585 1405
1575 883
1952 1930
1988 1325
615 722
994 1202
178 474
1978 1500
899 481
216 409
999 1817
...

output:

1012330476243
2000
1 369 1789 598 488 419 422 525 269 202 1079 694 1379 1454 1724 1545 88 246 1123 701 1522 1158 1696 1364 1918 131 1589 1832 872 1057 1532 345 1725 67 761 1634 1174 719 1807 650 693 61 718 554 841 234 1935 175 220 105 917 1784 997 1315 1530 92 257 802 1071 1369 1257 1046 1275 993 31...

result:

ok correct!

Test #85:

score: -4
Wrong Answer
time: 2ms
memory: 3920kb

input:

2000 3
1213 130
101 508
72 1199
1550 1096
1099 861
1515 627
1299 1672
1338 105
1444 1019
15 1560
1949 971
52 1312
30 529
186 1687
1917 484
1971 349
537 1223
1955 1377
300 1060
1786 1811
1960 90
1959 1353
1831 1548
303 511
1073 1197
863 1527
1379 994
31 9
1247 1707
1395 1532
29 1544
119 296
1919 1554...

output:

803960409885
1502
1 1365 1487 1721 810 1986 1821 668 513 1002 830 1255 1557 751 574 1802 1658 1491 60 1261 640 1010 374 1292 1450 1710 1896 942 718 1246 706 295 1377 1955 729 954 242 1809 93 1610 381 435 1127 863 384 1356 1067 181 1503 1169 1153 1997 1241 563 1475 1303 1310 1208 388 1069 1483 1181 8...

result:

wrong answer your profit is 803960409885 but jury's plan is 1001405462082, your plan is not optimal!

Subtask #6:

score: 0
Skipped

Dependency #5:

0%