zoukankan      html  css  js  c++  java
  • 洛谷P3806 点分治

    点分治

    第一次写点分治。。感觉是一个神奇而又暴力的东西orz
    点分治大概就是用来处理树上链的信息,把路径分成过点x和不过点x的两种,不过点x的路径可以变成过点x的子树中一点的路径,递归处理

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 50005;
    int n, m, cnt, rt, ans, tot, sum, head[N], size[N], dis[N], rd[N], query[N], all[N];
    bool vis[N], judge[N], res[N];
    struct Edge{ int v, next, w; } edge[N<<2];
    
    void addEdge(int a, int b, int c){
        edge[cnt].v = b, edge[cnt].w = c, edge[cnt].next = head[a], head[a] = cnt ++;
    }
    
    void dfs(int s, int fa){
        size[s] = 1;
        int mp = 0;
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(u == fa || vis[u]) continue;
            dfs(u, s);
            size[s] += size[u];
            mp = max(mp, size[u]);
        }
        mp = max(mp, sum - size[s]);
        if(mp < ans) ans = mp, rt = s;
    }
    
    void getDis(int s, int fa){
        rd[++tot] = dis[s];
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(u == fa || vis[u]) continue;
            dis[u] = dis[s] + edge[i].w;
            getDis(u, s);
        }
    }
    
    void calc(int s){
        int num = 0;
        judge[0] = true;
        for(int i = head[s]; i != -1; i = edge[i].next){
            tot = 0; int u = edge[i].v;
            if(vis[u]) continue;
            dis[u] = edge[i].w, rd[0] = 0;
            getDis(u, s);
            for(int j = 1; j <= tot; j ++){
                for(int k = 0; k < m; k ++){
                    if(query[k] >= rd[j]) res[k] |= judge[query[k] - rd[j]];
                }
            }
            for(int j = 1; j <= tot; j ++){
                all[++num] = rd[j], judge[rd[j]] = true;
            }
        }
        for(int i = 1; i <= num; i ++) judge[all[i]] = false;
    }
    
    void solve(int s){
        vis[s] = true, calc(s);
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(vis[u]) continue;
            ans = INF, sum = size[u];
            dfs(u, 0), solve(rt);
        }
    }
    
    int main(){
    
        full(head, -1);
        n = read(), m = read();
        for(int i = 0; i < n - 1; i ++){
            int u = read(), v = read(), c = read();
            addEdge(u, v, c), addEdge(v, u, c);
        }
        for(int i = 0; i < m; i ++) query[i] = read();
        ans = INF, sum = n;
        dfs(1, 0), solve(rt);
        for(int i = 0; i < m; i ++) printf(res[i] ? "AYE
    " : "NAY
    ");
        return 0;
    }
    
  • 相关阅读:
    php socket 客户端代码
    linux crontab定时执行
    加载 pcntl 多进程
    Xdebug 配置
    Zend Debugger 配置
    windows SVN搭建
    深度学习笔记:优化方法总结(BGD,SGD,Momentum,AdaGrad,RMSProp,Adam)
    操作系统-分段机制
    C++中的new、operator new与placement new
    线程安全的概念
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10718298.html
Copyright © 2011-2022 走看看