/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 832.0 KiB
#2 Accepted 2ms 788.0 KiB
#3 Accepted 101ms 1.02 MiB
#4 Accepted 95ms 1.02 MiB
#5 Accepted 224ms 980.0 KiB
#6 Accepted 588ms 1.156 MiB
#7 Accepted 583ms 1.277 MiB
#8 Accepted 1307ms 3.352 MiB
#9 Accepted 1681ms 3.355 MiB
#10 Accepted 1857ms 4.531 MiB
#11 Time Exceeded ≥2100ms ≥8.43 MiB
#12 Time Exceeded ≥2101ms ≥20.418 MiB

Code

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

using namespace std;

const int MAX_N = 100000;  

vector<vector<int>> g;
vector<int> A, sz, in, out, euler_tour;
vector<int> freq(MAX_N + 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 : g[u]) {
        if (v != p) {
            dfs(v, u);
            sz[u] += sz[v];
        }
    }
    out[u] = timer - 1;
}

void update(int idx, int val) {
    for (; idx <= MAX_N; idx += idx & -idx) {
        freq[idx] += val;
    }
}

// To query Fenwick Tree (BIT)
int query(int idx) {
    int res = 0;
    for (; idx > 0; idx -= idx & -idx) {
        res += freq[idx];
    }
    return res;
}

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];

        g.clear();
        g.resize(N);

        for (int i = 0; i < N - 1; ++i) {
            int u, v;
            cin >> u >> v;
            u--; v--;
            g[u].push_back(v);
            g[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:49:14
Judged At
2025-01-02 15:49:14
Judged By
Score
50
Total Time
≥2101ms
Peak Memory
≥20.418 MiB