Submission #29477002


Source Code Expand

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint998244353;
// using mint = modint1000000007;
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep2(i,k,n) for (int i = (k); i < (n); ++i)
using namespace std;
using ll = long long;
// using P = pair<ll,ll>;
using P = pair<int,int>;
using vint = vector<int>;
using vll = vector<ll>;
using vvint = vector<vector<int>>;
using vvll = vector<vector<ll>>;

// const ll INF = (ll)2e18+9;
const int INF = (int)2e9+7;
// const ll MOD = (ll)1e9+9;
template<typename T>
void chmin(T &a, T b) { a = min(a, b); }
template<typename T>
void chmax(T &a, T b) { a = max(a, b); }

template<typename T>
void print(vector<T> v) {
    int n = v.size();
    rep(i,n) {
        if (i == 0) cout << v[i];
        else cout << ' ' << v[i];
    }
    cout << endl;
}

vvint graph;
vint score;
vvint child;
const int MAX_KTH = 20;

void dfs(int x, int p) {
    child[x].push_back(score[x]);

    for (int nx : graph[x]) {
        if (nx == p) continue;
        dfs(nx, x);
        for (int i = 1; i <= min((int)child[nx].size(), MAX_KTH); i++) {
            child[x].push_back(child[nx][i-1]);
        }
    }

    sort(rall(child[x]));
}

void solve() {
    int n, q;
    cin >> n >> q;
    score.resize(n);
    child.resize(n);
    rep(i,n) cin >> score[i];

    graph.resize(n);

    rep(i,n-1) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    dfs(0, -1);

    rep(i,q) {
        int v, k;
        cin >> v >> k;
        v--, k--;
        cout << child[v][k] << endl;
    }

}

int main() {
    solve();
    return 0;
}

Submission Info

Submission Time
Task E - Subtree K-th Max
User goropikari
Language C++ (GCC 9.2.1)
Score 500
Code Size 1854 Byte
Status AC
Exec Time 361 ms
Memory 36556 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 22
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 6 ms 3576 KiB
random_01.txt AC 361 ms 36456 KiB
random_02.txt AC 359 ms 36436 KiB
random_03.txt AC 361 ms 36556 KiB
random_04.txt AC 288 ms 15128 KiB
random_05.txt AC 284 ms 15076 KiB
random_06.txt AC 293 ms 15208 KiB
random_07.txt AC 311 ms 16092 KiB
random_08.txt AC 308 ms 16012 KiB
random_09.txt AC 315 ms 16084 KiB
random_10.txt AC 304 ms 15848 KiB
random_11.txt AC 300 ms 15752 KiB
random_12.txt AC 304 ms 15780 KiB
random_13.txt AC 320 ms 16080 KiB
random_14.txt AC 307 ms 16096 KiB
random_15.txt AC 318 ms 16012 KiB
random_16.txt AC 312 ms 16040 KiB
random_17.txt AC 307 ms 16000 KiB
random_18.txt AC 316 ms 16044 KiB
sample_01.txt AC 4 ms 3608 KiB
sample_02.txt AC 2 ms 3528 KiB
sample_03.txt AC 3 ms 3604 KiB