QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#602641#8716. 树AAA___#WA 3ms11840kbC++203.7kb2024-10-01 11:27:032024-10-01 11:27:05

Judging History

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

  • [2024-10-01 11:27:05]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:11840kb
  • [2024-10-01 11:27:03]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<stack>
#include<set>
#include<unordered_set>
#include<queue>
#include<deque>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<array>
#include<functional>
using namespace std;
typedef long long ll;
ll highbit(ll x){
    ll ans=2;
    int num=1;
    while(x){
        x>>=2;
        ans<<=2;
        num++;
    }
    return num;
}
ll lowbit(ll x){
    return x&(-x);
}
long long max(long long a,long long b){
    return a>b?a:b;
}
long long min(long long a,long long b){
    return a>b?b:a;
}
ll gcd(ll x,ll y)
{
    if(y==1)
        return x;
    return gcd(y,x%y);
}

const int maxn=4e5;
vector<int> G[maxn];
int arr[maxn];
struct WANDL{
    int size[maxn],dep[maxn],son[maxn],top[maxn],fa[maxn];
    void dfs(int x,int pre){
        fa[x]=pre;
        size[x]=1;
        dep[x]=dep[pre]+1;
        son[x]=0;
        for(auto i:G[x]){
            if(i==pre){
                continue;
            }
            dfs(i,x);
            size[x]+=size[i];
            if(size[i]>size[son[x]]){
                son[x]=i;
            }
        }
        return;        
    }
    void dfstop(int x,int pre,int t){
        top[x]=t;
        if(!son[x]){
            return;
        }
        for(auto i:G[x]){
            if(i==pre){
                continue;
            }
            if(i==son[x]){
                dfstop(i,x,top[x]);
            }else{
                dfstop(i,x,i);
            }
        }
        return ;
    }
    void init(int root){
        dep[0]=-1;
        dfs(root,0);
        dfstop(root,0,root);
    }
    int lca(int a,int b){
        while(top[a]!=top[b]){
        if(dep[top[a]]<dep[top[b]]){
            swap(a,b);
        }
        a=fa[top[a]];
        }
        return (dep[a]>dep[b]?b:a);       
    }
}wdl;
bool judge(int u,int x,int v){
    if(u==v){
        return false;
    }
    int f=wdl.lca(u,v);
    if(f!=u&&f!=v){
    }else{
        if(f==u){
            if(wdl.lca(v,x)==x){
                return true;
            }
        }else{
            if(wdl.lca(u,x)==x){
                return true;
            }
        }
    }
    if(f==wdl.lca(u,x)&&wdl.lca(v,x)==x){
        return true;
    }
    return false;
}
void solve() {
    int n,m,q;
    cin>>n>>m>>q;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    wdl.init(1);
    for(int i=1;i<=m;i++){
        cin>>arr[i];
    }
    ll ans=2;
    for(int i=3;i<=m;i++){
        if(!judge(arr[i-2],arr[i-1],arr[i])){
            ans++;
        }
    }
    if(m==2){
        while(q--){
            cout<<2<<endl;
        }
        return ;
    }
    while(q--){
        int p,x;
        cin>>p>>x;
        ll b1=0,b2=0;
        if(p+1<=m&&p-1>=1){
            if(!judge(arr[p-1],arr[p],arr[p+1])){
                b1++;
            }
            if(!judge(arr[p-1],x,arr[p+1])){
                b2++;
            }
        }
        if(p+2<=m){
            if(!judge(arr[p],arr[p+1],arr[p+2])){
                b1++;
            }
            if(!judge(x,arr[p+1],arr[p+2])){
                b2++;
            }
        }
        if(p-2>=1){
            if(!judge(arr[p-2],arr[p-1],arr[p])){
                b1++;
            }
            if(!judge(arr[p-2],arr[p-1],x)){
                b2++;
            }
        }
        ans+=(b2-b1);
        cout<<ans<<endl;
        arr[p]=x;
    }
    return ;
}
int main(void){
    unsigned int t;
    //freopen("input.in","r",stdin);
    //cin>>t;
    t=1;
    while(t--){
        solve();
    }
    return 0;
}

详细

Test #1:

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

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: 3ms
memory: 11840kb

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
176
176
176
175
175
176
176
177
177
177
175
174
174
174
174
174
172
172
172
172
172
172
172
172
172
172
172
171
171
171
171
172
172
172
172
172
172
172
172
174
176
174
174
175
175
176
175
174
174
174
174
174
174
174
174
174
175
173
173
173
173
173
173
173
174
174
174
174
176
175
...

result:

wrong answer 6th numbers differ - expected: '175', found: '176'