跪求巨佬帮助,点分治全T

P3806 【模板】点分治 1

Editzed @ 2022-11-05 21:48:25

#include<bits/stdc++.h>
using namespace std;
using cint = int;
constexpr int maxn = 20010,inf= 2e9;
struct {
    int nxt, v, w;
}edge[maxn << 1];
int siz[maxn],rt,maxq,sum,maxsiz,minsiz,dis[maxn],discnt,n, m, u, v, w, k, cnt, head[maxn],q[maxn];
bool vis[maxn],has[10000010],res[maxn];
queue<int> tip;
inline void addedge(cint u, cint v, cint w) {
    edge[++cnt] = { .nxt = head[u],.v = v,.w = w };
    head[u] = cnt;
}
void getsiz(cint x, cint fa) {
    siz[x] = 1;
    maxsiz = 0;
    for (auto i = head[x]; i; i = edge[i].nxt) {
        const auto v = edge[i].v;
        if (v == fa || vis[x]) continue;
        getsiz(v, x);
        siz[x] += siz[v];
        maxsiz = max(siz[v], maxsiz);
    }
    maxsiz = max(maxsiz, sum - siz[x]);
    if (maxsiz < minsiz) rt = x, minsiz = maxsiz;
}
void getdis(cint x, cint fa,cint disx) {
    if (disx < 10000010) dis[++discnt] = disx;
    for (auto i = head[x]; i; i = edge[i].nxt) {
        const auto v = edge[i].v;
        if (v == fa || vis[x]) continue;
        getdis(v, x, disx + edge[i].w);
    }
}
inline void check(cint dis) {
    for (int i = 1; i <= m; ++i) if (q[i] >= dis) res[i] |= has[q[i] - dis];
}

void dfs(cint x, cint fa) {
    has[0] = vis[x] = 1;
    tip.push(0);
    for (int i = head[x]; i; i = edge[i].nxt) {
        const int v = edge[i].v;
        if (v == fa || vis[v]) continue;
        discnt = 0;
        getdis(v, x, edge[i].w);
        for (int j = 1; j <= discnt; ++j) check(dis[j]);
        for (int j = 1; j <= discnt; ++j) tip.push(dis[j]),has[dis[j]] = 1;
    }
    while (!tip.empty())
        has[tip.front()] = 0,tip.pop();
    for (int i = head[x]; i; i = edge[i].nxt) {
        const int v = edge[i].v;
        if (v == fa || vis[v]) continue;
        sum = siz[v];
        rt = 0;
        minsiz = inf;
        getsiz(v, x);
        getsiz(rt, -1);
        dfs(rt, x);
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i < n; ++i) {
        cin >> u >> v >> w;
        addedge(u, v, w), addedge(v, u, w);
    }
    for (int i = 1; i <= m; ++i) cin >> q[i],maxq = max(maxq,q[i]);
    rt = 0;
    minsiz = inf;
    sum = n;
    getsiz(1, -1);
    getsiz(rt, -1);
    dfs(rt, -1);
    for (int i = 1; i <= m; ++i) {
        if (res[i]) cout << "AYE\n";
        else cout << "NAY\n";
    }
}

谢谢!


by cool_milo @ 2022-11-05 22:24:49

@Editzed 第20行和第32行的v写成x了 第17行maxsze应该设成局部变量


by zhenjianuo2025 @ 2022-11-05 22:56:22

orz


by Editzed @ 2022-11-06 07:36:40

@cool_milo

跪谢巨佬!


|