QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#771207#2993. 林克卡特树propane25 615ms35040kbC++203.9kb2024-11-22 10:49:122024-11-22 10:49:13

Judging History

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

  • [2024-11-22 10:49:13]
  • 评测
  • 测评结果:25
  • 用时:615ms
  • 内存:35040kb
  • [2024-11-22 10:49:12]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<array>
#include<algorithm>
using namespace std;
using LL = long long;
const int maxn = 3e5 + 5;
vector<pair<int, int> > g[maxn];
int in[maxn], seq[maxn], ts;

void dfs(int u, int fa){
    for(auto it = g[u].begin(); it != g[u].end(); it++){
        if (it->first == fa){
            g[u].erase(it);
            break;
        }
    }
    in[u] = ++ts;
    seq[ts] = u;
    for(auto [j, w] : g[u]){
        dfs(j, u);
    }
}

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int n, k;
    cin >> n >> k;
    for(int i = 0; i < n - 1; i++){
        int a, b, c;
        cin >> a >> b >> c;
        // 取反转化为求最小值
        c = -c;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
    dfs(1, -1);

    // 转化为可以选k+1条链(可以退化为一个点)
    k += 1;

    // 0: 已经终止
    // 1: 当前子树有一条到根的链
    vector<array<pair<LL, LL>, 2> > dp(n + 1);

    // wqs二分要求在结果相同的时候尽量多拿物品
    // 所以等价于选择任意条链使得总和最小

    auto check = [&](int mid){
        for(int i = ts; i >= 1; i--){
            int u = seq[i];
            
            vector<int> p;
            for(auto [j, w] : g[u]){
                dp[j][1].first += w;
                p.push_back(j);
            }
            sort(p.begin(), p.end(), [&](int a, int b){
                LL d1 = dp[a][1].first - dp[a][0].first;
                LL d2 = dp[b][1].first - dp[b][0].first;
                if (d1 != d2) return d1 < d2;
                LL d3 = dp[a][1].second - dp[a][0].second;
                LL d4 = dp[b][1].second - dp[b][0].second;
                return d3 > d4;
            });
            // 当前点是一条链的LCA
            {
                pair<LL, LL> ans{mid, 1};
                for(int k = 0; k < 2 and k < p.size(); k++){
                    int j = p[k];
                    if (dp[j][1].first < dp[j][0].first or (dp[j][1].first == dp[j][0].first and dp[j][1].second > dp[j][0].second)){
                        ans.first += dp[j][1].first;
                        ans.second += dp[j][1].second;
                    }
                    else{
                        ans.first += dp[j][0].first;
                        ans.second += dp[j][0].second;
                    }
                }
                for(int k = 2; k < p.size(); k++){
                    int j = p[k];
                    ans.first += dp[j][0].first;
                    ans.second += dp[j][0].second;
                }
                dp[u][0] = ans;
            }
            {
                pair<LL, LL> ans{0, 0};
                for(int k = 0; k < 1 and k < p.size(); k++){
                    int j = p[k];
                    if (dp[j][1].first < dp[j][0].first or (dp[j][1].first == dp[j][0].first and dp[j][1].second > dp[j][0].second)){
                        ans.first += dp[j][1].first;
                        ans.second += dp[j][1].second;
                    }
                    else{
                        ans.first += dp[j][0].first;
                        ans.second += dp[j][0].second;
                    }
                }
                for(int k = 1; k < p.size(); k++){
                    int j = p[k];
                    ans.first += dp[j][0].first;
                    ans.second += dp[j][0].second;
                }
                dp[u][1] = ans;
            }
        }
        return dp[1][0];
    };

    int l = -2e6, r = 2e6;
    while(l < r){
        int mid = l + (r - l + 1) / 2;
        auto [sum, cnt] = check(mid);
        if (cnt >= k) l = mid;
        else r = mid - 1;
    }
    auto [sum, cnt] = check(r);
    cout << -(sum - 1LL * k * r) << '\n';

}

詳細信息


Pretests


Final Tests

Test #1:

score: 0
Wrong Answer
time: 317ms
memory: 23492kb

input:

200000 0
65350 50045 831081
197523 52910 450640
102223 58430 595075
145187 41777 -548397
119538 69164 -976961
42852 63503 -590523
155411 169713 -523260
156688 13869 -63012
199944 82780 21461
93164 20930 -202990
2189 180064 931389
89237 187565 277289
156421 46988 774727
29142 19573 -953773
44537 1258...

output:

-82709734903

result:

wrong answer 1st lines differ - expected: '119876064', found: '-82709734903'

Test #2:

score: 0
Wrong Answer
time: 539ms
memory: 34496kb

input:

300000 0
197640 17080 162783
269725 187014 -181947
240689 261756 129022
226971 264281 338196
2415 179116 -871598
103266 44170 -454670
63425 143632 660611
205510 137031 -575054
248763 191913 233536
106763 213425 369236
197546 277600 -933947
219363 134954 807154
156605 218354 -516243
23354 181485 -803...

output:

-125273794424

result:

wrong answer 1st lines differ - expected: '187647882', found: '-125273794424'

Test #3:

score: 0
Wrong Answer
time: 307ms
memory: 23232kb

input:

200000 1
181791 162820 711757
127748 11452 -795032
80860 71793 525612
195881 135260 -126472
183820 157817 665318
165220 20364 400920
187386 108213 -236737
133696 28527 280508
26024 19170 59816
18044 95464 497705
96297 191435 -191786
130752 20531 -683234
94787 31667 441623
47892 175810 -734631
146477...

output:

-98109445031

result:

wrong answer 1st lines differ - expected: '133041435', found: '-98109445031'

Test #4:

score: 0
Wrong Answer
time: 516ms
memory: 34036kb

input:

300000 1
188256 180568 -883916
297662 188433 62579
72882 3060 802792
173772 156239 -685924
89475 97850 109283
189977 238977 668196
147681 137368 287673
130735 126820 328423
163022 197254 404113
103379 245688 273971
84409 142542 121838
279404 107017 349027
53002 42234 150702
38970 130301 -657900
2262...

output:

-137366213266

result:

wrong answer 1st lines differ - expected: '243973784', found: '-137366213266'

Test #5:

score: 0
Wrong Answer
time: 319ms
memory: 23484kb

input:

200000 2
169901 12952 -717273
167468 57457 57238
112530 187631 720251
140886 80961 -211382
169996 116263 747467
133399 190844 -317325
37450 39059 -649790
189065 39630 28609
123082 110715 -212169
27668 161879 -8632
192678 126317 566068
15559 23831 -820002
147769 67130 -565425
96285 77276 11875
137112...

output:

-90719074503

result:

wrong answer 1st lines differ - expected: '272549445', found: '-90719074503'

Test #6:

score: 0
Wrong Answer
time: 322ms
memory: 24004kb

input:

200000 2
12078 169076 -942765
44175 99956 231323
133198 8579 171890
18545 179563 -85659
99402 116954 -31337
31702 166261 441837
31558 170873 531500
176395 135683 124560
161927 102775 -976865
35809 136934 299229
191169 34738 -533879
185462 176939 -750013
43633 41368 -926589
71922 149691 -137419
13463...

output:

-91655353557

result:

wrong answer 1st lines differ - expected: '142193848', found: '-91655353557'

Test #7:

score: 0
Wrong Answer
time: 528ms
memory: 34352kb

input:

300000 2
220743 272654 5034
119645 261362 707980
258300 182256 -670779
182916 190543 -319978
153462 244137 -863595
249563 134557 -982616
223363 22292 61412
47052 11723 -305744
91646 202184 -303558
272326 24578 -647471
48668 168529 233438
30267 295657 -410001
213986 81526 -386001
276522 87775 969181
...

output:

-121836767613

result:

wrong answer 1st lines differ - expected: '373502704', found: '-121836767613'

Test #8:

score: 0
Wrong Answer
time: 324ms
memory: 23488kb

input:

200000 100
16537 61788 402424
161077 77028 -919110
117310 153524 -290999
183518 111544 -117636
73244 165828 883254
117689 166229 -406860
102026 40518 78656
159440 72889 -675522
106123 23028 732235
86129 134361 340255
56567 159088 58610
125473 59177 -372900
158520 163684 -349257
108476 165042 875739
...

output:

-83725402279

result:

wrong answer 1st lines differ - expected: '1927078979', found: '-83725402279'

Test #9:

score: 0
Wrong Answer
time: 308ms
memory: 23500kb

input:

200000 100
159645 190327 -790296
181809 12423 601994
123183 83267 -49442
134128 130575 -143436
164650 149794 429016
47721 105379 296109
18240 27184 762687
38772 128862 596455
113039 3632 -307682
158647 61354 301670
63054 125621 -331135
12548 92826 570965
189760 87332 500426
74656 19969 228158
131009...

output:

-98989015827

result:

wrong answer 1st lines differ - expected: '1928973825', found: '-98989015827'

Test #10:

score: 0
Wrong Answer
time: 507ms
memory: 33904kb

input:

300000 100
131574 241226 825936
43551 161469 -432167
92729 242024 -852728
201397 89958 -598808
212544 177234 33151
160560 166450 857
270273 200219 -436713
287533 21869 -586096
206427 267598 48749
151663 185902 -336130
124834 57033 -435251
14650 115941 564478
240297 64983 714036
56725 256596 574766
9...

output:

-134625907014

result:

wrong answer 1st lines differ - expected: '2067331570', found: '-134625907014'

Test #11:

score: 0
Wrong Answer
time: 513ms
memory: 34688kb

input:

300000 100
263059 214437 -956970
266221 5485 -984258
181157 191851 337012
166949 93678 423724
270903 216008 -533514
178527 242172 63569
190242 167484 -620162
74008 278321 -654753
170660 3765 353481
181023 8801 982726
155475 162086 -824228
237659 285329 799133
222437 90885 822446
144544 187010 88694
...

output:

-156617459825

result:

wrong answer 1st lines differ - expected: '2084990799', found: '-156617459825'

Test #12:

score: 0
Wrong Answer
time: 543ms
memory: 34076kb

input:

300000 100
25419 209149 -500631
108405 59781 442111
293496 81726 -921827
135966 256780 -174547
209773 193974 -358614
21666 114744 -106858
221058 70979 -209083
295305 246008 -585729
148509 157116 -640851
143114 200242 651047
75896 68199 -893175
297649 209143 422060
213992 183699 199818
201195 275824 ...

output:

-151569702592

result:

wrong answer 1st lines differ - expected: '1890469382', found: '-151569702592'

Test #13:

score: 5
Accepted
time: 342ms
memory: 24272kb

input:

200000 130406
40594 19720 -623122
23706 44660 -874727
198946 155951 446918
18573 173446 -730875
620 170942 760623
90071 160815 -451054
44927 119046 601497
152317 43323 547496
13277 66904 -129002
39821 13557 -337519
97682 195872 473610
135525 112760 -323544
191848 22168 950602
25024 195672 -479997
98...

output:

44325098163

result:

ok single line: '44325098163'

Test #14:

score: 0
Wrong Answer
time: 310ms
memory: 23520kb

input:

200000 112025
19434 26866 -522901
41544 82125 340019
42503 188348 -283235
47014 76883 -752315
192433 97139 -364401
142520 45516 773937
119246 43968 -87291
24207 146679 868137
80687 82213 -813080
36329 84717 -568093
122342 155905 -124629
166598 28449 925294
36746 7420 -767398
186177 79549 -558811
123...

output:

45542854700

result:

wrong answer 1st lines differ - expected: '45620696034', found: '45542854700'

Test #15:

score: 5
Accepted
time: 335ms
memory: 23788kb

input:

200000 159073
90997 155660 688588
122016 194855 364001
108373 157814 921909
126532 153527 89711
77452 13721 434419
168396 14935 -742122
25592 91528 -156210
171969 92303 830841
174160 64741 993375
90321 26349 -426120
108013 164044 720212
150049 110076 525431
172048 119753 887134
145459 147158 78386
1...

output:

32509638528

result:

ok single line: '32509638528'

Test #16:

score: 5
Accepted
time: 345ms
memory: 23236kb

input:

200000 138730
34756 87461 -636823
118637 99255 -915828
126441 146865 191707
142168 127467 -20126
81775 88255 -222870
165937 84373 716993
104552 144925 -279669
157496 176688 691692
91940 55387 996047
26473 100735 402521
180263 64826 -845891
193409 27113 -260656
82076 68265 -505721
47211 61310 330882
...

output:

41824057737

result:

ok single line: '41824057737'

Test #17:

score: 0
Wrong Answer
time: 554ms
memory: 33492kb

input:

300000 129178
23041 100501 -373992
227874 165730 605092
67462 240962 -713691
109035 104054 -362417
187831 102896 554829
335 127284 -810036
141401 282130 750195
228816 71494 -696039
130283 15637 771038
119508 43490 520169
42895 277561 504582
103769 114888 -343346
73436 73051 491763
196506 25657 13280...

output:

57314796225

result:

wrong answer 1st lines differ - expected: '68210040353', found: '57314796225'

Test #18:

score: 0
Wrong Answer
time: 615ms
memory: 35040kb

input:

300000 152830
264154 103322 476154
41997 134920 -402116
163045 84775 591957
109208 9792 -983414
23856 44103 -745743
90231 82597 -569317
170372 74011 -945586
90507 199076 -328824
134113 2043 -636000
240437 131789 555722
236029 167549 -799563
273537 278524 287973
7746 84943 707424
113967 107808 -81031...

output:

70515363923

result:

wrong answer 1st lines differ - expected: '71122489923', found: '70515363923'

Test #19:

score: 5
Accepted
time: 555ms
memory: 34004kb

input:

300000 220606
73646 165467 211934
287560 202214 503555
133070 247406 930057
135894 259547 953228
71803 157101 -918434
222521 210059 -833619
207233 164303 424927
28849 254325 -553475
115615 159949 44857
71463 13068 -109967
220595 75134 902520
266060 244008 632129
288006 100470 602933
150032 239332 53...

output:

57882988995

result:

ok single line: '57882988995'

Test #20:

score: 5
Accepted
time: 557ms
memory: 33320kb

input:

300000 177288
96606 159646 -843008
6102 77679 -290722
162415 126260 610363
143376 146328 -745143
64929 265269 605571
183004 109222 754682
59516 295312 -675501
17670 184822 740404
203705 225595 -824744
249255 59274 761581
299854 210063 -551417
231581 65698 808182
181562 142802 143491
146718 135568 -2...

output:

68962524975

result:

ok single line: '68962524975'