Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : It is a tree

#include <bits/stdc++.h>
using namespace std;

// Subtask 1 : All queries are for root.

class Tree {
  public:
    int n;
    vector<vector<int>> adj;
    vector<int> depth;
    Tree(int n) {
        this->n = n;
        adj.resize(n);
        depth.resize(n);
    }

    void dfs(int src, int par) {
        if (par == -1) {
            depth[src] = 0;
        } else {
            depth[src] = depth[par] + 1;
        }

        for (auto child : adj[src]) {
            if (child == par) {
                continue;
            }
            dfs(child, src);
        }
    }
};

void solve() {
    int n, q;
    cin >> n >> q;
    Tree t(n);
    for (int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        t.adj[x].push_back(y);
        t.adj[y].push_back(x);
    }
    t.dfs(0, -1);

    vector<int> &depth = t.depth;
    vector<int> state(n);
    map<int, int> freq;
    int uniq_count = 0;

    for (int i = 0; i < q; i++) {
        int type, u;
        cin >> type >> u;
        u--;
        if (type == 1) {
            // For this subtask, u is always root.
            cout << uniq_count << "\n";
        } else {
            if (u == 0) {
                continue;
            }
            if (state[u] == 0) {
                if (freq[depth[u]] == 0) {
                    uniq_count++;
                }
                if (freq[depth[u]] == 1) {
                    uniq_count--;
                }
                freq[depth[u]]++;
            } else {
                if (freq[depth[u]] == 1) {
                    uniq_count--;
                }
                if (freq[depth[u]] == 2) {
                    uniq_count++;
                }
                freq[depth[u]]--;
            }

            state[u] ^= 1;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

// Subtask 2 : All update queries are at the start.

class Tree {
  public:
    int n;
    vector<vector<int>> adj;
    // uniq_depth_cnt[i] means the unique depth counts including the i-th
    // vertex's depth.
    vector<int> depth, a, uniq_depth_cnt;
    vector<map<int, int>> depth_freq;
    Tree(int n) {
        this->n = n;
        adj.resize(n);
        depth.resize(n);
        a.resize(n);
        uniq_depth_cnt.resize(n);
        depth_freq.resize(n);
    }

    void dfs(int src, int par) {
        if (par == -1) {
            depth[src] = 0;
        } else {
            depth[src] = depth[par] + 1;
        }
        if (a[src] == 1) {
            depth_freq[src][depth[src]]++;
            uniq_depth_cnt[src] = 1;
        }

        for (auto child : adj[src]) {
            if (child == par) {
                continue;
            }
            dfs(child, src);

            // Assume that parent is always the big set.
            // We need to use cached results from big set, so we store the uniq
            // depth count in the subtree after adding each children.
            int cnt = uniq_depth_cnt[src];
            if (depth_freq[child].size() > depth_freq[src].size()) {
                cnt = uniq_depth_cnt[child];
                swap(depth_freq[child], depth_freq[src]);
            }

            for (auto &[key, freq] : depth_freq[child]) {
                auto &big_set = depth_freq[src];
                if (big_set[key] == 0 && freq == 1) {
                    cnt++;
                }
                if (big_set[key] == 1 && freq > 0) {
                    cnt--;
                }
                big_set[key] += freq;
            }
            uniq_depth_cnt[src] = cnt;
        }
    }

    void reset() {
        depth_freq.assign(n, {});
        uniq_depth_cnt.assign(n, 0);
        depth.assign(n, 0);
    }
};

void solve() {
    int n, q;
    cin >> n >> q;
    Tree t(n);
    for (int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        t.adj[x].push_back(y);
        t.adj[y].push_back(x);
    }

    bool dfs_done = false;
    for (int i = 0; i < q; i++) {
        int type, u;
        cin >> type >> u;
        u--;
        if (type == 1) {
            // For this subtask, all update queries are at the start.
            if (!dfs_done) {
                t.dfs(0, -1);
                dfs_done = true;
            }
            cout << t.uniq_depth_cnt[u] - (t.a[u] == 1) << "\n";
        } else {
            t.a[u] ^= 1;
            // Uncomment if you want to solve all versions in O(n^2).
            // Only for testing.
            // t.reset();
            // dfs_done = false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
    return 0;
}