/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.02 MiB
#2 Accepted 5ms 4.926 MiB
#3 Accepted 44ms 5.27 MiB
#4 Accepted 43ms 5.203 MiB
#5 Accepted 51ms 5.562 MiB
#6 Accepted 57ms 6.52 MiB
#7 Accepted 57ms 6.473 MiB
#8 Accepted 83ms 8.66 MiB
#9 Accepted 83ms 8.566 MiB
#10 Accepted 85ms 9.32 MiB
#11 Time Exceeded ≥2100ms ≥11.367 MiB
#12 Time Exceeded ≥2101ms ≥20.562 MiB

Code

#include <bits/stdc++.h>

using namespace std;

// #include "debug.h"

const int N = 1e5 + 10;

int n;
int a[N];
int q;
vector<int> g[N];
vector<int> sub[N];
int sz[N];
int ans[N];

void merge(int u, int v) {
        if (sub[v].size() > sub[u].size()) {
                sub[v].swap(sub[u]);
        }

        for (int i : sub[v]) {
                sub[u].push_back(i);
        }
}

void dfs(int u, int v) {
        sub[u].push_back(a[u]);

        for (int c : g[u]) {
                if (c == v) {
                        continue;
                }

                dfs(c, u);

                sz[u] += sz[c];
                merge(u, c);
        }

        vector<int> cnt(sz[u] + 1);
        for (int i : sub[u]) {
                if (i <= sz[u]) {
                        cnt[i] = 1;
                }
        }

        for (int i = 1; i <= sz[u]; ++i) {
                ans[u] += (cnt[i] == 0);
        }
}

void solve() {
        cin >> n;

        for (int i = 0; i < n; ++i) {
                cin >> a[i];
                g[i].clear();
                sub[i].clear();
                sz[i] = 1;
                ans[i] = 0;
        }

        for (int i = 0; i < n - 1; ++i) {
                int u, v;
                cin >> u >> v;
                --u, --v;

                g[u].push_back(v), g[v].push_back(u);
        }

        dfs(0, -1);

        int q;
        cin >> q;
        for (int i = 0; i < q; ++i) {
                int x;
                cin >> x;
                cout << ans[x - 1] << '\n';
        }
}

int32_t main() {
        ios_base::sync_with_stdio(false);
        cin.tie(0);

        int tc = 1;
        cin >> tc;

        while (tc--) {
                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 16:12:44
Judged At
2025-01-02 16:12:44
Judged By
Score
50
Total Time
≥2101ms
Peak Memory
≥20.562 MiB