QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#602613#8716. 树liguo#WA 0ms5740kbC++202.2kb2024-10-01 11:12:582024-10-01 11:13:02

Judging History

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

  • [2024-10-01 11:13:02]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:5740kb
  • [2024-10-01 11:12:58]
  • 提交

answer

#include<cstdio>
using namespace std;
int n,m,q;
const int maxn=2e6+10;
struct node{
	int to,next;
}e[maxn*2];
int head[maxn],k=0;
void add(int a,int b){
	e[++k].next=head[a];
	e[k].to=b;
	head[a]=k;
}
struct nod{
	int pl,to;
}h[maxn];
int fa[maxn][22],deep[maxn];
void dfs(int id){
	deep[id]=deep[fa[id][0]]+1;
	for(int i=1;i<21;i++){
		if(fa[id][i-1]==0) continue;
		fa[id][i]=fa[fa[id][i-1]][i-1];
	}
	for(int i=head[id];i;i=e[i].next){
		int tp=e[i].to;
		if(tp==fa[id][0]) continue;
		fa[tp][0]=id;
		dfs(tp);
	}
}
int lca(int a,int b){
	if(deep[a]>deep[b]) return lca(b,a);
	for(int i=20;i>=0;i--){
		if(deep[a]>deep[fa[b][i]]) continue;
		b=fa[b][i];
	}
	if(a==b) return a;
	for(int i=20;i>=0;i--){
		if(fa[a][i]==fa[b][i]) continue;
		b=fa[b][i];
		a=fa[a][i];
	}
	return fa[a][0];
}
int main(){
	scanf("%d %d %d",&n,&m,&q);
	int a,b;
	for(int i=1;i<n;i++){
		scanf("%d %d",&a,&b);
		add(a,b);
		add(b,a);
	}
	for(int i=1;i<=m;i++) scanf("%d",&h[i].pl);
	dfs(1);
	int ans=1;
	if(n>1) ans++;
	for(int i=2;i<=n;i++){
		int c=lca(h[i].pl,h[i-1].pl);
		if(c==h[i-1].pl){
			h[i].to=-1;
			if(h[i-1].to>0){
				c=lca(h[i].pl,h[i-2].pl);
				if(c!=h[i-1].pl) ans++;
			}
		}
		else if(c==h[i].pl){
			h[i].to=1;
			if(h[i-1].to<0) ans++;
		}
		else{
			h[i].to=-1;
			if(h[i-1].to<0) ans++;
		}
	}
	while(q--){
		scanf("%d%d",&a,&b);
		if(a!=1){
			for(int i=a;i<=n&&i<=a+2;i++){
				int c=lca(h[i].pl,h[i-1].pl);
				if(c==h[i-1].pl){
					h[i].to=-1;
					if(h[i-1].to>0){
						c=lca(h[i].pl,h[i-2].pl);
						if(c!=h[i-1].pl) ans--;
					}
				}
				else if(c==h[i].pl){
					h[i].to=1;
					if(h[i-1].to<0) ans--;
				}
				else{
					h[i].to=-1;
					if(h[i-1].to<0) ans--;
				}
			}
		//printf("***%d\n",ans);
			h[a].pl=b;
			for(int i=a;i<=n&&i<=a+2;i++){
				int c=lca(h[i].pl,h[i-1].pl);
				if(c==h[i-1].pl){
					h[i].to=-1;
					if(h[i-1].to>0){
						c=lca(h[i].pl,h[i-2].pl);
						if(c!=h[i-1].pl) ans++;
					}
				}
				else if(c==h[i].pl){
					h[i].to=1;
					if(h[i-1].to<0) ans++;
				}
				else{
					h[i].to=-1;
					if(h[i-1].to<0) ans++;
				}
		//printf("***%d\n",ans);
			}	
		}
		else h[a].pl=b;
		printf("%d\n",ans);
	}
	return 0;
}

详细

Test #1:

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

input:

5 5 3
2 1
3 2
1 4
5 1
1 5 4 2 3
1 3
5 3
3 3

output:

4
4
5

result:

ok 3 number(s): "4 4 5"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 5740kb

input:

30 200 200
10 24
10 13
10 26
13 29
27 26
17 24
27 21
17 15
13 5
13 30
27 3
18 21
9 21
2 24
10 4
11 5
2 8
10 23
1 18
21 25
4 20
12 23
22 27
28 27
18 7
13 6
14 30
10 19
16 21
14 29 25 30 1 17 22 21 11 19 21 30 13 1 22 10 14 7 29 7 15 21 25 29 25 7 29 7 1 23 3 17 2 7 4 27 18 26 3 6 5 3 16 26 20 19 16 2...

output:

27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
27
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
...

result:

wrong answer 1st numbers differ - expected: '174', found: '27'