/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.762 MiB
#2 Accepted 3ms 2.77 MiB
#3 Accepted 89ms 3.02 MiB
#4 Accepted 64ms 3.023 MiB
#5 Accepted 170ms 2.98 MiB
#6 Accepted 344ms 3.312 MiB
#7 Accepted 369ms 3.312 MiB
#8 Accepted 892ms 5.148 MiB
#9 Accepted 919ms 5.09 MiB
#10 Accepted 997ms 6.074 MiB
#11 Time Exceeded ≥2099ms ≥13.297 MiB
#12 Time Exceeded ≥2101ms ≥28.676 MiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

using namespace std;
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 1e5 + 5;
int arr[N], ans[N], sz[N];
vector<int> adj[N];

void shelby() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> arr[i], adj[i].clear();
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    auto dfs = [&](auto &&self, int u, int par)-> int {
        sz[u] = 1;
        for (auto &v: adj[u]) if (v != par) sz[u] += self(self, v, u);
        return sz[u];
    };
    dfs(dfs, 1, -1);

    auto dfs2 = [&](auto &&self, int u, int par)-> set<int> {
        set<int> ret{arr[u]};
        ans[u] = sz[u] - (arr[u] <= sz[u]);
        for (auto &v: adj[u]) {
            if (v == par) continue;
            set<int> st = self(self, v, u);
            for (auto &it: st) if (!ret.count(it)) ret.insert(it), ans[u] -= (it <= sz[u]);
        }
        return ret;
    };
    dfs2(dfs2, 1, -1);

    int q;
    cin >> q;
    while (q--) {
        int x;
        cin >> x;
        cout << ans[x] << '\n';
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1157 Roy and Tree Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-13 05:16:38
Judged At
2025-01-13 05:16:38
Judged By
Score
50
Total Time
≥2101ms
Peak Memory
≥28.676 MiB