/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 110ms 532.0 KiB
#4 Accepted 104ms 568.0 KiB
#5 Accepted 257ms 908.0 KiB
#6 Accepted 576ms 1.711 MiB
#7 Accepted 592ms 1.672 MiB
#8 Accepted 1329ms 14.262 MiB
#9 Accepted 1676ms 20.551 MiB
#10 Accepted 1759ms 31.629 MiB
#11 Memory Exceeded ≥2170ms ≥256.016 MiB
#12 Memory Exceeded ≥2181ms ≥256.016 MiB

Code

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

using namespace std;

vector<vector<int>> g;
vector<int> A, sz;
vector<vector<int>> val;
 
void dfs(int u, int p) {
    sz[u] = 1;
    val[u].push_back(A[u]);
    for (int v : g[u]) {
        if (v != p) {
            dfs(v, u);
            sz[u] += sz[v];
            val[u].insert(val[u].end(), val[v].begin(), val[v].end());
        }
    }
}

int solve(int x) {
    int S = sz[x];
    unordered_map<int, int> freq;
    for (int v : val[x]) {
        freq[v]++;
    }
    int miss = 0;
    for (int i = 1; i <= S; ++i) {
        if (freq[i] == 0) miss++;
    }
    return miss;
}

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);
        val.clear();
        val.resize(N);
        
        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:40:06
Judged At
2025-01-02 15:40:06
Judged By
Score
50
Total Time
≥2181ms
Peak Memory
≥256.016 MiB