社区讨论

0pts 求调

P3806【模板】点分治参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mm5lcxwj
此快照首次捕获于
2026/02/28 08:37
上周
此快照最后确认于
2026/03/01 21:00
上周
查看原帖
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int kMaxN = 1e4 + 5, V = 1e7 + 5;

struct V {
  vector<pair<int, int>> e;
  int s;
} v[kMaxN];
int n, k, m, p = -1, a[V], ans;
vector<int> l;
bool b[kMaxN];

void Fh(int f, int x) {
  v[x].s = 1;
  int m = 0;
  for (auto i : v[x].e) {
    if (!b[i.first] && i.first != f) {
      Fh(x, i.first);
      if (p != -1) {
        return;
      }
      m = max(m, v[i.first].s), v[x].s += v[i.first].s;
    }
  }
  if (max(m, n - v[x].s) <= n / 2) {
    p = x;
    v[f].s = n - v[x].s;
  }
}

void Dfs(int f, int x, int y) {
  if (y > k) {
    return;
  }
  ans += a[k - y] + (y == k);
  l.push_back(y);
  for (auto i : v[x].e) {
    if (!b[i.first] && i.first != f) {
      Dfs(x, i.first, y + i.second);
    }
  }
}

void Solve(int x) {
  for (auto i : v[x].e) {
    if (!b[i.first] && i.first != x) {
      int s = l.size();
      Dfs(x, i.first, i.second);
      for (int j = s; j < l.size(); j++) {
        a[l[j]]++;
      }
    }
  }
  for (auto i : l) {
    a[i]--;
  }
  l.clear();
  b[x] = 1;
  for (auto i : v[x].e) {
    if (!b[i.first]) {
      n = v[i.first].s, p = -1;
      Fh(0, i.first);
      Solve(p);
    }
  }
}
signed main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1, x, y, z; i < n; i++) {
    cin >> x >> y >> z;
    v[x].e.emplace_back(y, z), v[y].e.emplace_back(x, z);
  }
  int val = n;
  while (m--) {
    fill(b + 1, b + n + 1, 0);
    cin >> k;
    n = val;
    ans = 0;
    Fh(0, 1);
    Solve(p);
    cout << (ans ? "AYE\n" : "NAY\n");
  }
  return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...