/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 4.926 MiB
#2 Accepted 5ms 5.137 MiB
#3 Accepted 36ms 6.867 MiB
#4 Accepted 5ms 5.145 MiB
#5 Accepted 5ms 5.109 MiB

Code

#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define endl '\n'
#define Endl '\n'

using namespace std;

const int N = 2e5 + 5;
int tc, n, m;
ll Ans[N];
vector<int> x[N];

void dfs(int node, int par, int cnt) {
    Ans[cnt]++;
    for (auto i : x[node]) {
        if (i == par) {
            continue;
        }
        dfs(i, node, cnt + 1);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); // cout.tie(0);
    cin >> tc;
    while (tc--) {
        cin >> n >> m;
        for (int i = 0; i < n + 5; i++) {
            x[i].clear();
            Ans[i] = 0;
        }
        for (int i = 0; i < n - 1; i++) {
            int a, b;
            cin >> a >> b;
            x[a].push_back(b);
            x[b].push_back(a);
        }
        dfs(1, -1, 0);
        for (int i = 1; i <= n; i++) {
            Ans[i] += Ans[i - 1];
        }
        while (m--) {
            int x;
            cin >> x;
            cout << Ans[x] << endl;
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1053 Water on Tree
Contest
Brain Booster #3
Language
C++17 (G++ 13.2.0)
Submit At
2024-05-06 15:41:48
Judged At
2024-11-11 03:33:58
Judged By
Score
100
Total Time
36ms
Peak Memory
6.867 MiB