点分治只过了125,其他全WA求助

P3806 【模板】点分治 1

happybob @ 2022-07-14 12:07:02

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

const int N = 1e5 + 5, M = 1e7 + 5;

struct Edge
{
    int v, w;
    Edge(int _v, int _w): v(_v), w(_w){}
};

vector<Edge> G[N];
int n, m;
int dis[N];
bool st[N], f[M];

int get_size(int u, int fa)
{
    if (st[u]) return 0;
    int res = 1;
    for (int i = 0; i < G[u].size(); i++)
    {
        if (G[u][i].v != fa)
        {
            res += get_size(G[u][i].v, u);
        }
    }
    return res;
}

int get_wc(int u, int fa, int& wc, int tot)
{
    if (st[u]) return 0;
    int sum = 1, maxn = 0;
    for (int i = 0; i < G[u].size(); i++)
    {
        if (G[u][i].v != fa)
        {
            int v = get_wc(G[u][i].v, u, wc, tot);
            maxn = max(maxn, v);
            sum += v;
        }
    }
    maxn = max(maxn, tot - sum);
    if (maxn <= tot / 2) wc = u;
    return sum;
}

void get_dist(int u, int fa, int& c, int w)
{
    if (st[u]) return;
    dis[++c] = w;
    for (int i = 0; i < G[u].size(); i++)
    {
        int nx = G[u][i].v;
        if (nx == fa) continue;
        get_dist(fa, u, c, w + G[u][i].w);
    }
}

void Init(int u)
{
    if (st[u]) return;
    get_wc(u, -1, u, get_size(u, -1));
    st[u] = true;
    int cur = 0;
    for (int i = 0; i < G[u].size(); i++)
    {
        int nx = G[u][i].v;
        get_dist(nx, u, cur, G[u][i].w);
    }
    for (int i = 1; i <= cur; i++)
    {
        if (dis[i] <= (int)1e7) f[dis[i]] = 1;
        for (int j = i + 1; j <= cur; j++)
        {
            int w = dis[i] + dis[j];
            if (w > (int)1e7) continue;
            f[w] = 1;
            //printf("%d\n", w);
        }
    }
    for (int i = 0; i < G[u].size(); i++) Init(G[u][i].v);
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; i++)
    {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        G[u].push_back(Edge(v, w));
        G[v].push_back(Edge(u, w));
    }
    Init(1);
    while (m--)
    {
        int k;
        scanf("%d", &k);
        if (f[k]) printf("AYE\n");
        else printf("NAY\n");
    }
    return 0;
}

by happybob @ 2022-07-14 12:07:49

全都是read N,expected A


by happybob @ 2022-07-14 12:27:40

此贴终结


|