QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#284068#5418. Color the TreeckainWA 15ms8268kbC++142.4kb2023-12-15 22:50:202023-12-15 22:50:21

Judging History

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

  • [2023-12-15 22:50:21]
  • 评测
  • 测评结果:WA
  • 用时:15ms
  • 内存:8268kb
  • [2023-12-15 22:50:20]
  • 提交

answer

#include<bits/stdc++.h>
#define pii pair<int, int>
#define fr first
#define sc second
#define int long long
using namespace std;

inline int rd(){
	int s=0, f=1; char c=getchar();
	while(!isdigit(c)) f^=(c=='-'), c=getchar();
	while(isdigit(c)) s=s*10+c-'0', c=getchar();
	return f? s:-s;
}

void wt(int x, char c=0){
	if(x<0) return putchar('-'), wt(-x, c);
	if(x>9) wt(x/10); putchar(x%10+'0');
	if(c) putchar(c);
}

const int N=1e5+5;

int n, a[N], mxd, anc[N][22], dep[N]={-1}, mx[N];
vector<int> e[N];

int get_lca(int u, int v){
	if(dep[v]<dep[u]) swap(u, v);
	int dif=dep[v]-dep[u];
	for(int i=20; ~i; i--) if(dif>>i&1) u=anc[u][i];
	if(u==v) return u;
	for(int i=20; ~i; i--) if(anc[u][i]!=anc[v][i]) u=anc[u][i], v=anc[v][i];
	return anc[u][0];
}

vector<int> vec[N];
int nxt[N];

namespace ST{

int st[N][22];

void init(){
	for(int i=0; i<n; i++) st[i][0]=a[i];
	for(int j=1; (1<<j)<=n; j++)
		for(int i=0; i+(1<<j)-1<n; i++) st[i][j]=min(st[i][j-1], st[i+(1<<j-1)][j-1]);
}

int que(int l, int r){
	int k=__lg(r-l+1);
	return min(st[l][k], st[r-(1<<k)+1][k]);
}

}

void solve(){
	n=rd();
	for(int i=0; i<n; i++) a[i]=rd();
	for(int i=1, u, v; i<n; i++){
		u=rd(), v=rd();
		e[u].push_back(v);
		e[v].push_back(u);
	}
	//init
	function<void(int, int)> dfs=[&](int u, int fa){
		mx[u]=dep[u]=dep[fa]+1;
		mxd=max(mxd, dep[u]);
		anc[u][0]=fa;
		for(int i=1; (1<<i)<=20; i++) anc[u][i]=anc[anc[u][i-1]][i-1];
		for(int v:e[u]) if(v!=fa) dfs(v, u), mx[u]=max(mx[u], mx[v]);
	};
	dfs(1, 0);
	for(int i=1; i<=n; i++) vec[dep[i]].push_back(i);
	nxt[0]=-1;
	for(int i=1; i<=mxd; i++){
		nxt[i]=0;
		for(int j=0; j+1<vec[i].size(); j++){
			nxt[i]=max(nxt[i], dep[get_lca(vec[i][j], vec[i][j+1])]);
		}
		sort(vec[i].begin(), vec[i].end(), [](int u, int v){
			return mx[u]>mx[v];
		});
	}
	ST::init();
	//

	//calc ans
	int ans=0;
	for(int i=0; i<=mxd; i++){
		int cur=1e18;
		for(int j=i; ~j; j=nxt[j]){
			int l=i-j, r=i-nxt[j]-1;
			int L=0, R=vec[j].size();
			while(R-L>1){
				int mid=(L+R)/2;
				if(mx[vec[j][mid]]>=i) L=mid;
				else R=mid;
			}
			cur=min(cur, ST::que(l, r)*(L+1));
		}
		ans+=cur;
	}
	//

	wt(ans, '\n');

	for(int i=0; i<=n; i++){
		e[i].clear();
		vec[i].clear();
		memset(anc[i], 0, sizeof(anc[i]));
	}
	mxd=0;
}

signed main(){
	int T=rd();
	while(T--){
		solve();
	}
	return 0;
}
/*
1
4
10 15 40 1
1 2
2 3
2 4
*/

详细

Test #1:

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

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: -100
Wrong Answer
time: 15ms
memory: 8268kb

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
174
222
230
156
240
225
126
100
81
155
73
154
129
149
129
228
230
132
191
153
171
78
282
195
286
191
211
119
215
211
233
88
252
239
236
175
180
195
121
109
149
180
175
226
210
182
97
206
59
56
31
115
204
203
172
139
208
53
144
189
171
173
137
233
94
163
273
80
350
156
133
146
159
252
269
138
222...

result:

wrong answer 2nd numbers differ - expected: '168', found: '174'