蒟蒻刚学淀粉质,求助QAQ

P3806 【模板】点分治 1

NaCly_Fish @ 2019-01-07 19:04:18

rt
好像少算了一些,该输出是的地方很多输出否
而且莫名T了,不知道是不是重心找错了

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<bitset>
#define N 10002
#define inf 0x3f3f3f3f
using namespace std;

struct edge{
    int v,w;
    edge(int v=0,int w=0):v(v),w(w){}
};

vector<edge> adj[N];
int ext[10000002]; //ext[i]表示长为i的路径数 
int dis[N],size[N],son[N],node[N];//size是子树大小
bool vis[N];
int n,m,r,sum,cnt;

inline void read(int &x);
void get_root(int u,int fa);
void partition(int u);
void get_dist(int u,int fa,int len);
void solve(int u,int len,int w);

int main(){
    int u,v,w,k;
    read(n),read(m);
    for(int i=1;i<n;++i){
        read(u),read(v),read(w);
        adj[u].push_back(edge(v,w));
        adj[v].push_back(edge(u,w));
    }
    sum = son[r] = n;
    get_root(1,0);
    partition(r);
    for(int i=1;i<=m;++i){
        read(k);
        printf(ext[k]>0?"AYE\n":"NAY\n");
    }
    return 0;
}

void solve(int u,int len,int w){
    cnt = 0;
    dis[u] = len;
    get_dist(u,0,len);
    for(int i=1;i<=cnt;++i){
        for(int j=1;j<=cnt;++j){
            if(i==j) continue;
            ext[node[i]+node[j]] += w;
        }
    }
}

void get_dist(int u,int fa,int len){
    int v,l = adj[u].size();
    node[++cnt] = dis[u];
    for(int i=0;i<l;++i){
        v = adj[u][i].v;
        if(v==fa||vis[v]) continue;
        dis[v] = dis[u]+adj[u][i].w;
        get_dist(v,u,dis[v]);
    }
}

void partition(int u){
    solve(u,0,1);
    vis[u] = true;
    int v,l = adj[u].size();
    for(int i=0;i<l;++i){
        v = adj[u][i].v;
        if(vis[v]) continue;
        solve(u,adj[u][i].w,-1);
        sum = size[u];
        r = 0;
        son[r] = n;
        get_root(v,u);
        partition(r);
    }
}

void get_root(int u,int fa){
    size[u] = 1,son[u] = 0;
    int v,l = adj[u].size();
    for(int i=0;i<l;++i){
        v = adj[u][i].v;
        if(v==fa||vis[v]) continue;
        get_root(v,u);
        size[u] += size[v];
        son[u] = max(son[u],size[v]);
    }
    son[u] = max(son[u],sum-size[u]);
    if(son[u]<son[r]) r = u;
}

inline void read(int &x){
    x = 0;
    char c = getchar();
    while(c<'0'||c>'9') c = getchar();
    while(c>='0'&&c<='9'){
        x = (x<<3)+(x<<1)+(c^48);
        c = getchar();
    }
}

by NaCly_Fish @ 2019-01-07 19:18:29

on test 2:

input:

12 3
1 2 16
1 3 17
3 4 19
4 5 8
5 6 7
1 7 19
1 8 2
7 9 18
5 10 0
7 11 11
5 12 7
16
13
29

output:

NAY
NAY
AYE

answer:

AYE
NAY
AYE

by NaCly_Fish @ 2019-01-07 19:27:06

A掉了。。只是因为solve的地方把v错写成u了。。然后又T又WA


by L_M_ @ 2019-02-18 14:51:01

%%%%dalao


|