QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#357062#5418. Color the TreeInexhaustible#WA 14ms18384kbC++143.0kb2024-03-18 17:58:122024-03-18 17:58:12

Judging History

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

  • [2024-03-18 17:58:12]
  • 评测
  • 测评结果:WA
  • 用时:14ms
  • 内存:18384kb
  • [2024-03-18 17:58:12]
  • 提交

answer

#include <set>
#include <map>
#include <list>
#include <queue>
#include <cmath>
#include <time.h>
#include <random>
#include <bitset>
#include <vector>
#include <cstdio>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define mk make_pair
#define fi first
#define se second

inline int read(){
	int t = 0,f = 1;
	register char c = getchar();
	while (c < 48 || c > 57) f = (c == '-') ? (-1) : (f),c = getchar();
	while (c >= 48 && c <= 57) t = (t << 1) + (t << 3) + (c ^ 48),c = getchar();
	return f * t;
}

const int N = 1e5 + 10,LOGN = 17 + 1;
int T,n,idcnt,a[N],id[N],dep[N],mxdep[N],son[N],top[N],fa[N],dwn[N],lst[N];
int mlg[1 << LOGN],mn[LOGN][N];
ll dp[N];

vector <int> G[N];

void init(){
	mlg[0] = -1;
	for (int i = 1;i <= n;i++) mlg[i] = mlg[i >> 1] + 1;
	for (int i = 0;i < n;i++) mn[0][i] = a[i];
	for (int i = 1;i < LOGN;i++){
		for (int j = 0;j < n;j++){
			mn[i][j] = mn[i - 1][j];
			if (j + (1 << (i - 1)) < n) mn[i][j] = min(mn[i][j],mn[i - 1][j + (1 << (i - 1))]);
		}
	}
}

inline int getmn(int l,int r){
	int t = mlg[r - l + 1];
	return min(mn[t][l],mn[t][r - (1 << t) + 1]);
}

void dfs0(int u,int f){
	mxdep[u] = dep[u] = dep[f] + 1,fa[u] = f;
	son[u] = 0;
	for (int v : G[u]){
		if (v == f) continue;
		dfs0(v,u),mxdep[u] = max(mxdep[u],mxdep[v]);
		if (mxdep[v] > mxdep[son[u]]) son[u] = v;
	}
}

void dfs1(int u,int t){
	id[u] = ++idcnt,top[u] = t,dwn[t] = max(dwn[t],id[u]);
	if (son[u]) dfs1(son[u],t);
	for (int v : G[u]){
		if (v == fa[u] || v == son[u]) continue;
		dfs1(v,v);
	}
}

void upd(int u,int d){
	if (lst[u] + 1 > d) return ;
	ll v = getmn(lst[u] + 1,d);
	dp[u] = min(dp[u],v);
	lst[u] = d;
}

void DP(int u){
	for (int v : G[u]){
		if (v == fa[u]) continue;
		DP(v);
	}
	dp[id[u]] = a[0],lst[id[u]] = 0;
	int mxd = 0;
	for (int v : G[u]){
		if (v == fa[u] || v == son[u]) continue;
		mxd = max(mxd,mxdep[v]);
	}
	for (int i = id[u];i <= id[u] + mxd - dep[u];i++){
		upd(i,i - id[u] - 1);
	}
	for (int v : G[u]){
		if (v == fa[u] || v == son[u]) continue;
		for (int i = id[v];i <= dwn[v];i++){
			dp[id[u] + i - id[v] + 1] += dp[i];
		}
	}
	for (int i = id[u];i <= id[u] + mxd - dep[u];i++){
		upd(i,i - id[u]);
	}
}

void solve(){
	n = read();
	for (int i = 0;i < n;i++) a[i] = read();
	init();
	for (int i = 1;i <= n;i++) G[i].clear(),dwn[i] = 0;
	for (int i = 1;i < n;i++){
		int u = read(),v = read();
		G[u].emplace_back(v),G[v].emplace_back(u);
	}
	idcnt = 0;
	dfs0(1,0);
	dfs1(1,1);
	DP(1);
	ll ans = 0;
	for (int i = id[1];i <= dwn[1];i++){
		upd(i,i - id[1]);
		ans += dp[i];
	}
	printf("%lld\n",ans);
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	// freopen("out.out","w",stdout);
#endif
	T = read();
	while (T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 17332kb

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: 14ms
memory: 18384kb

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
177
222
230
156
255
225
126
100
81
172
73
173
127
160
149
228
230
132
217
155
171
78
282
195
286
204
211
119
198
211
239
88
252
239
236
175
180
197
145
109
148
180
175
226
210
182
109
199
59
56
31
115
220
203
172
155
226
53
158
189
170
173
137
233
107
163
273
80
350
156
133
153
159
252
269
148
2...

result:

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