/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 97ms 532.0 KiB
#4 Accepted 97ms 580.0 KiB
#5 Accepted 223ms 788.0 KiB
#6 Accepted 560ms 832.0 KiB
#7 Accepted 579ms 1000.0 KiB
#8 Accepted 1300ms 2.961 MiB
#9 Accepted 1668ms 2.957 MiB
#10 Accepted 1846ms 4.164 MiB
#11 Time Exceeded ≥2098ms ≥8.027 MiB
#12 Time Exceeded ≥2101ms ≥20.031 MiB

Code

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

using namespace std;

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

int solve(int x) {
    int S = sz[x];
    unordered_map<int, int> freq;
    for (int i = in[x]; i <= out[x]; ++i) {
        freq[euler_tour[i]]++;
    }

    int missing = 0;
    for (int i = 1; i <= S; ++i) {
        if (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:41:37
Judged At
2025-01-02 15:41:37
Judged By
Score
50
Total Time
≥2101ms
Peak Memory
≥20.031 MiB