/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 1ms 560.0 KiB
#3 Accepted 104ms 708.0 KiB
#4 Accepted 101ms 840.0 KiB
#5 Accepted 226ms 864.0 KiB
#6 Accepted 608ms 1.141 MiB
#7 Accepted 610ms 1.0 MiB
#8 Accepted 1348ms 2.973 MiB
#9 Accepted 1788ms 3.062 MiB
#10 Accepted 1971ms 4.379 MiB
#11 Time Exceeded ≥2094ms ≥8.117 MiB
#12 Time Exceeded ≥2096ms ≥20.18 MiB

Code

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

using namespace std;

const int MAX_N = 100000;

vector<vector<int>> tree;
vector<int> A, in, out, euler_tour, sz;
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;
}

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:53:45
Judged At
2025-01-02 15:53:45
Judged By
Score
50
Total Time
≥2096ms
Peak Memory
≥20.18 MiB