/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.066 MiB
#2 Accepted 3ms 2.066 MiB
#3 Accepted 213ms 5.965 MiB
#4 Accepted 3ms 2.062 MiB
#5 Accepted 3ms 2.066 MiB

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i,n) for(ll i=0;i<n;i++)
#define m(a,b,c) max(a,max(b,c))
#define N ll n;cin >> n;
#define Nk ll n,k;cin >> n >> k;
#define A ll a[n];f(i,n) cin >> a[i];
#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr);
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define S string s;cin>>s;
#define pb push_back

vector<vector<ll>> adj;
vector<bool> visited;
vector<ll> depth;

void dfs(ll node, ll d) {
    visited[node] = true;
    depth[node] = d;

    for (ll neighbor : adj[node]) {
        if (!visited[neighbor]) {
            dfs(neighbor, d + 1);
        }
    }
}

int main()
{
   FAST_IO;
   ll t;
   cin>>t;
   while(t--)
   {
        N;
        ll q;
        cin>>q;
        adj.resize(n + 1);
        visited.resize(n + 1, false);
        depth.resize(n + 1);
        for (ll i = 1; i < n; ++i) 
        {
            ll u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        
        dfs(1, 0);
        ll p=pow(10,5);
        ll tom[p+1]={0};
        for(ll i=1;i<=n;i++)
        {
            tom[depth[i]]++;
        }
        ll c[p+1];
        c[0]=1;
        f(i,p+1)
        {
            c[i+1]=c[i]+tom[i+1];
        }
        f(i,q)
        {
            ll x;
            cin>>x;
            cout<<c[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 16:23:08
Judged At
2024-11-11 03:33:09
Judged By
Score
100
Total Time
213ms
Peak Memory
5.965 MiB