/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 13ms 10.27 MiB
#2 Accepted 13ms 10.43 MiB
#3 Accepted 60ms 10.562 MiB
#4 Accepted 81ms 10.52 MiB
#5 Accepted 79ms 10.543 MiB
#6 Accepted 107ms 11.035 MiB
#7 Accepted 103ms 11.176 MiB
#8 Accepted 183ms 15.688 MiB
#9 Accepted 173ms 15.566 MiB
#10 Accepted 181ms 18.273 MiB
#11 Accepted 53ms 20.34 MiB
#12 Accepted 120ms 35.125 MiB
#13 Accepted 114ms 35.312 MiB
#14 Accepted 115ms 35.27 MiB
#15 Accepted 142ms 35.27 MiB
#16 Accepted 144ms 35.191 MiB
#17 Accepted 126ms 37.0 MiB
#18 Accepted 120ms 36.895 MiB
#19 Accepted 142ms 31.164 MiB
#20 Accepted 118ms 31.195 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];
ordered_set<int>st[N];
int naam[N];
void dfs(int u, int par) {
    st[naam[u]].insert(arr[u]);
    sz[u] = 1;
    for(auto v : adj[u]) {
        if(v == par) continue;
        dfs(v, u);
        if(st[naam[v]].size() > st[naam[u]].size()) swap(naam[u], naam[v]);
        for(auto x : st[naam[v]]) {
            st[naam[u]].insert(x);
        }
        sz[u] += sz[v];
    }
    ans[u] = sz[u] - st[naam[u]].order_of_key(sz[u] + 1);
}
// auto dfs = [&](auto &&self, int u, int par)-> ordered_set<int> {
    //     ordered_set<int> ret;
    //     ret.insert(arr[u]);
    //     sz[u] = 1;
    //     for (auto &v: adj[u]) {
    //         if (v == par) continue;
    //         ordered_set<int> o = self(self, v, u);
    //         if (o.size() > ret.size()) swap(ret, o);
    //         for (auto &it: o) ret.insert(it);
    //         sz[u] += sz[v];
    //     }
    //     ans[u] = sz[u] - (ret.order_of_key(sz[u] + 1));
    //     return ret;
    // };
void shelby() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> arr[i], adj[i].clear();
        st[i].clear();
        naam[i] = i;
    }
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    
    dfs(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:12:10
Judged At
2025-01-13 05:12:10
Judged By
Score
100
Total Time
183ms
Peak Memory
37.0 MiB