QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865112#5418. Color the Treejjh20100730WA 90ms38652kbC++141.9kb2025-01-21 14:59:352025-01-21 14:59:36

Judging History

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

  • [2025-01-21 14:59:36]
  • 评测
  • 测评结果:WA
  • 用时:90ms
  • 内存:38652kb
  • [2025-01-21 14:59:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
vector<int> G[N],g[N],D[N];
int T,n,st[25][N],dfn[N],cnt=0,d[N],fa[N][25],x[N],dp[N];
void dfs1(int u,int pre,int dep) {
	D[dep].push_back(u);
	dfn[u]=++cnt,d[u]=dep,fa[u][0]=pre;
	for(int i=1; i<=20; i++) fa[u][i]=fa[fa[u][i-1]][i-1];
	for(int v:G[u]) {
		if(v==pre) continue;
		dfs1(v,u,dep+1);
	}
}
int lca(int u,int v) {
	if(d[u]<d[v]) swap(u,v);
	for(int i=20; i>=0; i--) if(d[fa[u][i]]>=d[v]) u=fa[u][i];
	if(u==v) return u;
	for(int i=20; i>=0; i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
	return fa[u][0];
}
int calc(int l,int r) {
	int tmp=__lg(r-l+1);
	return min(st[tmp][l],st[tmp][r-(1<<tmp)+1]);
}
bool cmp(int a,int b) {return dfn[a]<dfn[b];}
void dfs2(int u,int pre,int dep) {
	if(g[u].empty()) {
		dp[u]=calc(dep-d[u]+1,dep-d[pre]);
		return;
	}
	dp[u]=0;
	for(int v:g[u]) {
		if(v==pre) continue;
		dfs2(v,u,dep);
		dp[u]+=dp[v];
	}
	dp[u]=min(dp[u],calc(dep-d[u]+1,dep-d[pre]));
}
void work() {
	cin>>n;
	for(int i=1; i<=n; i++) cin>>st[0][i];
	for(int i=1; (1<<i)<=n; i++) for(int j=1; j+(1<<i)-1<=n; j++) st[i][j]=min(st[i-1][j],st[i-1][j+(1<<i-1)]);
	for(int i=1; i<n; i++) {
		int u,v;
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	cnt=0;
	dfs1(1,0,1);
	int ans=0;
	for(int i=1; i<=n; i++) {
		if(D[i].empty()) break;
		int tot=0;
		for(int j:D[i]) x[++tot]=j;
		x[++tot]=1;
		sort(x+1,x+tot+1,cmp);
		int tmp=tot;
		for(int i=1; i<tmp; i++) x[++tot]=lca(x[i],x[i+1]);
		sort(x+1,x+tot+1);
		int len=unique(x+1,x+tot+1)-x-1;
		for(int i=1; i<len; i++) g[lca(x[i],x[i+1])].push_back(x[i+1]);
		dfs2(1,0,i);
		ans+=dp[1];
		for(int i=1; i<len; i++) g[lca(x[i],x[i+1])].clear();
	}
	cout<<ans<<"\n";
	for(int i=1; i<=n; i++) G[i].clear(),D[i].clear();
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	cin>>T;
	while(T--) work();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 36648kb

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: 90ms
memory: 38652kb

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:

185
174
222
230
156
255
225
126
100
81
163
73
159
148
155
144
228
230
140
195
153
171
78
282
195
286
191
211
121
222
211
252
88
252
239
244
175
180
195
126
109
148
180
188
226
210
182
97
234
59
56
31
115
224
203
176
155
213
53
158
189
187
173
137
233
100
163
273
80
350
156
133
184
159
252
269
157
22...

result:

wrong answer 1st numbers differ - expected: '180', found: '185'