求助,只能过sub#3
查看原帖
求助,只能过sub#3
671135
qinsishi楼主2025/7/27 20:37

rt,只能过subtask #3。

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

const int N = 1e4+10, V = 1e7+10;
vector<pair<int, int> > G[N];
vector<int> a, b; // 询问的k
bool ok[N], v[V]; // 点分树中的重心,桶数组
int siz[N], wi[N]; // 子树大小,最大的子树大小
int m;

void calc(int rt) {
  // 树的大小
  auto dfs0 = [](auto &&self, int x, int fa) -> void { 
    siz[x] = 1; 
    for (auto &p : G[x]) {
      int &y = p.first;
      if (y == fa || ok[y]) continue;
      self(self, y, x);
      siz[x] += siz[y];
    }
  };
  dfs0(dfs0, rt, 0);
  int n = siz[rt];
  if (n == 1) return;

  // 找重心
  // wi[rt] = siz[rt];
  auto dfs1 = [&n, &rt](auto &&self, int x, int fa) -> void {
    wi[x] = n - siz[x];
    for (auto &p : G[x]) {
      int &y = p.first;
      if (y == fa || ok[y]) continue;
      wi[x] = max(wi[x], siz[y]);  
    }
    if (wi[x] < wi[rt]) rt = x;
    for (auto &p : G[x]) {
      int &y = p.first;
      if (y == fa || ok[y]) continue;
      self(self, y, x);
    }
  };
  dfs1(dfs1, rt, 0);
  ok[rt] = 1;

  // 修改 dis 数组
  auto dfs2 = [](auto &&self, int x, int fa, int dis, bool t) -> void {
    if (dis < V) v[dis] = t;
    for (auto &p : G[x]) {
      int &y = p.first, &w = p.second;
      if (y == fa || ok[y]) continue;
      self(self, y, x, dis+w, t);
    }
  };
  dfs2(dfs2, rt, 0, 0, 0);

  // 查询ans
  auto dfs3 = [](auto &&self, int x, int fa, int dis) -> void {
    for (int i=0; i<m; ++i) 
      if (a[i] >= dis && v[a[i] - dis]) b[i] = 1;
    for (auto &p : G[x]) {
      int &y = p.first, &w = p.second;
      if (y == fa || ok[y]) continue;
      self(self, y, x, dis+w);
    }
  };

  v[0] = 1;
  for (auto &p : G[rt]) {
    int &x = p.first, &w = p.second;
    if (ok[x]) continue;
    dfs3(dfs3, x, rt, w); // 查询
    dfs2(dfs2, x, rt, w, 1); // 修改
  }

  for (auto &p : G[rt]) {
    if (ok[p.first]) continue;
    calc(p.first);
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> m;
  for (int i=1; i<n; ++i) {
    int u, v, w; cin >> u >> v >> w;
    G[u].push_back({v, w});
    G[v].push_back({u, w});
  }
  b.resize(m);
  for (int i=0; i<m; ++i) {
    int x; cin >> x;
    a.push_back(x);
  }
  calc(1);
  for (auto &i : b) cout << (i ? "AYE" : "NAY") << '\n';
  return 0;
}
2025/7/27 20:37
加载中...