QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#291125#2993. 林克卡特树MoRanSky100 ✓2358ms43940kbC++232.1kb2023-12-26 05:00:202023-12-26 05:00:20

Judging History

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

  • [2023-12-26 05:00:20]
  • 评测
  • 测评结果:100
  • 用时:2358ms
  • 内存:43940kb
  • [2023-12-26 05:00:20]
  • 提交

answer

// Skyqwq
#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N = 3e5 + 5, S = 102;
const LL INF = 0xcfcfcfcfcfcfcfcf;

int n, k, sz[N];
LL f[N][3], h[3], c[N][3], W, C[3];

vector<PII> g[N];

void inline mg(int u, int v, int w) {
	for (int x = 0; x < 3; x++) {
		for (int y = 0; y < 3; y++) {
			if (chkMax(h[x], f[u][x] + f[v][y])) C[x] = c[u][x] + c[v][y];
			if (!x && y == 1)
				if (chkMax(h[1], f[u][x] + f[v][y] + w)) C[1] = c[u][x] + c[v][y]; 
			if (x == 1 && y == 1)
				if (chkMax(h[2], f[u][x] + f[v][y] + w + W)) C[2] = c[u][x] + c[v][y] - 1;
		}
	}
	for (int x = 0; x < 3; x++) f[u][x] = h[x], c[u][x] = C[x], h[x] = INF;
}

void dfs(int u, int fa) {
	sz[u] = 1;
	f[u][0] = 0;
	for (PII t: g[u]) {
		int v = t.fi;
		if (v == fa) continue;
		dfs(v, u);
		mg(u, v, t.se);
		sz[u] += sz[v];
	}
	if (chkMax(f[u][1], f[u][0] - W)) c[u][1] = c[u][0] + 1;
}

int inline get(LL X) {
	W = X;
	memset(f, 0xcf, sizeof f);
	memset(h, 0xcf, sizeof h);
	memset(c, 0, sizeof c);
	dfs(1, 0);
	LL mx = INF; int cnt = 0;
	for (int i = 0; i < 3; i++)
		if (chkMax(mx, f[1][i])) cnt = c[1][i];
	return cnt;
}

int main() {
//	freopen("b.in", "r", stdin);
//	freopen("lct.in", "r", stdin);
	read(n), read(k); ++k;
	for (int i = 1, u, v, w; i < n; i++)
		read(u), read(v), read(w), g[u].pb(mp(v, w)), g[v].pb(mp(u, w));
	LL l = -4e11, r = 4e11;
	while (l < r) {
		LL mid = (l + r) >> 1;
		if (get(mid) <= k) r = mid;
		else l = mid + 1; 
	} 
	get(r);
	LL ans = max(max(f[1][0], f[1][1]), f[1][2]) + k * r;
    printf("%lld\n", ans);
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 747ms
memory: 35516kb

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:

119876064

result:

ok single line: '119876064'

Test #2:

score: 5
Accepted
time: 1499ms
memory: 42260kb

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:

187647882

result:

ok single line: '187647882'

Test #3:

score: 5
Accepted
time: 635ms
memory: 33548kb

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:

133041435

result:

ok single line: '133041435'

Test #4:

score: 5
Accepted
time: 1379ms
memory: 40392kb

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:

243973784

result:

ok single line: '243973784'

Test #5:

score: 5
Accepted
time: 720ms
memory: 34768kb

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:

272549445

result:

ok single line: '272549445'

Test #6:

score: 5
Accepted
time: 625ms
memory: 34508kb

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:

142193848

result:

ok single line: '142193848'

Test #7:

score: 5
Accepted
time: 1570ms
memory: 41860kb

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:

373502704

result:

ok single line: '373502704'

Test #8:

score: 5
Accepted
time: 707ms
memory: 34820kb

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:

1927078979

result:

ok single line: '1927078979'

Test #9:

score: 5
Accepted
time: 660ms
memory: 34836kb

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:

1928973825

result:

ok single line: '1928973825'

Test #10:

score: 5
Accepted
time: 1466ms
memory: 39508kb

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:

2067331570

result:

ok single line: '2067331570'

Test #11:

score: 5
Accepted
time: 1309ms
memory: 41940kb

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:

2084990799

result:

ok single line: '2084990799'

Test #12:

score: 5
Accepted
time: 1437ms
memory: 40288kb

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:

1890469382

result:

ok single line: '1890469382'

Test #13:

score: 5
Accepted
time: 719ms
memory: 36820kb

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: 5
Accepted
time: 641ms
memory: 33932kb

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:

45620696034

result:

ok single line: '45620696034'

Test #15:

score: 5
Accepted
time: 924ms
memory: 35328kb

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: 817ms
memory: 33588kb

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: 5
Accepted
time: 1252ms
memory: 39020kb

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:

68210040353

result:

ok single line: '68210040353'

Test #18:

score: 5
Accepted
time: 1458ms
memory: 43940kb

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:

71122489923

result:

ok single line: '71122489923'

Test #19:

score: 5
Accepted
time: 2358ms
memory: 40344kb

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: 1470ms
memory: 38288kb

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'