QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#382261#5350. 网络bachbeo2007100 ✓237ms18524kbC++232.7kb2024-04-08 09:55:502024-04-08 09:55:50

Judging History

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

  • [2024-04-08 09:55:50]
  • 评测
  • 测评结果:100
  • 用时:237ms
  • 内存:18524kb
  • [2024-04-08 09:55:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define fi first
#define se second
const ll inf=1e18;
const int maxn=100005;
int n,l,c[maxn];
pii par[maxn];
vector<pii> edge[maxn];
int ver[maxn],sz;
ll L[maxn],d[maxn],dep[maxn],ans;
void dfs(int u,int p){
    for(pii v:edge[u]){
        if(v.fi==p) continue;
        dep[v.fi]=dep[u]+v.se;
        par[v.fi]={u,v.se};
        dfs(v.fi,u);
    }
}
ll dfs2(int u,int p){
    ll Max=0;
    for(pii v:edge[u]){
        if(v.fi==p || c[v.fi]) continue;
        ll cur=v.se+dfs2(v.fi,u);
        ans=max(ans,Max+cur);Max=max(Max,cur);
    }
    return Max;
}
bool check(ll D){
    ll lm=-inf,ls=-inf,rm=-inf,rs=-inf,mm=-inf,ms=-inf;
    deque<int> dq;
    for(int i=0;i<sz;i++){
        while(!dq.empty() && L[i]+d[i]-L[dq.front()]+d[dq.front()]>D){
            ms=max(ms,L[dq.front()]+d[dq.front()]);
            mm=max(mm,d[dq.front()]-L[dq.front()]);
            dq.pop_front();
        }
        ll cur=d[i]+l-D;
        ls=max(ls,ms+L[i]+cur);
        lm=max(lm,ms-L[i]+cur);
        rm=max(rm,mm+L[i]+cur);
        rs=max(rs,mm-L[i]+cur);
        while(!dq.empty() && d[dq.back()]-L[dq.back()]<=d[i]-L[i]) dq.pop_back();
        dq.push_back(i);
    }
    rm=-rm;rs=-rs;
    int pls=sz,plm=-1,prs=sz-1,prm=0;
    for(int i=0;i<sz;i++){
        while(pls>0 && L[pls-1]+L[i]>=ls) pls--;
        while(plm<sz-1 && L[i]-L[plm+1]>=lm) plm++;
        while(L[prs]+L[i]>rs) prs--;
        while(prm<sz && L[i]-L[prm]>rm) prm++;
        if(max({pls,prm})<=min({plm,prs})) return true;
    }
    return false;
}
void solve(){
    sz=0;ans=0;
    for(int i=1;i<=n;i++){edge[i].clear();c[i]=0;}
    for(int i=1;i<n;i++){
        int u,v,w;cin >> u >> v >> w;
        edge[u].push_back({v,w});
        edge[v].push_back({u,w});
    }
    int x=0;dep[1]=0;dfs(1,0);
    for(int i=1;i<=n;i++){
        if(dep[i]>dep[x]) x=i;
    }
    int y=0;dep[x]=0;dfs(x,0);
    for(int i=1;i<=n;i++){
        if(dep[i]>dep[y]) y=i;
    }
    ll r=dep[y];
    c[y]=1;ver[sz++]=y;
    while(y!=x){
        L[sz]=par[y].se;y=par[y].fi;
        c[y]=1;ver[sz++]=y;
    }
    for(int i=0;i<sz;i++){
        d[i]=dfs2(ver[i],0);
        L[i]+=(i?L[i-1]:0);
        //cout << d[i] << ' ' << L[i] << ' ' << ver[i] << '\n';
    }
    ll l=ans,res=r;
    //cout << l << ' ' << r << '\n';
    while(r>=l){
        ll mid=(r+l)>>1;
        if(check(mid)){res=mid;r=mid-1;}
        else l=mid+1;
    }
    cout << res << '\n';
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    while(cin >> n >> l){
        if(n==0 && l==0) break;
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 6ms
memory: 5556kb

input:

10 957
1 2 283
2 6 2004
4 2 3077
9 1 2095
7 2 1053
2 8 861
9 10 2604
8 5 991
3 1 563
10 1316
9 8 2033
4 8 1885
4 7 124
10 7 1672
10 3 2205
4 2 1974
10 1 3216
5 9 39
8 6 3032
3 2147483647
1 2 2147483647
3 2 2147483647
5 2147483647
2 1 2147483647
3 2 2147483647
5 3 2147483647
4 5 2147483647
10 335
5 9...

output:

5455
7564
2147483647
4294967294
6277

result:

ok 5 number(s): "5455 7564 2147483647 4294967294 6277"

Test #2:

score: 5
Accepted
time: 7ms
memory: 7604kb

input:

20 443
3 4 778
4 9 594
4 7 587
19 3 250
4 15 363
17 4 291
19 14 564
6 17 541
3 1 773
17 8 12
6 13 568
10 1 183
15 2 63
18 2 68
8 5 330
20 8 823
3 12 270
16 7 194
11 5 212
20 118
4 8 951
8 6 1088
6 14 409
3 14 877
3 16 1179
16 15 189
5 15 385
5 11 604
10 11 252
3 19 800
6 18 661
13 15 169
2 10 413
15...

output:

2508
4162
6131
2147483647
4294967294
15028
4286
5607
5227
2126
1542

result:

ok 11 numbers

Test #3:

score: 5
Accepted
time: 9ms
memory: 7876kb

input:

46 31
39 4 378
39 20 404
4 11 461
2 39 62
20 45 398
11 3 225
4 34 406
33 39 502
41 4 528
4 27 442
35 45 458
35 25 535
14 11 380
31 3 106
29 11 213
20 9 77
18 29 151
3 7 50
21 3 182
18 37 299
45 40 271
15 9 419
7 12 71
16 7 52
4 17 326
35 32 101
13 3 292
38 31 362
40 30 203
23 45 320
24 35 443
17 10 ...

output:

2520
3987
5827
2147483647
4294967294
15326
10733
12138
3542

result:

ok 9 numbers

Test #4:

score: 5
Accepted
time: 9ms
memory: 7608kb

input:

47 989
34 38 904
34 31 1117
23 38 664
24 34 1340
24 44 863
24 28 292
31 41 491
38 27 892
38 39 314
44 6 2042
3 41 785
28 15 1850
39 21 1401
36 44 191
45 21 53
33 36 152
44 26 603
18 21 125
46 28 422
17 27 1016
47 31 1694
38 42 137
29 28 838
40 33 186
41 8 1446
10 44 1580
43 15 1140
35 39 1285
15 30 ...

output:

9086
14925
21423
2147483647
4294967294
60817
37185
41307
20004

result:

ok 9 numbers

Test #5:

score: 5
Accepted
time: 6ms
memory: 7904kb

input:

64 61
3 61 1001
3 60 1913
58 61 407
58 28 1799
3 54 1850
3 48 1259
61 49 656
19 48 675
49 31 1952
42 31 453
49 29 1128
39 58 1932
33 49 132
18 19 931
35 42 59
33 53 262
32 19 1070
25 58 1659
55 39 1123
12 60 998
12 37 286
29 26 258
34 55 448
42 62 328
41 25 34
16 58 1074
42 13 703
45 12 1361
22 61 1...

output:

10383
19451
26336
2147483647
4294967294
84811
75176
80233
22260

result:

ok 9 numbers

Test #6:

score: 5
Accepted
time: 7ms
memory: 7932kb

input:

296 298
39 46 1755
39 232 989
8 46 1265
83 46 306
234 46 675
133 232 1381
133 15 1499
83 120 1725
8 55 181
133 56 15
38 120 912
55 76 1348
252 46 272
205 15 1525
15 13 2131
13 198 1805
78 15 1640
46 91 1165
78 14 435
234 20 1878
75 205 1518
37 75 1780
47 133 1806
15 86 482
92 13 334
155 75 831
158 1...

output:

25406
158668
302577
2147483647
4294967294
418549
150936
156441
121166

result:

ok 9 numbers

Test #7:

score: 5
Accepted
time: 10ms
memory: 7808kb

input:

292 2190
228 235 4313
228 167 3309
167 126 887
175 126 203
236 126 336
52 228 4006
112 236 2243
235 216 1
292 126 4024
7 175 2752
74 126 941
100 52 4590
168 175 3421
84 52 2358
235 63 2456
167 211 1113
33 52 2917
72 33 1957
43 292 1417
117 43 2286
235 224 3035
52 125 3598
216 54 2860
125 173 3370
12...

output:

47309
344526
639998
2147483647
4294967294
986345
339357
329065
260708

result:

ok 9 numbers

Test #8:

score: 5
Accepted
time: 7ms
memory: 7724kb

input:

298 234
41 279 60
279 134 366
41 5 2491
279 8 2257
46 5 436
95 279 548
92 95 2474
52 41 2099
202 134 2675
121 95 697
76 46 957
13 52 1742
162 279 1602
8 7 194
11 46 1870
278 95 1651
31 162 1255
46 291 932
211 278 1778
52 189 794
211 220 2526
189 112 2571
7 242 2293
162 27 1226
112 47 2142
90 31 1643...

output:

25640
198722
383271
2147483647
4294967294
555715
175018
181077
142226

result:

ok 9 numbers

Test #9:

score: 5
Accepted
time: 17ms
memory: 7940kb

input:

997 141
819 482 1942
676 482 1633
482 11 1221
482 797 1941
11 325 192
11 370 1225
104 11 274
370 674 407
276 104 572
104 701 98
16 674 819
701 975 1507
468 975 814
482 594 1851
550 11 1731
706 819 37
16 766 1305
550 285 537
885 468 420
150 701 732
940 285 1552
824 104 660
11 395 115
797 403 782
674 ...

output:

27260
440534
196882
460683
511547
385107
1348002
943002
2147483647
4294967294
174090

result:

ok 11 numbers

Test #10:

score: 5
Accepted
time: 16ms
memory: 7812kb

input:

916 699
780 814 2734
814 222 2579
222 534 1357
222 343 139
272 222 3145
814 835 2239
814 862 1290
178 862 1918
780 639 879
639 704 3670
464 178 2892
639 564 1340
850 464 997
464 50 374
547 343 1889
627 780 1664
343 784 2616
496 627 1483
704 140 3761
343 901 616
784 635 1532
140 805 3335
343 667 1761...

output:

50656
937413
454122
910173
935843
761356
2706426
1760678
2147483647
4294967294
370190

result:

ok 11 numbers

Test #11:

score: 5
Accepted
time: 50ms
memory: 9512kb

input:

4796 727
2703 453 2739
2703 3049 179
1421 3049 3536
2829 1421 3568
3049 2145 1004
2145 4277 1669
2145 1210 885
2145 4607 4441
2580 1210 1772
4607 3940 729
4726 4607 396
2703 1539 3043
143 3940 3993
1054 1210 489
4277 4475 538
1210 2359 3328
4311 1210 1321
2580 1071 2866
4475 319 250
1539 2983 864
29...

output:

73199
2830062
11243287
11686429
16705190
48874709
66731031
4591288
13535854
21256486
2147483647
4294967294
3808654

result:

ok 13 numbers

Test #12:

score: 5
Accepted
time: 45ms
memory: 9872kb

input:

4924 633
4077 1081 2223
2955 1081 3134
1081 4491 2147
129 1081 3670
4491 3889 2581
4237 4077 764
3140 129 917
2130 1081 2642
2955 2046 439
1623 2130 99
3140 4800 2683
3411 1081 1186
1638 4491 3723
2130 3066 857
558 1638 917
3681 4237 1610
3240 3889 2128
3007 2046 194
844 3411 1147
3703 4077 2714
314...

output:

59512
2297677
8382880
8719409
12701086
36814673
52481104
3732801
10077363
17120650
2147483647
4294967294
3012800

result:

ok 13 numbers

Test #13:

score: 5
Accepted
time: 50ms
memory: 9944kb

input:

4761 192
2352 375 1860
2352 4696 3503
2352 2556 630
1805 375 1085
1805 4129 233
4438 2556 2195
3015 1805 590
2352 4192 2124
375 3203 966
2352 1770 443
3133 375 3916
1770 2239 295
1944 3133 315
1784 4192 3661
4696 656 3397
2646 1770 3117
2556 4561 68
1944 5 2144
4083 2352 489
3015 242 2694
2352 380 1...

output:

63055
2442107
9294364
9748708
13410080
41648538
62640217
4150997
10658389
18092325
2147483647
4294967294
3116522

result:

ok 13 numbers

Test #14:

score: 5
Accepted
time: 49ms
memory: 9768kb

input:

4891 244
4685 1980 837
4685 2685 1040
4205 4685 142
4205 213 2134
3002 4685 1423
4685 2901 1006
2685 1946 452
3835 4205 249
1299 213 2096
162 213 1728
162 2564 793
4422 2685 997
4205 519 220
1980 694 27
213 2160 1552
1296 3002 1255
2042 4685 468
2044 1299 1561
3438 2044 354
1452 1980 1273
241 2564 2...

output:

34858
1384101
5002102
5343806
7832834
21777220
31344055
2190794
6380986
9989897
2147483647
4294967294
1728690

result:

ok 13 numbers

Test #15:

score: 5
Accepted
time: 47ms
memory: 9720kb

input:

4684 271
2218 461 127
3630 461 674
4112 3630 108
417 461 370
2218 1125 904
511 417 466
2218 2265 297
2218 3669 372
2751 1125 206
2218 1024 81
4112 495 310
234 511 686
1211 4112 926
2256 3630 189
676 4112 28
511 301 254
1255 3669 123
1125 3522 544
1021 2265 402
1447 676 637
2509 495 130
1367 1024 931...

output:

17615
576697
2226092
2368069
3433173
9606560
11942606
943129
2656813
4485732
2147483647
4294967294
773696

result:

ok 13 numbers

Test #16:

score: 5
Accepted
time: 220ms
memory: 18024kb

input:

24094 66
19002 16021 1388
16021 380 1761
16046 19002 1147
22179 19002 416
16046 18342 1326
22179 6326 466
19345 16046 615
16021 19829 1249
17238 19345 998
15773 16046 1630
22442 19345 604
17238 21999 1037
11402 380 1613
15680 22442 1681
16046 16204 1258
1802 11402 1095
15805 18342 1121
1826 22179 75...

output:

39175
5610717
20691800
21387986
30806161
81185521
123441098
8716517
26420646
40673076
2147483647
4294967294
7018348

result:

ok 13 numbers

Test #17:

score: 5
Accepted
time: 237ms
memory: 18492kb

input:

23445 1433
22490 20906 1770
20906 10975 2814
10975 10804 1624
10804 4189 936
9788 10804 1291
9147 9788 2787
10804 10954 3022
3549 10975 2384
22490 11142 2358
10804 2903 2293
4189 8702 21
21108 10975 2639
5831 10804 2555
10975 15894 972
15984 20906 1385
14506 20906 1956
20906 18811 3015
14506 11494 2...

output:

61282
8805603
37308662
39416784
56733902
142006777
207958438
14793184
44778554
71852863
2147483647
4294967294
12001104

result:

ok 13 numbers

Test #18:

score: 5
Accepted
time: 202ms
memory: 17848kb

input:

23266 505
11641 10002 272
10002 10187 1280
15897 10187 1626
13154 15897 3626
15707 10187 2258
15897 1592 2556
1592 22558 3135
10187 6374 1241
15897 17267 3427
15897 11393 3907
17037 11393 3926
7278 6374 3236
15897 7065 2401
20643 11393 3624
17457 6374 1061
2804 10002 3601
21347 10002 3285
17457 2142...

output:

88039
13019611
54238964
58118400
81343650
198322128
288622653
21132515
61126030
100378426
2147483647
4294967294
17219358

result:

ok 13 numbers

Test #19:

score: 5
Accepted
time: 210ms
memory: 17816kb

input:

23599 625
6632 5508 848
6632 370 1445
20170 5508 760
5508 4912 21
370 18332 179
6632 4250 891
18332 10748 491
19472 10748 1115
5508 19939 1086
20170 13556 1381
20615 6632 1356
12385 10748 1369
4250 5106 140
1717 20615 1087
6421 13556 1349
18332 14692 608
8147 10748 533
6421 1713 243
4168 6632 1548
1...

output:

34404
4875956
17902894
18709656
26485191
78689697
113840150
7526492
21774129
35020170
2147483647
4294967294
5834776

result:

ok 13 numbers

Test #20:

score: 5
Accepted
time: 206ms
memory: 18524kb

input:

23950 205
19087 14505 397
14505 4532 158
14505 7591 212
19586 19087 377
16628 19586 42
19586 6246 160
4002 19087 230
16412 19087 69
19087 6489 206
18009 16412 58
20523 4002 330
16044 6246 5
7591 12316 35
16044 12993 418
14869 6246 184
20523 7026 331
20200 20523 324
12993 17425 107
20200 6663 248
205...

output:

8953
1348866
5580190
6019755
7828614
23772015
33886214
2318949
6126632
9855169
2147483647
4294967294
1736386

result:

ok 13 numbers

Extra Test:

score: 0
Extra Test Passed