/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 768.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 260ms 2.996 MiB
#4 Accepted 1ms 284.0 KiB
#5 Accepted 1ms 532.0 KiB

Code

#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> tr;
vector<int> dh;
vector<int> cc;

void dfs(int nd, int pt, int d) {
    dh[nd] = d;
    cc[d]++;
    for (int ch : tr[nd]) {
        if (ch != pt) {
            dfs(ch, nd, d + 1);
        }
    }
}

int main() {
    int t;
    cin >> t;

    while (t--) {
        int n, q;
        cin >> n >> q;

        tr.assign(n + 1, vector<int>());
        dh.assign(n + 1, 0);
        cc.assign(n + 1, 0);

        for (int i = 1; i < n; ++i) {
            int u, v;
            cin >> u >> v;
            tr[u].push_back(v);
            tr[v].push_back(u);
        }

        dfs(1, 0, 0);

        for (int i = 1; i <= n; ++i) {
            cc[i] += cc[i - 1];
        }

        for (int i = 0; i < q; ++i) {
            int x;
            cin >> x;

            cout << cc[x] << endl;
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1053 Water on Tree
Contest
Brain Booster #3
Language
C++20 (G++ 13.2.0)
Submit At
2024-05-06 15:26:31
Judged At
2024-11-11 03:34:26
Judged By
Score
100
Total Time
260ms
Peak Memory
2.996 MiB