/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 348.0 KiB
#3 Accepted 60ms 592.0 KiB
#4 Accepted 64ms 588.0 KiB
#5 Accepted 79ms 804.0 KiB
#6 Accepted 95ms 1020.0 KiB
#7 Accepted 97ms 1.02 MiB
#8 Accepted 146ms 3.707 MiB
#9 Accepted 144ms 3.59 MiB
#10 Accepted 146ms 5.305 MiB
#11 Time Exceeded ≥2097ms ≥10.617 MiB
#12 Time Exceeded ≥2100ms ≥24.926 MiB

Code

/**
 *    author:   Binoy Barman
 *    created:  2025-01-02 22:34:25
**/

#include<bits/stdc++.h>
#ifdef LOCAL
#include "debug/trace.hpp"
#else
#define dbg(...) 42
#define print(...) 42
#endif
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = 1e9;

// #define int long long
#define nl '\n'
#define all(v) v.begin(), v.end()
#define Testcase_Handler int tts, tc = 1; cin >> tts; hell: while(tts--)
#define uniq(v) sort(all(v)), v.resize(distance(v.begin(), unique(v.begin(), v.end())))
template<class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {bool first = true;for(auto &x : a) {if(!first) out << ' ';first = false;out << x;}return out;};

namespace Dark_Lord_Binoy {
void preprocess() {}
void setup() {
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr);
    preprocess();

    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}
}
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> 
using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k . O(logn)
// find_by_order(k) : K-th element in a set (counting from zero). O(logn)
template <typename T, typename R> 
using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 1e5 + 9;

int a[N], ans[N];
struct Dfs {
    vector<vector<int>> g;
    vector<int> sz;
    vector<bool> vis;
    vector<set<int>> c;

    Dfs(int n) : sz(n + 1), vis(n + 1), g(n + 1), c(n + 1) {}

    void add_edge(int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u); 
    }
    void dfs(int v, int p = -1) {
        vis[v] = true;
        sz[v] = 1;
        int big = 0, mx = 0;
        for(auto u : g[v]) if(!vis[u]) {
            dfs(u, v);
            sz[v] += sz[u];
            if(sz[u] > mx) {
                mx = sz[u];
                big = u;
            }
        }
        if(big == 0) {
            c[v].insert(a[v]);
        } else {
            c[v] = move(c[big]);
            c[big].clear();
            for(auto u : g[v]) if(u != big) {
                for(auto x : c[u]) {
                    c[v].insert(x);
                }
                c[u].clear();
            }
            c[v].insert(a[v]);
        }
        int nn = sz[v];
        int ase = distance(c[v].begin(), c[v].lower_bound(nn + 1));
        int gg = c[v].size();
        ans[v] = nn - max(0, min(ase, gg));
    }
};

int32_t main() {
    Dark_Lord_Binoy::setup();
    Testcase_Handler {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        Dfs g(n);
        for (int i = 1; i < n; i++) {
            int u, v;
            cin >> u >> v;
            g.add_edge(u, v);
        }
        g.dfs(1);
        int q;
        cin >> q;
        while(q--) {
            int x;
            cin >> x;
            cout << ans[x] << nl;
        }
    }

    print(_Time_);
    return 0;
}

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:58:41
Judged At
2025-01-02 16:58:41
Judged By
Score
50
Total Time
≥2100ms
Peak Memory
≥24.926 MiB