码迷,mamicode.com
首页 > 其他好文 > 详细

LuoguP3806 【模板】点分治1 (点分治)

时间:2019-07-16 12:20:24      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:||   got   template   namespace   bug   ons   lse   include   mes   

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")

#else

#define D_e_Line ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std; 
const int N = 10007;
const int MAXX = 10000000;

struct Edge{
    int nxt,pre,w;
}e[N<<1];
int cntEdge,head[N];
inline void add(int u,int v,int w){
    e[++cntEdge] = (Edge){head[u], v, w}, head[u] = cntEdge;
}

int minn, root, treeSize;

int vis[N],siz[N];
inline void GetRoot(int u,int fa){
    siz[u] = 1;
    int sum = 0;
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(vis[v] || v == fa) continue;
        GetRoot(v, u);
        sum = Max(sum, siz[v]);
        siz[u] += siz[v];
    }
    sum = Max(sum, treeSize - siz[u]);
    if(sum < minn){
        minn = sum;
        root = u;
    }
}

int dis[N];

int tot;
inline void GetDistance(int u,int fa,int w){
    dis[++tot] = w;
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(vis[v] || v == fa) continue;
        GetDistance(v, u, w + e[i].w);
    }
}

int num[MAXX];
inline void Solve(int u,int w,int flag){ // w is the influence of the beginning node
    tot = 0;
    GetDistance(u, u, 0);
    R(i,1,tot){
        R(j,1,tot){
            if(flag && dis[i] + dis[j] <= MAXX)
                ++ num[dis[i] + dis[j]];
            else
                if(dis[i] + dis[j] +w <= MAXX)
                    -- num[dis[i] + dis[j] + w];
        }
    }
}

inline void DFS(int u){
    Solve(u, 0, 1);
    vis[u] = true;
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(vis[v]) continue;
        Solve(v, (e[i].w << 1), 0); // QAQ // \ T^T /
        treeSize = siz[v];
        minn = 0x7fffffff;
        GetRoot(v, u);
        DFS(root);
    }
}

int main(){
    int n,m;
    io >> n >> m;
    R(i,2,n){
        int u,v,w;
        io >> u >> v >> w;
        add(u, v, w);
        add(v, u, w);
    }
    
    treeSize = n; // because I forgot this sentence, the whole morning...
    minn = 0x7fffffff;
    GetRoot(1, 1);
    DFS(root);
    
    while(m--){
        int x;
        io >> x;
        if(num[x])
            printf("AYE\n");
        else
            printf("NAY\n");
    }
    
    return 0;
}

LuoguP3806 【模板】点分治1 (点分治)

标签:||   got   template   namespace   bug   ons   lse   include   mes   

原文地址:https://www.cnblogs.com/bingoyes/p/11194071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!