QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#417051#8716. 树FlyingFeatherWA 2ms12112kbC++172.6kb2024-05-22 13:45:072024-05-22 13:45:08

Judging History

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

  • [2024-05-22 13:45:08]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:12112kb
  • [2024-05-22 13:45:07]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
vector<int>v[N];
int a[N],b[N],depth[N],fa[N][20];
void bfs(int root)
{
    memset(depth,0x3f,sizeof depth);
    depth[0] = 0,depth[root] = 1;
    queue<int> q;
    q.push(root);
    while(q.size())
    {
        int t = q.front();
        q.pop();
        for(auto j:v[t])
        {
            if(depth[j]>depth[t]+1)
            {
                depth[j] = depth[t]+1;
                q.push(j);
                fa[j][0] = t;
                for(int k=1;k<=19;k++)
                {
                    fa[j][k] = fa[fa[j][k-1]][k-1];
                }
            }
        }
    }
}
int lca(int a, int b)
{
    if (depth[a] < depth[b]) swap(a, b);
    for (int k = 19; k >= 0; k -- )
        if (depth[fa[a][k]] >= depth[b])
            a = fa[a][k];
    if (a == b) return a;
    for (int k = 19; k >= 0; k -- )
        if (fa[a][k] != fa[b][k])
        {
            a = fa[a][k];
            b = fa[b][k];
        }
    return fa[a][0];
}
int cal(int i)
{
	if(b[i]==0)
	{
		if(b[i+1]==0)  return 0;
		else if(b[i+1]==1)  return 1;
		else  return 1;
	}
	else if(b[i]==1)
	{
		if(b[i+1]==0)
		{
			if(lca(a[i],a[i+2])==a[i]||lca(a[i],a[i+2])==a[i+2])  return 1;
			return 0;
		}
		else if(b[i+1]==1)  return 0;
		else  return 1;
	}
	else
	{
		if(b[i+1]==0)  return 0;
		else if(b[i+1]==1)  return 1;
		else  return 1;
	}
}
int main()
{
	int n,m,q;
	cin>>n>>m>>q;
	for(int i=1;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		v[x].push_back(y),v[y].push_back(x);
	}
	bfs(1);
	for(int i=1;i<=m;i++)  cin>>a[i];
	for(int i=1;i<m;i++)
	{
		if(lca(a[i],a[i+1])==a[i])  b[i]=0;
		else if(lca(a[i],a[i+1])==a[i+1])  b[i]=1;
		else  b[i]=2;
	}
	int ans=2;
	/*for(int i=1;i<m;i++)  cout<<b[i]<<" ";
	cout<<endl;*/
	for(int i=1;i<m-1;i++)  ans+=cal(i);
	//cout<<ans<<endl;
	while(q--)
	{
		int idx,x,num1=0,num2=0;
		cin>>idx>>x;
		for(int i=max(1,idx-2);i<=min(idx+2,m-2);i++)  num1+=cal(i);
		a[idx]=x;
		for(int i=max(1,idx-2);i<=min(idx+2,m-1);i++)
			if(lca(a[i],a[i+1])==a[i])  b[i]=0;
			else if(lca(a[i],a[i+1])==a[i+1])  b[i]=1;
			else  b[i]=2;
		/*cout<<"a:";
		for(int i=1;i<=m;i++)  cout<<a[i]<<" ";
		cout<<endl;
		cout<<"b:";
		for(int i=1;i<m;i++)  cout<<b[i]<<" ";
		cout<<endl;*/
		for(int i=max(1,idx-2);i<=min(idx+2,m-2);i++)  num2+=cal(i);
		ans+=num2-num1;
		cout<<ans<<endl;
	}
}
/*
5 5 3
2 1
3 2
1 4
5 1
1 5 4 2 3
1 3
5 3
3 3
*/
/*
9 8 1
1 2
2 3
3 4
4 5
1 6
6 7
7 8
8 9
5 1 9 7 5 3 8 4
*/
/*
6 9 1
1 2
1 3
2 4
3 6
3 5
4 5 6 2 4 3 5 1 4
*/

詳細信息

Test #1:

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

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: 11440kb

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:

161
162
162
162
161
162
162
163
162
162
163
163
164
164
163
162
161
161
161
161
161
161
161
161
161
160
160
159
159
159
159
159
159
159
159
159
161
161
161
161
161
161
161
161
162
162
162
163
164
164
165
164
164
163
164
164
164
165
165
165
166
167
167
167
167
167
167
168
168
169
169
168
169
167
166
...

result:

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