/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 61ms 532.0 KiB
#4 Accepted 61ms 576.0 KiB
#5 Accepted 86ms 584.0 KiB
#6 Accepted 98ms 1.02 MiB
#7 Accepted 97ms 816.0 KiB
#8 Accepted 125ms 2.809 MiB
#9 Accepted 131ms 2.828 MiB
#10 Accepted 123ms 3.875 MiB
#11 Accepted 44ms 14.32 MiB
#12 Accepted 135ms 35.199 MiB
#13 Accepted 113ms 35.156 MiB
#14 Accepted 122ms 35.27 MiB
#15 Accepted 120ms 35.184 MiB
#16 Accepted 122ms 35.27 MiB
#17 Accepted 127ms 37.02 MiB
#18 Accepted 129ms 36.938 MiB
#19 Accepted 117ms 22.805 MiB
#20 Accepted 116ms 22.77 MiB

Code

#include <bits/stdc++.h>

using i64 = long long;

using B = std::bitset<100000>;

void solve() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        a[i]--;
    }
    std::vector<std::vector<int>> g(n);
    std::vector<int> size(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    std::vector<int> hson(n);
    auto build = [&](auto&& self, int v, int fa) -> int {
        int sz = 1, hsz = 0, hs = -1;
        for (auto w : g[v]) {
            if (w != fa) {
                int s = self(self, w, v);
                sz += s;
                if (s > hsz) {
                    hsz = s;
                    hs = w;
                }
            }
        }
        hson[v] = hs;
        size[v] = sz;
        return sz;
    };
    build(build, 0, -1);

    std::vector<int> ans(n);
    auto f = [&](auto&& self, int v, int fa) -> std::set<int> {
        if (hson[v] < 0) {
            if (a[v] == 0) {
                ans[v] = 1;
            }
            return std::set<int>{a[v]};
        }
        std::set<int> has = self(self, hson[v], v);
        ans[v] = ans[hson[v]];
        for (auto it = has.lower_bound(size[hson[v]]); it != has.end(); it = next(it)) {
            if (*it >= size[v]) {
                break;
            }
            ans[v]++;
        }
        for (auto w : g[v]) {
            if (w != fa && w != hson[v]) {
                std::set<int> subM = self(self, w, v);
                for (auto key : subM) {
                    if (has.find(key) == has.end() && key < size[v]) {
                        ans[v]++;
                    }
                    has.insert(key);
                }
            }
        }
        if (has.find(a[v]) == has.end() && a[v] < size[v]) {
            ans[v]++;
        }
        has.insert(a[v]);
        return has;
    };

    f(f, 0, -1);
    int q;
    std::cin >> q;
    while (q--) {
        int x;
        std::cin >> x;
        x--;
        std::cout << size[x] - ans[x] << "\n";
    }

}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    int t;
    std::cin >> t;
    while (t--) {
        solve();
    }
}

Information

Submit By
Type
Submission
Problem
P1157 Roy and Tree Permutation
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 15:47:18
Judged At
2025-01-02 15:47:18
Judged By
Score
100
Total Time
135ms
Peak Memory
37.02 MiB