/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 328.0 KiB
#2 Accepted 1ms 764.0 KiB
#3 Runtime Error 11ms 2.57 MiB
#4 Memory Exceeded ≥487ms ≥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);
  vector <int> ans(n + 2);
  for (int i = 0; i < n; i++) {
    ans[d[i] + 1] += 1;
  }

  for (int i = 1; i <= n; 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:50:43
Judged At
2024-11-11 03:27:47
Judged By
Score
10
Total Time
487ms
Peak Memory
256.016 MiB