QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#892045#10082. Adrian the Wonder Child致远星 (Penghao Zhai, Qiwen Xu, Xubei Zhong)#AC ✓666ms42892kbC++234.1kb2025-02-09 21:23:022025-02-09 21:23:07

Judging History

This is the latest submission verdict.

  • [2025-02-09 21:23:07]
  • Judged
  • Verdict: AC
  • Time: 666ms
  • Memory: 42892kb
  • [2025-02-09 21:23:02]
  • Submitted

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
int n,k,m;
vector<pii> G[200200];
int ans;
vector<int> pool;
int siz[200200],mx[200200];
int ban[200200];
void dfs1(int u,int fa)
{
	pool.pb(u);
	siz[u]=1;
	mx[u]=0;
	for(auto pr:G[u])
		if(!ban[pr.first]&&pr.first!=fa)
		{
			dfs1(pr.first,u);
			siz[u]+=siz[pr.first];
			mx[u]=max(mx[u],siz[pr.first]);
		}
}
int sub[200200],rem[200200],cost[200200],val[200200],color[200200];
void dfs2(int u,int fa,int co,int len,int col,int rep,int flag,int rm,int d,int stc)
{
	sub[u]=rep;
	rem[u]=(flag?len%(k+1):rm);
	cost[u]=co+len/(k+1);
	val[u]=d;
	color[u]=stc;
	for(auto pr:G[u])
	{
		int v=pr.first;
		int w=pr.second;
		if(v==fa||ban[v]) continue;
		if(w==col)
			dfs2(v,u,co,len+1,col,rep,flag,rm,d+1,stc);
		else if(flag)
			dfs2(v,u,co+len/(k+1),1,col^1,rep,0,len%(k+1),d+1,stc);
		else
			dfs2(v,u,co+len/(k+1),1,col^1,rep,flag,rm,d+1,stc);
	}
}
void go(int x)
{
	pool.clear();
	dfs1(x,0);
	int p=x;
	for(auto p2:pool)
		if(max(mx[p2],siz[x]-siz[p2])<max(mx[p],siz[x]-siz[p]))
			p=p2;
	x=p;
	for(auto pr:G[x])
		if(!ban[pr.first])
			dfs2(pr.first,x,0,1,pr.second,pr.first,1,-1,1,pr.second);
	for(auto u:pool)
		if(u!=x)
			if(cost[u]<=m)
				ans=max(ans,val[u]);
	vector<array<int,4>> v[2];
	for(auto u:pool)
		if(u!=x)
			v[color[u]].push_back({cost[u],val[u],sub[u],u});
	srt(v[0]);
	rsrt(v[1]);
	int val1=-1,val2=-1;
	int rep1=-1,rep2=-1;
	p=0;
	auto add=[&](int val,int rep)
	{
		if(rep1==rep)
			val1=max(val1,val);
		else if(rep2==rep)
		{
			val2=max(val2,val);
			if(val2>val1)
			{
				swap(val1,val2);
				swap(rep1,rep2);
			}
		}
		else
		{
			if(val>val1)
			{
				swap(val,val1);
				swap(rep,rep1);
			}
			if(val>val2)
			{
				swap(val,val2);
				swap(rep,rep2);
			}
		}
	};
	for(auto arr:v[1])
	{
		while(p<sz(v[0])&&v[0][p][0]+arr[0]<=m)
		{
			add(v[0][p][1],v[0][p][2]);
			p++;
		}
		if(arr[2]!=rep1&&~rep1) ans=max(ans,val1+arr[1]);
		if(arr[2]!=rep2&&~rep2) ans=max(ans,val2+arr[1]);
	}
	rev(v[1]);
	for(int c=0;c<2;c++)
	{
		val1=val2=rep1=rep2=-1;
		p=0;
		for(int i=sz(v[c])-1;i>=0;i--)
		{
			while(p<sz(v[c])&&v[c][p][0]+v[c][i][0]<m)
			{
				add(v[c][p][1],v[c][p][2]);
				p++;
			}
			if(v[c][i][2]!=rep1&&~rep1) ans=max(ans,val1+v[c][i][1]);
			if(v[c][i][2]!=rep2&&~rep2) ans=max(ans,val2+v[c][i][1]);
		}
	}
	for(int c=0;c<2;c++)
	{
		p=0;
		for(int i=sz(v[c])-1;i>=0;i--)
		{
			while(p<sz(v[c])&&v[c][p][0]+v[c][i][0]<m)
				p++;
			int j=i;
			while(j&&v[c][j-1][0]==v[c][j][0]) j--;
			if(p==sz(v[c])||v[c][p][0]+v[c][i][0]!=m)
			{
				i=j;
				continue;
			}
			val1=val2=rep1=rep2=-1;
			vector<int> v1,v2;
			for(int x=j;x<=i;x++) v1.pb(v[c][x][3]);
			int p2=p;
			while(p2<sz(v[c])-1&&v[c][p2+1][0]==v[c][p2][0]) p2++;
			for(int x=p;x<=p2;x++) v2.pb(v[c][x][3]);
			sort(ALL(v1),[&](int A,int B){return rem[A]<rem[B];});
			sort(ALL(v2),[&](int A,int B){return rem[A]>rem[B];});
			int po=0;
			for(auto ind:v2)
			{
				while(po<sz(v1)&&rem[v1[po]]+rem[ind]<=k)
				{
					add(val[v1[po]],sub[v1[po]]);
					po++;
				}
				if(sub[ind]!=rep1&&~rep1) ans=max(ans,val1+val[ind]);
				if(sub[ind]!=rep2&&~rep2) ans=max(ans,val2+val[ind]);
			}
			i=j;
		}
	}
	ban[x]=1;
	for(auto pr:G[x])
		if(!ban[pr.first])
			go(pr.first);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>k>>m;
	for(int i=1;i<n;i++)
	{
		int u,v,w;
		cin>>u>>v>>w;
		G[u].pb(v,w);
		G[v].pb(u,w);
	}
	go(1);
	cout<<ans<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

7

result:

ok 1 number(s): "7"

Test #2:

score: 0
Accepted
time: 0ms
memory: 7796kb

input:

30 2 3
23 10 0
29 26 0
3 30 0
7 9 0
11 12 0
27 21 0
5 9 0
29 18 0
22 14 0
3 24 0
1 19 0
15 25 0
1 17 0
21 2 0
2 30 0
16 12 0
27 17 0
6 20 0
28 10 0
16 4 0
14 11 0
13 16 0
16 19 0
28 7 0
8 14 0
26 20 0
25 4 0
15 5 0
8 18 0

output:

11

result:

ok 1 number(s): "11"

Test #3:

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

input:

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

output:

28

result:

ok 1 number(s): "28"

Test #4:

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

input:

200 3 15
27 152 0
3 46 0
111 63 0
121 55 0
12 142 0
45 48 0
191 13 1
99 104 0
183 165 0
5 23 0
115 87 0
162 63 0
112 80 0
68 19 0
87 103 0
194 107 0
14 84 0
71 187 0
177 75 0
123 185 0
33 116 0
32 119 0
185 35 0
142 10 0
189 128 0
104 44 0
50 151 0
110 29 0
117 49 0
41 4 0
100 93 0
40 114 0
151 108 ...

output:

64

result:

ok 1 number(s): "64"

Test #5:

score: 0
Accepted
time: 0ms
memory: 7936kb

input:

300 7 7
155 249 1
150 129 1
187 213 1
290 61 1
226 69 1
137 142 1
217 46 1
189 95 1
178 65 1
37 26 1
297 227 1
219 195 1
118 38 1
269 267 1
283 196 1
223 175 1
27 287 1
232 273 0
194 185 1
223 26 1
196 48 1
296 237 1
88 117 1
11 31 1
238 109 1
278 188 1
8 3 1
286 28 1
149 93 1
71 34 1
99 1 1
17 64 1...

output:

87

result:

ok 1 number(s): "87"

Test #6:

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

input:

5000 8 7
1528 4768 0
4183 4420 0
1373 958 0
4916 1373 0
1128 1373 0
1523 642 0
1373 4345 0
2805 4151 0
3439 1386 0
2198 679 0
3958 2913 0
2505 3558 0
542 3125 0
2062 1373 0
499 2974 0
2663 4797 0
1638 1004 0
629 1596 0
2641 4658 0
1373 4512 0
1688 1351 0
4596 1626 0
1373 1769 0
4997 4981 0
153 2892 ...

output:

110

result:

ok 1 number(s): "110"

Test #7:

score: 0
Accepted
time: 4ms
memory: 8376kb

input:

5000 16 2
3669 2074 0
1305 2409 0
4741 2242 0
185 1853 0
3105 4678 0
3081 4081 0
2120 2192 0
3669 3794 0
4280 3214 0
1784 4194 0
2718 3604 0
2127 4587 0
146 3477 0
88 4856 0
3505 953 0
3513 1190 0
527 4083 0
2422 21 0
1173 398 0
2158 8 0
1044 3050 0
3162 163 0
4061 4315 0
862 1616 0
3669 181 0
531 1...

output:

89

result:

ok 1 number(s): "89"

Test #8:

score: 0
Accepted
time: 5ms
memory: 8524kb

input:

5000 500 2
424 3691 1
2472 4378 1
2787 424 1
2963 424 1
605 3339 1
4294 1096 1
4585 3488 1
2451 4517 1
4895 1200 1
4063 3339 1
589 424 1
578 1198 1
1954 1946 1
2794 2012 1
3144 1159 1
4268 2369 1
2747 1825 1
1287 3565 1
4157 2215 1
806 2681 1
4486 424 1
2915 3705 1
2515 3747 1
3422 2220 1
4288 573 1...

output:

1504

result:

ok 1 number(s): "1504"

Test #9:

score: 0
Accepted
time: 5ms
memory: 6480kb

input:

5000 2 500
688 2422 0
754 1445 0
4867 4956 0
612 4128 0
57 959 0
2032 4469 0
3073 3490 0
4769 722 0
2071 1556 0
2515 2033 0
467 4850 0
2970 2015 0
1359 4442 0
2056 1820 0
3022 2906 1
821 373 0
4544 2068 0
2395 1052 0
4510 427 0
3170 3542 0
1792 3739 0
1381 1915 0
2071 3074 0
2905 4657 0
2122 621 0
3...

output:

1657

result:

ok 1 number(s): "1657"

Test #10:

score: 0
Accepted
time: 89ms
memory: 15736kb

input:

70000 17 3
66457 65426 0
44082 49846 0
4775 6733 0
48020 56143 0
49749 19263 0
15655 49101 0
11303 67053 0
24390 57491 0
25792 10529 0
3061 61644 0
28324 13500 0
34005 38905 0
49984 66775 0
42135 53750 0
27685 36239 0
60685 18083 0
8596 59795 0
59007 22262 0
60966 45716 0
50149 22788 0
34976 39845 0...

output:

377

result:

ok 1 number(s): "377"

Test #11:

score: 0
Accepted
time: 70ms
memory: 14364kb

input:

70000 2 10
40367 50440 0
9371 58663 0
42270 37733 0
45184 73 0
53225 32563 0
12158 2205 0
55795 6833 0
25624 23179 0
29679 25853 0
22145 28218 0
30172 4772 0
57370 28433 0
17107 36304 0
608 60034 0
24040 54843 0
13717 27778 0
1975 42025 0
49923 38386 0
2303 54965 0
55063 41917 0
43480 10448 0
39673 ...

output:

63

result:

ok 1 number(s): "63"

Test #12:

score: 0
Accepted
time: 180ms
memory: 18668kb

input:

70000 10000 2
16910 21547 1
66588 51893 1
28236 61736 1
57642 26604 1
46870 15501 1
4189 21016 1
5731 63345 1
30437 69143 1
6445 47816 1
24586 1854 1
10400 15652 1
40906 42117 1
36346 35288 1
54090 66234 1
37412 41097 1
31390 30127 1
52437 22078 1
60664 26175 1
61124 23831 1
4045 20786 1
35927 5830 ...

output:

45025

result:

ok 1 number(s): "45025"

Test #13:

score: 0
Accepted
time: 149ms
memory: 19660kb

input:

70000 3 8000
6149 68762 1
4065 62079 1
63303 49265 1
5481 47973 1
16347 19856 1
20451 39510 1
8890 10583 1
69819 56443 1
42127 14895 1
8489 29500 1
2540 35077 1
46348 13615 1
9475 35091 1
12918 38434 1
6407 33814 1
20116 53089 1
39453 19708 1
58265 68082 1
10484 54935 1
22835 36090 1
47609 40953 1
1...

output:

32844

result:

ok 1 number(s): "32844"

Test #14:

score: 0
Accepted
time: 315ms
memory: 28060kb

input:

199000 100 0
93940 159091 1
129441 47385 1
57968 102499 1
23604 75965 1
29131 106782 1
88876 44085 1
70636 3289 1
188465 155369 1
72921 43931 1
191690 194909 1
105432 32221 1
186330 118874 1
2647 117155 1
124235 137066 1
30268 95485 1
124909 128713 1
28376 39689 1
90960 169337 1
72952 103036 1
77923...

output:

394

result:

ok 1 number(s): "394"

Test #15:

score: 0
Accepted
time: 309ms
memory: 28152kb

input:

199000 17 0
139098 150518 1
191349 183234 1
149533 140660 1
105768 112225 1
118975 156380 1
166536 137299 1
18185 194346 1
131721 120972 1
20758 134043 1
86031 129887 1
71568 79657 1
197170 98216 1
87595 28526 1
64512 25702 1
126426 30762 1
3512 152431 1
175044 177494 1
33536 44438 1
112239 61650 1
...

output:

140

result:

ok 1 number(s): "140"

Test #16:

score: 0
Accepted
time: 666ms
memory: 38732kb

input:

199000 50000 0
158194 83366 1
130822 34360 1
178799 125690 1
130822 26014 1
152166 111320 1
24888 12072 1
130822 65217 1
50740 79676 1
188594 92464 1
60555 17069 1
9854 193300 1
130822 150558 1
95062 96942 1
72459 188871 1
33661 107620 1
193797 130822 1
179139 13789 1
108723 132269 1
130822 9587 1
1...

output:

170000

result:

ok 1 number(s): "170000"

Test #17:

score: 0
Accepted
time: 280ms
memory: 31068kb

input:

199000 2400 0
45353 11287 0
45353 46364 0
45353 19640 0
155198 32317 0
6879 129242 0
154132 45353 0
45353 172433 0
10895 45353 0
13050 45353 0
45353 57475 0
15728 130510 0
85431 39930 0
45353 75888 0
129284 177387 0
85037 21132 0
65164 45353 0
147969 45353 0
32541 153095 0
196719 45353 0
29911 65397...

output:

22472

result:

ok 1 number(s): "22472"

Test #18:

score: 0
Accepted
time: 366ms
memory: 31080kb

input:

199000 56 0
22304 148692 0
77407 108013 0
91274 113851 0
179385 91274 1
74931 91274 0
12560 91274 0
142264 82976 1
125866 91274 0
91274 47232 0
159428 191414 0
104021 91274 1
48659 118750 0
123492 82924 0
143869 11102 0
166570 24204 1
91274 20180 0
149702 81152 0
111268 75377 0
153689 91274 1
126861...

output:

17428

result:

ok 1 number(s): "17428"

Test #19:

score: 0
Accepted
time: 223ms
memory: 27724kb

input:

200000 80 1
116384 21730 1
15274 15259 1
122903 52712 1
54410 81011 1
154274 100907 1
157687 18350 1
155221 26207 1
171875 171003 1
79341 189510 1
152560 192143 1
39888 130160 1
18886 72878 1
20907 3450 1
127864 150904 1
168030 42772 1
48639 60942 1
162454 55231 1
65646 102108 1
176708 51529 1
55958...

output:

369

result:

ok 1 number(s): "369"

Test #20:

score: 0
Accepted
time: 268ms
memory: 27788kb

input:

200000 10 4
35705 53898 1
115740 97680 1
64070 138794 1
198017 27804 1
124503 88807 1
112559 107150 1
111768 46187 1
94155 83380 1
23142 87881 1
140183 98787 1
86901 178832 1
1844 171858 1
28544 55504 1
81863 88540 1
170118 12149 1
69160 194669 1
92703 18300 1
160972 156169 1
44787 73653 1
176801 16...

output:

116

result:

ok 1 number(s): "116"

Test #21:

score: 0
Accepted
time: 610ms
memory: 41304kb

input:

200000 10000 1
56426 99262 0
21854 52128 0
66107 129362 0
9999 182486 0
127369 118372 0
91977 19737 0
61630 105319 0
49088 74252 0
99970 41436 0
99974 129980 0
149383 130998 0
11183 41953 0
198216 17324 0
69403 197214 0
86145 137967 0
77643 128156 0
193172 105349 0
18918 171007 0
105645 58460 0
1634...

output:

148560

result:

ok 1 number(s): "148560"

Test #22:

score: 0
Accepted
time: 612ms
memory: 42892kb

input:

200000 3 30000
91595 145753 0
28683 125986 0
149444 30888 0
57603 77843 0
142254 145753 0
150851 37457 0
79198 166337 0
149086 153825 0
92264 15613 0
140716 6634 0
88635 159793 0
88559 148791 0
130201 109390 0
103751 127612 0
114298 59399 0
122227 164126 0
90748 1708 0
43315 150998 0
53554 35250 0
7...

output:

123117

result:

ok 1 number(s): "123117"

Test #23:

score: 0
Accepted
time: 201ms
memory: 34104kb

input:

200000 20 500
46294 45496 0
191276 199138 0
159860 166506 0
102792 191276 0
191276 83325 0
22257 68750 0
185401 103445 0
47385 191276 0
50591 31419 0
27414 184382 0
191276 67062 0
191276 93569 0
191276 23973 0
191276 171514 0
12215 191276 0
30015 132172 0
191276 152726 0
131756 191276 0
191276 10723...

output:

43153

result:

ok 1 number(s): "43153"

Test #24:

score: 0
Accepted
time: 241ms
memory: 30476kb

input:

200000 45 900
76698 8660 0
86283 8660 0
8660 154469 0
110072 15622 0
30997 93592 0
195911 8660 0
118544 148537 0
184649 61480 0
161469 61022 0
198024 8660 0
150869 8660 0
13013 8660 0
8660 163824 0
136133 90213 0
168217 8660 0
8660 45422 0
8660 125139 0
105705 27713 0
30248 104987 0
130899 6819 0
18...

output:

42649

result:

ok 1 number(s): "42649"

Test #25:

score: 0
Accepted
time: 0ms
memory: 7916kb

input:

3 2 2
1 2 0
2 3 0

output:

2

result:

ok 1 number(s): "2"

Test #26:

score: 0
Accepted
time: 492ms
memory: 40708kb

input:

200000 199999 199999
52616 188652 0
188652 80309 0
80309 116551 0
116551 25621 0
25621 37235 0
37235 46899 0
46899 42325 0
42325 65417 0
65417 126636 0
126636 14075 0
14075 193014 0
193014 63023 0
63023 149571 0
149571 63548 0
63548 133432 0
133432 91281 0
91281 92417 0
92417 15983 0
15983 194643 0
...

output:

199999

result:

ok 1 number(s): "199999"

Test #27:

score: 0
Accepted
time: 491ms
memory: 41404kb

input:

200000 2 199999
78811 22127 0
22127 65337 0
65337 182957 0
182957 157210 0
157210 110503 0
110503 80929 0
80929 145374 0
145374 169057 0
169057 187204 0
187204 35028 0
35028 127010 0
127010 46652 0
46652 97858 0
97858 108871 0
108871 145719 0
145719 162199 0
162199 42629 0
42629 175263 0
175263 3975...

output:

199999

result:

ok 1 number(s): "199999"

Test #28:

score: 0
Accepted
time: 486ms
memory: 40900kb

input:

200000 2 59999
89024 172075 0
172075 7850 0
7850 181921 0
181921 5284 0
5284 112341 0
112341 36541 0
36541 22599 0
22599 151921 0
151921 87023 0
87023 83244 0
83244 144513 0
144513 58138 0
58138 160510 0
160510 113424 0
113424 69971 0
69971 70548 0
70548 146708 0
146708 26665 0
26665 23345 0
23345 6...

output:

179999

result:

ok 1 number(s): "179999"

Test #29:

score: 0
Accepted
time: 480ms
memory: 39844kb

input:

200000 2 99999
129265 38722 0
38722 153452 0
153452 153515 0
153515 177925 0
177925 127088 0
127088 152925 0
152925 157176 0
157176 76041 0
76041 191305 0
191305 153830 0
153830 135783 0
135783 150666 0
150666 31787 0
31787 114769 0
114769 147400 0
147400 88304 0
88304 74246 0
74246 13466 0
13466 15...

output:

199999

result:

ok 1 number(s): "199999"