/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 4.316 MiB
#2 Accepted 5ms 4.32 MiB
#3 Accepted 275ms 6.438 MiB
#4 Accepted 6ms 4.285 MiB
#5 Accepted 5ms 4.324 MiB

Code

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

const int MX = 101010;
ll n, q, x; 
vector<ll> vis(MX), dp(MX), g[MX];

void dfs(ll u, ll d){
  vis[u] = 1;
  for (auto v : g[u]){
    if (!vis[v]){
      dp[d]++;
      dfs(v, d + 1);
    }
  }
}

int main(){
  int t;
  cin >> t;
  while (t--){
    cin >> n >> q;
    for (int i = 1; i <= n; i++){
      vis[i] = 0;
      dp[i] = 0;
      g[i].clear();
    }
    dp[1] = 1;
    for (int i = 1; i < n; i++){
      int u, v;
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    dfs(1, 1);
    for (int i = 1; i <= n; i++){
      dp[i] += dp[i - 1];
    }
    for (int i = 1; i <= q; i++){
      cin >> x;
      cout << dp[x] << endl;
    }
  }
}

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 16:32:18
Judged At
2024-11-11 03:33:01
Judged By
Score
100
Total Time
275ms
Peak Memory
6.438 MiB