QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#462890#8716. 树PorNPtree#TL 3ms12816kbC++171.6kb2024-07-04 09:02:502024-07-04 09:02:50

Judging History

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

  • [2024-07-04 09:02:50]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:12816kb
  • [2024-07-04 09:02:50]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

vector<int> G[N];
int m, dep[N], fa[N], siz[N], son[N], top[N], b[N];

void dfs1(int x) {
    siz[x] = 1;
    for (auto v : G[x]) if (v != fa[x]) {
        fa[v] = x, dep[v] = dep[x] + 1;
        dfs1(v);
        siz[x] += siz[v];
        (siz[v] > siz[son[x]]) && (son[x] = v);
    }
}

void dfs2(int x) {
    for (auto v : G[x]) if (v != fa[x]) {
        top[v] = (v == top[x] ? top[x] : v);
        dfs2(v);
    }
}

int LCA(int x, int y) {
    while (top[x] != top[y]) {
        int &z = (dep[top[x]] > dep[top[y]] ? x : y);
        z = fa[top[z]];
    }
    return (dep[x] < dep[y] ? x : y);
}

int dis(int x, int y) {
    return dep[x] + dep[y] - (dep[LCA(x, y)] << 1);
}

int check(int x, int y, int z) {
    return dis(x, y) + dis(y, z) == dis(x, z);
}

int chk(int x) {
    if (x >= 1 && x + 2 <= m) {
        return check(b[x], b[x + 1], b[x + 2]);
    }
    return 0;
}

signed main() {
    int n, q; scanf("%d%d%d", &n, &m, &q);
    for (int i = 1, x, y; i < n; ++i) {
        scanf("%d%d", &x, &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs1(dep[1] = 1);
    dfs2(top[1] = 1);
    int res = m;
    for (int i = 1; i <= m; ++i) {
        scanf("%d", &b[i]);
    }
    for (int i = 1; i <= m - 2; ++i) res -= chk(i);
    while (q--) {
        int x, y; scanf("%d%d", &x, &y);
        res += chk(x - 2), res += chk(x - 1), res += chk(x);
        b[x] = y;
        res -= chk(x - 2), res -= chk(x - 1), res -= chk(x);
        printf("%d\n", res);
    }
    return 0;
}

詳細信息

Test #1:

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

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

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
Time Limit Exceeded

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:

133598
133598
133598
133598
133598
133598
133596
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133596
133596
133596
133596
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133598
133596
133596
133596
133596
133596...

result: