QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#603108#8716. 树frs#WA 0ms12540kbC++143.2kb2024-10-01 14:43:112024-10-01 14:43:11

Judging History

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

  • [2024-10-01 14:43:11]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:12540kb
  • [2024-10-01 14:43:11]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int N = 2e5 + 5;

int n, m, qr, p, g, b[N], ans;
int fa[N][22], dep[N];
bool f[N];
vector<int> ve[N];

void dfs(int x, int last, int d){
    dep[x] = d;
    fa[x][0] = last;
    for (auto i:ve[x]){
        if (i == last) continue;
        dfs(i, x, d+1);
    }
}

int lca(int x, int y){
    if (dep[x] > dep[y]) swap(x, y);
    int z = dep[y]-dep[x], c = 0;
    while (z){
        if (z & 1) y = fa[y][c];
        z >>= 1;
        c++;
    }
    if (x == y) return x;
    for (int i = 20; i >= 0; --i) {
        if (fa[x][i] == fa[y][i]) continue;
        x = fa[x][i];
        y = fa[y][i];
    }
    return fa[x][0];
}

void check(int i){
    int l = 0, r = 0, z = 0, ret;
    ret = lca(b[i - 1], b[i + 1]);
    if(ret == b[i - 1]) l++;
    else if(ret == b[i + 1]) r++;
    ret = lca(b[i - 1], b[i]);
    if(ret == b[i - 1]) l++;
    else if(ret == b[i]) z++;
    ret = lca(b[i], b[i + 1]);
    if(ret == b[i]) z++;
    else if(ret == b[i + 1]) r++;
    
    if(f[i] == 1){
        if(l == 2 && z == 1) f[i] = 0;
        else if(r == 2 && z == 1) f[i] = 0;
        else if(l == 0 && r == 0 && (z == 1 || (z == 2 && lca(b[i - 1], b[i + 1]) == b[i]) || (z == 2 && lca(lca(b[i - 1], b[i + 1]), b[i]) != b[i]))){
            f[i] = 0;
        }
        if(f[i] == 0) ans--;
    }
    else{
        if(l == 2 && z == 1) f[i] = 0;
        else if(r == 2 && z == 1) f[i] = 0;
        else if(l == 0 && r == 0 && (z == 1 || (z == 2 && lca(b[i - 1], b[i + 1]) == b[i]) || (z == 2 && lca(lca(b[i - 1], b[i + 1]), b[i]) != b[i]))){
            f[i] = 0;
        }
        else{
            f[i] = 1;
            ans++;
        }
    }
}

int main(){
    scanf("%d%d%d", &n, &m, &qr);
    int u, v;
    for(int i = 1; i < n; i++){
        scanf("%d%d", &u, &v);
        ve[u].push_back(v);
        ve[v].push_back(u);
    }
    for(int i = 1; i <= m; i++){
        scanf("%d", &b[i]);
    }
    
    dfs(1, 0, 1);
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= 20; ++j) {
            fa[i][j] = fa[fa[i][j-1]][j-1];
        }
    }
    f[1] = 1;
    f[m] = 1;
    for(int i = 2; i < m; i++){
        int l = 0, r = 0, z = 0, ret;
        ret = lca(b[i - 1], b[i + 1]);
        if(ret == b[i - 1]) l++;
        else if(ret == b[i + 1]) r++;
        ret = lca(b[i - 1], b[i]);
        if(ret == b[i - 1]) l++;
        else if(ret == b[i]) z++;
        ret = lca(b[i], b[i + 1]);
        if(ret == b[i]) z++;
        else if(ret == b[i + 1]) r++;
        
        f[i] = 1;
        if(l == 2 && z == 1) f[i] = 0;
        else if(r == 2 && z == 1) f[i] = 0;
        else if(l == 0 && r == 0 && (z == 1 || (z == 2 && lca(b[i - 1], b[i + 1]) == b[i]) || (z == 2 && lca(lca(b[i - 1], b[i + 1]), b[i]) != b[i]))){
            f[i] = 0;
        }
    }
    ans = 0;
    for(int i = 1; i <= m; i++){
        if(f[i]) ans++;
    }
    
    while(qr--){
        scanf("%d%d", &p, &g);
        b[p] = g;
        if(p > 1 && p < m) check(p);
        if(p - 1 > 1 && p - 1 < m) check(p - 1);
        if(p + 1 > 1 && p + 1 < m) check(p + 1);
        printf("%d\n", ans);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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:

171
172
172
172
172
172
173
173
173
173
174
174
174
174
173
173
172
172
172
172
171
170
169
169
169
169
169
169
169
169
169
168
168
169
169
169
169
169
169
169
170
170
169
169
169
169
170
171
172
171
172
172
172
172
172
172
172
172
172
172
173
174
173
173
173
173
173
174
174
174
174
174
174
174
174
...

result:

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