Tree 点分治

题目描述

给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K

输入输出格式

输入格式:

N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k

输出格式:

一行,有多少对点之间的距离小于等于k

输入输出样例

输入样例#1: 
7
1 6 13 
6 3 9 
3 5 7 
4 1 3 
2 4 20 
4 7 2 
10
输出样例#1: 
5





#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline int read() {
    int res=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
    return res;
}
#define reg register
#define N 200005
int n, tot, k;
long long ans;

int head[N], cnt = 1;
struct edge {
    int nxt, to, val;
}ed[N*2];
inline void add(int x, int y, int z)
{
    ed[++cnt] = (edge){head[x], y, z};
    head[x] = cnt;
}

bool cut[N*2];


int siz[N], root, mrt = 1e9;
void dfs(int x, int fa)
{
    siz[x] = 1;
    for (reg int i = head[x] ; i ; i = ed[i].nxt)
    {
        int to = ed[i].to;
        if (to == fa or cut[i]) continue;
        dfs(to, x);
        siz[x] += siz[to];
    }    
}    
void efs(int x, int fa)
{
    int tmp = tot - siz[x];
    for (reg int i = head[x] ; i ; i = ed[i].nxt)
    {
        int to = ed[i].to;
        if (to == fa or cut[i]) continue;
        efs(to, x);
        tmp = max(tmp, siz[to]);
    }
    if (tmp < mrt) mrt = tmp, root = x;
}    
inline int FindRoot(int x)
{
    return x;
    dfs(x, 0);
    mrt = 1e9;
    tot = siz[x];
    root = n;
    efs(x, 0);
    return root;
}


int a[N];
int top;
void Work(int x, int fa, int d)
{
    a[++top] = d;
    for (reg int i = head[x] ; i ; i = ed[i].nxt)
    {
        int to = ed[i].to;
        if (to == fa or cut[i]) continue;
        Work(to, x, d + ed[i].val);
    }
}
inline int Calc(int x, int d)
{
    int res = 0;
    top = 0;
    Work(x, 0, d);
    sort (a + 1, a + 1 + top);
    int l = 1, r = top;
    while (l < r)
    {
        while(a[l] + a[r] > k and l < r) r--;
        res += r - l, l ++;
    }        
    return res;
}
void solve(int rt)
{
    root = FindRoot(rt);
    ans += Calc(root, 0);
    for (reg int i = head[root] ; i ; i = ed[i].nxt)
    {
        int to = ed[i].to;
        if (cut[i]) continue;
        cut[i] = cut[i ^ 1] = 1;
        ans -= Calc(to, ed[i].val);
        solve(to);
    }
}
    

int main()
{
    n = read();
    for (reg int i = 1 ; i < n ; i ++)
    {
        int x = read(), y = read(), z = read();
        add(x, y, z), add(y, x, z);
    } k = read();
    solve(1);
    printf("%d\n", ans);
    return 0;
}

 



转载于:https://www.cnblogs.com/BriMon/p/9479963.html

K-D Tree(K-Dimensional Tree)算法是一种基于分治法的数据结构,用于高维空间的搜索和排序。它的基本思想是将多维空间中的点以某种方式分割成更小的子空间,然后在每个子空间中递归地进行搜索。这样可以大大降低搜索的复杂度。 具体来说,K-D Tree算法可以分为以下几步: 1. 选择一个维度,将数据点按照该维度的值进行排序。 2. 找到该维度的中位数,将其作为当前节点,并将数据点分为左右两个子集。 3. 递归地构建左子树和右子树,每次选择一个新的维度进行划分。 4. 最终得到一个K-D Tree。 在搜索时,我们可以从根节点开始,按照一定的规则向下遍历,直到找到目标点或者无法继续向下搜索。具体的规则是: 1. 如果目标点在当前节点的左子树中,则继续向左子树搜索。 2. 如果目标点在当前节点的右子树中,则继续向右子树搜索。 3. 如果目标点和当前节点在选定的维度上的值相等,则说明已经找到目标点。 分治法是一种常见的算法思想,它将一个大规模的问题分解成若干个小规模的子问题,每个子问题独立地求解,然后将这些子问题的解合并起来得到原问题的解。分治法通常包含三个步骤:分解、求解、合并。 具体来说,分治法可以分为以下几步: 1. 分解:将原问题分成若干个子问题,每个子问题规模较小且结构与原问题相同。 2. 求解:递归地求解每个子问题,直到问题规模足够小可以直接求解。 3. 合并:将所有子问题的解合并成原问题的解。 分治法的优点是可以有效地降低算法的时间复杂度。但是它的缺点是需要额外的空间来存储子问题的解,而且分解和合并的过程也需要耗费一定的时间。因此,需要根据实际情况选择合适的算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值