/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 324.0 KiB
#2 Accepted 3ms 320.0 KiB
#3 Accepted 36ms 2.668 MiB
#4 Accepted 3ms 324.0 KiB
#5 Accepted 3ms 320.0 KiB

Code

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

#define int long long

const int mx = 1e5+9;

vector<int>tree[mx];
int h[mx];

void dfs (int x, int p, int l) {
  h[l]++;
  for (auto u : tree[x]) {
      if (u != p) {
          dfs (u, x, l+1);
      }
  }
}

int32_t main () {
  cin.tie(0)->sync_with_stdio(0);
  int t = 1;
  cin >> t;
  while (t--) {
      int n, q;
      cin >> n >> q;
      for (int i = 0; i < n-1; i++) {
          int u, v;
          cin >> u >> v;
          tree[u].push_back(v);
          tree[v].push_back(u);
      }
      dfs (1, 0, 0);
      for (int i = 1; i <= n; i++) {
          h[i] += h[i-1];
      }
      while (q--) {
          int x;
          cin >> x;
          cout << h[x] << "\n";
      }
      for (int i = 0; i <= n; i++) {
          h[i] = 0;
          tree[i].clear();
      }
  }
}

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:40:24
Judged At
2024-11-11 03:34:01
Judged By
Score
100
Total Time
36ms
Peak Memory
2.668 MiB