QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#603125#8716. 树Komorebie#WA 1ms3676kbC++172.7kb2024-10-01 14:51:002024-10-01 14:51:14

Judging History

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

  • [2024-10-01 14:51:14]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3676kb
  • [2024-10-01 14:51:00]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define PII pair<int, int>
#define pi acos(-1.0)

struct LCA {
    vector<vector<int>> G;
    vector<vector<int>> fa;
    vector<int> dep;
    LCA(int n)
    {
        G.resize(n + 1);
        fa.resize(n + 1, vector<int>(31, 0));
        dep.resize(n + 1, 0);
    }

    void dfs(int u, int f)
    {
        fa[u][0] = f;
        dep[u] = dep[f] + 1;
        for (int i = 1; i < 31; i++) {
            fa[u][i] = fa[fa[u][i - 1]][i - 1];
        }
        for (int v : G[u]) {
            if (v != f) {
                dfs(v, u);
            }
        }
    }

    int lca(int x, int y)
    {
        if (dep[x] < dep[y]) {
            swap(x, y);
        }
        int d = dep[x] - dep[y];
        for (int i = 0; (1 << i) <= d; i++) {
            if ((d >> i) & 1) x = fa[x][i];
        }
        if (x == y) return x;
        for (int i = log2(dep[y]); i >= 0; i--) {
            if (fa[x][i] != fa[y][i]) {
                x = fa[x][i];
                y = fa[y][i];
            }
        }
        return fa[x][0];
    }
    bool isfa(int x, int y) { return lca(x, y) == x; }

    int dist(int x, int y) { return dep[x] + dep[y] - 2 * dep[lca(x, y)]; }
};

int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, m, q;
    cin >> n >> m >> q;
    LCA T(n);
    vector<int> b(m + 1);
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        T.G[u].push_back(v), T.G[v].push_back(u);
    }
    for (int i = 1; i <= m; i++) cin >> b[i];
    T.dfs(1, 0);
    vector<bool> son(m + 1, false);
    son[1] = 1;

    auto calc = [&](int i) {
        if (i > 2 && T.lca(b[i - 2], b[i]) != b[i - 1] && T.isfa(b[i - 1], b[i - 2]) && T.isfa(b[i - 1], b[i])) return false;
        return T.isfa(b[i - 1], b[i]);
    };

    for (int i = 2; i <= m; i++) {

        son[i] = calc(i);
    }
    int ans = m;
    for (int i = 3; i <= m; i++) {
        if (son[i] == 1) ans--;
    }
    // cout << ans << endl;
    while (q--) {
        int p, w;
        cin >> p >> w;
        unordered_map<int, bool> tmp;
        int last = b[p];
        for (int i = max(2, p - 2); i <= min(m, p + 2); i++) tmp[i] = son[i];
        b[p] = w;
        for (int i = max(2, p - 2); i <= min(m, p + 2); i++) {
            son[i] = calc(i);
        }
        for (int i = max(3, p - 2); i <= min(m, p + 2); i++) {
            ans -= son[i] - tmp[i];
        }
        for (int i = max(2, p - 2); i <= min(m, p + 2); i++) {
            son[i] = tmp[i];
        }
        b[p] = last;
        cout << ans << endl;
    }
    return 0;
}

詳細信息

Test #1:

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

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: 1ms
memory: 3604kb

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:

184
185
185
185
185
186
186
186
185
185
185
185
186
186
186
186
185
185
185
185
185
184
184
184
184
184
184
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
183
182
182
182
181
181
182
182
183
183
183
184
184
184
184
185
185
185
185
186
186
187
187
187
187
187
187
189
189
189
189
189
188
...

result:

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