/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 832.0 KiB
#2 Accepted 2ms 836.0 KiB
#3 Runtime Error 11ms 2.719 MiB
#4 Memory Exceeded ≥485ms ≥256.016 MiB

Code

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
using namespace std;

void solve(int cs) {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    int u, v;
    cin >> u >> v, --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  vector<int> d(n, 0);
  auto dfs = [&](auto &&self, int node, int par) -> void {
    for (auto &nodes : g[node]) {
      if (nodes != par) {
        d[nodes] = d[node] + 1;
        self(self, nodes, node);
      }
    }
  };
  
  dfs(dfs, 0, -1);
  int lim = 1e5 + 2;
  vector <int> ans(lim);
  for (int i = 0; i < n; i++) {
    ans[d[i] + 1] += 1;
  }

  for (int i = 1; i < lim; i++) ans[i] += ans[i - 1];

  while (q--) {
    int v;
    cin >> v;
    cout << ans[v - 1] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  cin >> tc;
  for (int cs = 1; cs <= tc; cs++) {
    solve(cs);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1053 Water on Tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-11 03:53:52
Judged At
2024-11-11 03:27:46
Judged By
Score
10
Total Time
485ms
Peak Memory
256.016 MiB