/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 788.0 KiB
#2 Accepted 2ms 788.0 KiB
#3 Accepted 99ms 1.02 MiB
#4 Accepted 123ms 1.066 MiB
#5 Accepted 220ms 996.0 KiB
#6 Accepted 556ms 1.27 MiB
#7 Accepted 572ms 1.352 MiB
#8 Accepted 1314ms 3.352 MiB
#9 Accepted 1667ms 3.363 MiB
#10 Accepted 1844ms 4.547 MiB
#11 Time Exceeded ≥2100ms ≥8.441 MiB
#12 Time Exceeded ≥2100ms ≥20.414 MiB

Code

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

const int MAXN = 100000;

vector<vector<int>> tree;
vector<int> A, in, out, euler_tour, sz;
vector<int> fenwick_tree(MAXN + 1, 0);
int timer = 0;

void dfs(int u, int p) {
    in[u] = timer++;
    euler_tour.push_back(A[u]);
    sz[u] = 1;
    for (int v : tree[u]) {
        if (v != p) {
            dfs(v, u);
            sz[u] += sz[v];
        }
    }
    out[u] = timer - 1;
}

void update(int idx, int val) {
    while (idx <= MAXN) {
        fenwick_tree[idx] += val;
        idx += idx & -idx;
    }
}

int query(int idx) {
    int sum = 0;
    while (idx > 0) {
        sum += fenwick_tree[idx];
        idx -= idx & -idx;
    }
    return sum;
}

int solve(int x) {
    int S = sz[x];
    unordered_map<int, int> node_freq;

    for (int i = in[x]; i <= out[x]; ++i) {
        node_freq[euler_tour[i]]++;
    }

    int missing = 0;
    for (int i = 1; i <= S; ++i) {
        if (node_freq[i] == 0) {
            missing++;
        }
    }
    return missing;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int T;
    cin >> T;

    while (T--) {
        int N;
        cin >> N;

        A.resize(N);
        for (int i = 0; i < N; ++i) {
            cin >> A[i];
        }

        tree.clear();
        tree.resize(N);

        for (int i = 0; i < N - 1; ++i) {
            int u, v;
            cin >> u >> v;
            u--; v--;
            tree[u].push_back(v);
            tree[v].push_back(u);
        }

        sz.clear();
        sz.resize(N, 0);
        in.clear();
        out.clear();
        in.resize(N, -1);
        out.resize(N, -1);
        euler_tour.clear();

        timer = 0;
        dfs(0, -1);

        int Q;
        cin >> Q;

        while (Q--) {
            int x;
            cin >> x;
            x--;
            cout << solve(x) << "\n";
        }
    }

    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 15:55:07
Judged At
2025-01-02 15:55:07
Judged By
Score
50
Total Time
≥2100ms
Peak Memory
≥20.414 MiB