QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#623406#8716. 树bruteforce_WA 817ms9968kbC++202.1kb2024-10-09 11:46:582024-10-09 11:46:59

Judging History

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

  • [2024-10-09 11:46:59]
  • 评测
  • 测评结果:WA
  • 用时:817ms
  • 内存:9968kb
  • [2024-10-09 11:46:58]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+7,inf=1e18,mod=998244353,S=21;
int n,m,Q;
vector<vector<int>> e,fa;
vector<int> dep;
void dfs(int u)
{
	dep[u]=dep[fa[u][0]]+1;
	for(auto v:e[u])
	{
		if(v==fa[u][0]) continue;
		fa[v][0]=u;
		dfs(v);
	}
	for(int i=1; i<S; i++)
	{
		fa[u][i]=fa[fa[u][i-1]][i-1];
	}
}
int lca(int x,int y)
{
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=S-1; i>=0; i--)
	{
		if(dep[fa[x][0]]>=dep[y])
		{
			x=fa[x][0];
		}
	}
	if(x==y) return x;
	for(int i=S-1; i>=0; i--)
	{
		if(fa[x][i]!=fa[y][i])
		{
			x=fa[x][i];
			y=fa[y][i];
		}
	}
	return fa[x][0];
}
void O_o()
{
	cin>>n>>m>>Q;
	e.assign(n+1,vector<int>());
	fa.assign(n+1,vector<int>(S,0));
	dep.assign(n+1,0);
	for(int i=1; i<n; i++)
	{
		int x,y;
		cin>>x>>y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	dfs(1);
	vector<int> b(2*m),ls(2*m);
	vector<bool> del(2*m,0);
	cin>>b[1];
	for(int i=2; i<=m; i++)
	{
		cin>>b[i*2-1];
		b[i*2-2]=lca(b[i*2-3],b[i*2-1]);
	}
	ls[2]=1;
	int tot=0;
	for(int i=2; i<2*m-1; i++)
	{
		int l1=lca(b[ls[i]],b[i]),l2=lca(b[i],b[i+1]),l3=lca(b[ls[i]],b[i+1]);
		int num=(l1==b[i])+(l2==b[i])+(l3==b[i]);
		if(num==1||num==3)
		{
//			if(i&1)
//				cout<<i/2+1<<"\n";
			del[i]=1;
			tot++;
			ls[i+1]=ls[i];
		}
		else
		{
			del[i]=0;
			ls[i+1]=i;
		}
	}
//	cout<<2*n-1-tot<<"\n";
	while(Q--)
	{
		int id,x;
		cin>>id>>x;
		id=id*2-1;
		b[id]=x;
		if(id>1)
		{
			b[id-1]=lca(b[id-2],b[id]);
		}
		if(id<m*2-1)
		{
			b[id+1]=lca(b[id],b[id+2]);
		}
		for(int i=max(2ll,id-4); i<=min(2*m-2,id+4); i++)
		{
			tot-=del[i];
			int l1=lca(b[ls[i]],b[i]),l2=lca(b[i],b[i+1]),l3=lca(b[ls[i]],b[i+1]);
			int num=(l1==b[i])+(l2==b[i])+(l3==b[i]);
			if(num==1||num==3)
			{
				del[i]=1;
				tot++;
				ls[i+1]=ls[i];
			}
			else
			{
				del[i]=0;
				ls[i+1]=i;
			}
		}
		cout<<2*m-1-tot<<"\n";
	}
}
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cout<<fixed<<setprecision(12);
	int T=1;
//	cin>>T;
	while(T--)
	{
		O_o();
	}
}


















详细

Test #1:

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

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: 0
Accepted
time: 1ms
memory: 3820kb

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:

174
175
175
175
175
175
175
175
175
175
176
176
176
176
176
176
175
175
175
175
174
173
173
173
173
173
173
174
174
174
174
173
173
174
174
174
174
174
174
174
174
174
173
173
173
174
173
173
174
173
174
174
174
174
174
174
174
174
174
174
174
175
174
174
174
174
174
175
175
177
177
177
177
177
176
...

result:

ok 200 numbers

Test #3:

score: -100
Wrong Answer
time: 817ms
memory: 9968kb

input:

1000 200000 200000
142 266
266 877
877 673
673 473
923 473
923 55
55 288
679 288
85 679
85 460
296 460
793 296
262 793
40 262
40 680
647 680
999 647
56 999
550 56
550 774
774 939
939 423
423 168
168 554
554 93
329 93
329 474
221 474
890 221
890 304
752 304
345 752
345 269
290 269
290 781
781 264
859...

output:

372219
372216
372216
372216
372216
372216
372216
372217
372217
372217
372217
372217
372218
372218
372218
372218
372218
372218
372217
372217
372219
372219
372220
372222
372220
372222
372222
372221
372221
372221
372220
372218
372218
372218
372218
372218
372218
372218
372218
372218
372218
372218
372218...

result:

wrong answer 1st numbers differ - expected: '133598', found: '372219'