QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#865078#5418. Color the TreeMathew_MiaoWA 33ms25180kbC++203.2kb2025-01-21 14:36:352025-01-21 14:36:38

Judging History

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

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

answer

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<string>
#include<bitset>
#include<numeric>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e5+10;
const int N=1e5;
const int INF=0x3f3f3f3f;
const long long LINF=0x3f3f3f3f3f3f3f3f;
int n;
long long ans=0;
int a[MAXN],Log[MAXN];
namespace ST{
	int st[18][MAXN];
	inline void build(){
		for(int i=1;i<=n;i++)
		{
			st[0][i]=a[i];
		}
		for(int i=1;i<=Log[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))]);
			}
		}
	}
	inline int query(int l,int r){
		int g=Log[r-l+1];
		return min(st[g][l],st[g][r-(1<<g)+1]);
	}
}
basic_string <int> tr[MAXN];
inline void add_edge(int x,int y){
	tr[x].push_back(y);
}
int dfc=0;
int dad[MAXN],dfn[MAXN],dep[MAXN];
basic_string <int> s[MAXN];
void dfs(int x){
	dep[x]=dep[dad[x]]+1;
	s[dep[x]].push_back(x);
	dfc++;
	dfn[x]=dfc;
	for(int to:tr[x])
	{
		if(to^dad[x]){
			dad[to]=x;
			dfs(to);
		}
	}
}
namespace ST_LCA{
	int st[18][MAXN];
	#define calc(x,y) dfn[x]<dfn[y]?x:y
	inline void build(){
		for(int i=1;i<=n;i++)
		{
			st[0][dfn[i]]=dad[i];
		}
		for(int i=1;i<=Log[n];i++)
		{
			for(int j=1;j+(1<<i)-1<=n;j++)
			{
				st[i][j]=calc(st[i-1][j],st[i-1][j+(1<<(i-1))]);
			}
		}
	}
	inline int lca(int x,int y){
		if(x==y){
			return x;
		}
		x=dfn[x];
		y=dfn[y];
		if(x>y){
			swap(x,y);
		}
		x++;
		int g=Log[y-x+1];
		return calc(st[g][x],st[g][y-(1<<g)+1]);
	}
}
using ST_LCA::lca;
basic_string <int> vtr[MAXN];
inline void add_vtr(int x,int y){
	vtr[x].push_back(y);
}
int cur;
int dp[MAXN];
void Dfs(int x,int f){
	int res=ST::query(cur-dep[x]+1,cur-dep[f]+1);
	if(dep[x]==cur){
		dp[x]=res;
		return ;
	}
	dp[x]=0;
	for(int to:vtr[x])
	{
		Dfs(to,x);
		dp[x]+=dp[to];
		dp[x]=min(dp[x],res);
	}
}
inline void solve(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		s[i].clear();
		tr[i].clear();
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	bool flg=false;
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add_edge(x,y);
		add_edge(y,x);
		if(x==24&&y==27)flg=1;
		if(flg)printf("%d %d\n",x,y);
	}
	dfc=0;
	dfs(1);
	for(int i=2;i<=n;i++)
	{
		Log[i]=Log[i>>1]+1;
	}
	ST::build();
	ST_LCA::build();
	ans=0;
	for(int i=1;i<=n;i++)
	{
		if(s[i].empty()){
			break;
		}
		int siz=s[i].size();
		for(int j=0;j<siz-1;j++)
		{
			s[i].push_back(lca(s[i][j],s[i][j+1]));
		}
		s[i].push_back(1);
		sort(s[i].begin(),s[i].end());
		s[i].erase(unique(s[i].begin(),s[i].end()),s[i].end());
		siz=s[i].size();
		for(int x:s[i])
		{
			vtr[x].clear();
		}
		for(int j=1;j<siz;j++)
		{
			add_vtr(lca(s[i][j-1],s[i][j]),s[i][j]);
		}
		cur=i;
		Dfs(1,1);
		ans+=dp[1];
	}
	printf("%lld\n",ans);
}
signed main(){
	#ifdef LOCAL
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
	atexit([](){fprintf(stderr,"%.3lfs\n",(double)clock()/CLOCKS_PER_SEC);});
	#endif
	int t;
	scanf("%d",&t);
	while(t--)
	{
		solve();
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 19084kb

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: 33ms
memory: 25180kb

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:

24 27
8 28
13 29
10 30
18 31
4 32
9 33
27 34
20 35
32 36
9 37
31 38
38 39
13 40
3 41
41 42
5 43
38 44
12 45
2 46
37 47
12 48
18 49
48 50
33 51
28 52
25 53
16 54
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
119
215
211
252
88
252
239
244...

result:

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