点分治模板题

https://www.luogu.org/problem/P3806

#include<bits/stdc++.h>
using namespace std;

const int maxn=2e4+10;
int head[maxn],ver[maxn],nxt[maxn],edge[maxn];
int tot;
int vis[maxn]; // 分治时用来标记哪个重心已经使用过
int ans,size[maxn],root,sum;
// 求重心时使用,ans记录目前标记重心最小子树大小,size记录子树大小 ,root为重心,sum为目前该分治的子树大小
//map<int,int> m;
int m[100000000],mcnt,mm[maxn]; // m[i]用来标记每个子节点到根的距离为i是否存在,mm用来清空m时用不然会T。
int dis[maxn],bian[maxn],cnt; // dis[i]记录子节点到目前根的距离,bian记录每个存在的距离
int q[1005],judge[1005];
int n,k;

void add(int x,int y,int z)
{
    ver[++tot]=y;
    edge[tot]=z;
    nxt[tot]=head[x];
    head[x]=tot;
}
void dfs_find(int x,int fa) // 求重心
{
    size[x]=1;
    int max_part=0;
    for(int i=head[x]; i; i=nxt[i])
    {
        int y=ver[i];
        if(y==fa||vis[y]) continue;
        dfs_find(y,x);
        size[x]+=size[y];
        max_part=max(max_part,size[y]);
    }
    max_part=max(max_part,sum-size[x]);
    if(ans>max_part)
    {
        ans=max_part;
        root=x;
    }
}

void dfs(int x,int fa)
{
    bian[++cnt]=dis[x];
    for(int i=head[x]; i; i=nxt[i])
    {
        int y=ver[i];
        if(vis[y]||y==fa) continue;
        dis[y]=edge[i]+dis[x];
        dfs(y,x);
    }
}
void solve(int x) // 分治为子树解决,每次给一个子树的重心。
{
    mcnt=0;
    for(int i=head[x]; i; i=nxt[i])
    {
        cnt=0;
        int y=ver[i];
        dis[y]=edge[i];
        if(vis[y]) continue;
        dfs(y,x);
        for(int j=1; j<=cnt; j++)
        {
            for(int l=1; l<=k; l++)
                if(q[l]-bian[j]>=0&&(m[q[l]-bian[j]]==1||q[l]==bian[j]))
                    judge[l]=1;
        }
        for(int j=1; j<=cnt; j++)
        {
            m[bian[j]]=1;
            mm[++mcnt]=bian[j];
        }
    }
    for(int i=1;i<=mcnt;i++)
        m[mm[i]]=0;
}
void divide(int x) //  划分子树
{
    vis[x]=1;
    solve(x);
    for(int i=head[x]; i; i=nxt[i])
    {
        int y=ver[i];
        if(vis[y]) continue;
        ans=sum=size[y];
        dfs_find(y,0);
        divide(root);
    }
}

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n-1; i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    for(int i=1; i<=k; i++)
        scanf("%d",&q[i]);
    ans=sum=n;
    dfs_find(1,0);
    divide(root);
    for(int i=1; i<=k; i++)
    {
        if(judge[i]) printf("AYE\n");
        else printf("NAY\n");
    }
}

原文地址:https://www.cnblogs.com/dongdong25800/p/11567160.html

时间: 2024-07-31 22:27:03

点分治模板题的相关文章

POJ 1741 树分治(点分治模板题)

POJ 1741 题意:求一棵树中点对<=k的数量. 总结:点分治,搞不太懂..大概敲了一遍 #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<queue> #include<stac

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

几道树剖模板题

寒假后半段一直都在外出旅游..颓了好久..qaq 旅游期间写了几道树剖模板题,贴上来.. BZOJ 1036 没啥好说的,裸题 1 #include <cstdio> 2 #include <algorithm> 3 4 #define LEFT (segt[cur].l) 5 #define RIGHT (segt[cur].r) 6 #define MID (segt[cur].mid) 7 #define SUM (segt[cur].Sum) 8 #define MAX (

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

洛谷P3381——费用流模板题

嗯..随便刷了一道费用流的模板题....来练练手. #include<iostream> #include<cstdio> #include<cstring> using namespace std; int h[5210],d[5210],used[5210],que[100010],last[5210]; int k=1,INF=0x7fffffff,ans1=0,ans2=0; inline int read(){ int t=1,num=0; char c=ge

HDU 1251 Trie树模板题

1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b

LA 4670 出现次数最多的子串 (AC自动机模板题)

Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; ea

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i