/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 2.836 MiB
#2 Accepted 4ms 2.816 MiB
#3 Accepted 41ms 4.109 MiB
#4 Accepted 3ms 2.82 MiB
#5 Accepted 3ms 2.816 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
vector<int>adj[N];
bool vis[N];
int level[N];   
void bfs(int start) {
    queue<int> q;
    q.push(start);
    vis[start] = true;
    level[start] = 0;
    while (!q.empty()) {
        int node = q.front();
        q.pop();

        for (int neighbor : adj[node]) {
            if (!vis[neighbor]) {
                q.push(neighbor);
                level[neighbor] = level[node] + 1;
                vis[neighbor] = true;
            }
        }
    }
}
void solve() {
    int n, q;
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        adj[i].clear();
        level[i] = -1;
        vis[i] = false;
    }
    for(int i = 0; i < n - 1; i++){
        int u, v;
        cin >> u >> v;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    bfs(1);
    vector<int>levels;
    for(int i = 1; i <= n; i++){
        levels.push_back(level[i]);
    }
    sort(levels.begin(), levels.end());
    
    while(q--){
        int x;
        cin >> x;
        int ans = upper_bound(levels.begin(), levels.end(), x) - levels.begin();

        cout << ans << '\n';
    }


}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1053 Water on Tree
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 23:37:24
Judged At
2024-11-11 02:43:47
Judged By
Score
100
Total Time
41ms
Peak Memory
4.109 MiB