QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#864857#5418. Color the TreeyinheeWA 67ms18424kbC++173.0kb2025-01-21 09:58:302025-01-21 09:58:30

Judging History

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

  • [2025-01-21 09:58:30]
  • 评测
  • 测评结果:WA
  • 用时:67ms
  • 内存:18424kb
  • [2025-01-21 09:58:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define mems(x,y) memset(x,y,sizeof x)
#define Mp make_pair
#define eb emplace_back
#define gc getchar
#define pc putchar
#define fi first
#define se second
#define il inline
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define drep(i,a,b) for(int i=(a);i>=(b);i--)
#define go(i,u) for(int i=head[u];i;i=e[i].nxt)
	typedef long long ll;
	typedef pair<int,int> pii;
	template<typename T>
	il void read(T &x){
		int f=1;x=0;char c=gc();
		while(c<'0'||c>'9'){
			if(c=='-'){
				f=-1;
			}
			c=gc();
		}
		while(c>='0'&&c<='9'){
			x=x*10+c-48,c=gc();
		}
		x*=f;
	}
	template<typename T,typename ...Args>
	void read(T &x,Args &...args){
		read(x),read(args...);
	}
	template<typename T>
	il void write(T x){
		char buf[43];int len=0;
		if(x<0){
			pc('-'),x=-x;
		}
		do{
			buf[++len]=x%10,x/=10;
		}while(x);
		while(len){
			pc(buf[len--]+'0');
		}
	}
}
using namespace my_std;
const int N=1e5+7,M=-1,inf=0x3f3f3f3f,mod=0;
int n,m,D,a[N],cur,dfn[N],dep[N],fa[N][17],dp[N];
vector<int> g[N];
int tot,head[N];
struct node{
	int to,nxt;
}e[N<<1];
il void add(int u,int v){
	e[++tot]={v,head[u]},head[u]=tot;
}
struct STable{
	int st[17][N];
	void init(){
		rep(i,1,n){
			st[0][i]=a[i];
		}
		rep(i,1,__lg(n)){
			rep(j,1,n-(1<<i)+1){
				st[i][j]=min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
			}
		}
	}
	il int qry(int l,int r){
		int k=__lg(r-l+1);
		return min(st[k][l],st[k][r-(1<<k)+1]);
	}
}ST;
void dfs1(int u,int f){
	dfn[u]=++cur;
	g[dep[u]=dep[f]+1].eb(u);
	fa[u][0]=f;
	rep(i,1,16){
		fa[u][i]=fa[fa[u][i-1]][i-1];
	}
	go(i,u){
		int v=e[i].to;
		if(v==f){
			continue;
		}
		dfs1(v,u);
	}
}
void dfs2(int u,int f){
	go(i,u){
		int v=e[i].to;
		dfs2(v,u);
		dp[u]+=dp[v];
	}
	if(!dp[u]){
		dp[u]=inf;
	}
	dp[u]=min(dp[u],ST.qry(D-dep[u]+1,D-dep[f]));
}
il int getLca(int u,int v){
	if(dep[u]<dep[v]){
		swap(u,v);
	}
	drep(i,16,0){
		if(dep[fa[u][i]]>=dep[v]){
			u=fa[u][i];
		}
	}
	if(u==v){
		return u;
	}
	drep(i,16,0){
		if(fa[u][i]!=fa[v][i]){
			u=fa[u][i],v=fa[v][i];
		}
	}
	return fa[u][0];
}
il bool cmp(int x,int y){
	return dfn[x]<dfn[y];
}
int solve(vector<int> f){
	sort(f.begin(),f.end(),cmp);
	int k=f.size()-1;
	rep(i,1,k){
		f.eb(getLca(f[i-1],f[i]));
	}
	sort(f.begin(),f.end(),cmp);
	f.erase(unique(f.begin(),f.end()),f.end());
	rep(i,1,(int)f.size()-1){
		add(getLca(f[i-1],f[i]),f[i]);
	}
	dfs2(1,0),tot=0;
	int ret=dp[1];
	for(int i:f){
		head[i]=dp[i]=0;
	}
	return ret;
}
void Yorushika(){
	read(n),tot=cur=0;
	rep(i,1,n){
		read(a[i]);
		head[i]=0,g[i].clear();
	}
	ST.init();
	rep(i,1,n-1){
		int u,v;read(u,v);
		add(u,v),add(v,u);
	}
	dfs1(1,0),tot=0;
	rep(i,1,n){
		head[i]=0;
	}
	ll ans=0;
	rep(i,1,n){
		if(!g[i].size()){
			break;
		}
		if(i>1){
			g[i].eb(1);
		}
		D=i,ans+=solve(g[i]);
	}
	printf("%lld\n",ans);
}
signed main(){
	int t=1;
	read(t);
	while(t--){
		Yorushika();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
4
10 15 40 1
1 2
2 3
2 4
5
10 5 1 100 1000
1 2
2 3
2 4
4 5
4
1000 200 10 8
1 2
2 3
3 4

output:

35
17
1218

result:

ok 3 number(s): "35 17 1218"

Test #2:

score: 0
Accepted
time: 48ms
memory: 11996kb

input:

3000
54
43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44
1 2
1 3
2 4
3 5
4 6
2 7
4 8
1 9
3 10
1 11
8 12
2 13
9 14
1 15
1 16
15 17
15 18
7 19
1 20
19 21
13 22
10 23
17 24
9 25
9 26
24 27
8 28...

output:

180
168
222
230
156
240
225
126
100
81
155
73
154
127
149
124
228
230
132
187
153
170
78
282
195
286
191
211
119
197
211
233
88
252
239
233
173
180
195
121
109
148
180
175
226
210
182
97
199
59
56
31
115
204
203
172
139
208
53
140
189
170
173
137
233
94
163
273
80
350
156
133
146
159
240
269
137
222...

result:

ok 3000 numbers

Test #3:

score: 0
Accepted
time: 55ms
memory: 17972kb

input:

300
474
5 24 21 41 15 23 43 48 32 19 27 40 10 49 40 6 18 41 43 31 45 18 35 36 12 10 23 45 28 23 14 43 37 45 12 16 20 17 49 13 22 8 30 19 27 40 22 14 30 47 16 39 25 48 21 26 50 8 14 26 9 30 41 15 44 24 16 46 50 39 25 47 24 45 21 18 26 21 5 39 15 10 47 48 11 44 44 33 23 14 35 39 35 30 38 9 13 15 39 5 ...

output:

329
183
264
219
323
220
348
342
410
395
80
201
299
144
207
408
360
215
283
104
320
394
277
210
273
285
242
253
265
379
360
322
202
351
195
196
266
270
171
342
239
283
286
300
331
317
345
268
173
296
275
224
480
330
264
162
199
378
254
214
231
293
229
259
241
268
380
419
233
185
364
341
328
237
320
3...

result:

ok 300 numbers

Test #4:

score: 0
Accepted
time: 67ms
memory: 18424kb

input:

30
4926
18 13 47 7 21 39 28 48 21 44 14 18 39 13 46 33 6 49 9 7 10 29 29 25 38 15 16 42 41 41 40 14 26 13 6 19 17 31 24 18 30 24 48 46 38 21 28 42 29 50 33 28 18 26 18 42 13 23 35 20 32 6 17 25 44 46 35 36 50 24 7 29 34 14 41 41 8 33 22 46 7 6 22 40 24 8 44 36 38 13 37 37 25 22 7 43 50 33 19 44 24 4...

output:

427
520
443
359
427
408
371
415
482
436
269
530
478
485
435
453
418
503
443
453
405
425
347
564
297
435
559
453
213
395

result:

ok 30 numbers

Test #5:

score: -100
Wrong Answer
time: 50ms
memory: 12000kb

input:

3000
74
555233197 518812085 998593787 821753058 967306600 663279435 696954042 885219300 489323226 146136486 447383587 785317885 838306349 708275482 715157265 298848995 400280904 374077023 673881523 207667786 885945020 459791675 992519779 327830583 821713695 253985403 926395863 419409783 138881726 80...

output:

1144663020
5155836197
-3080619068
2562751093
2761702533
-2648835600
2695865551
2999820393
728392127
-1794503022
1391052597
1263778743
1249231074
-46469263
-2491463648
2287799528
-939686293
943597727
2314239676
2236789542
2766114996
-1989066488
-784152919
107658008
3146896433
1500707255
312638823
-37...

result:

wrong answer 1st numbers differ - expected: '6742611216', found: '1144663020'