[fd-oi]淀粉质(点分治)mengbier学习

文章目录

前言

淀粉质顾名思义这就是淀粉质23333
写下蒟蒻的理解吧…
感觉像是个暴力的优化…
事实上就是将递归的层数控制在log层

例题

传送门

Language:Default
Tree
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 31611 Accepted: 10567

Description

Give a tree with n vertices,each edge has a length(positive integer less than 1001).
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.

Input

The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l.
The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0

Sample Output

8

Source

A

首先当然是那道入门题啦
给出一棵 n n ( 1 n 1 0 5 ) 1 \leq n \leq 10^5) 个节点的树
每条边都有一个权值 w i w_i ( 1 w i 1 0 9 1 \leq w_i \leq 10^9 )
问对于无序点对 ( u , v ) (u,v) 使得 ( u , v ) (u,v) 之间的边权值和 s u m sum ( s u m k sum \leq k
的点对有多少个

  • Part1
    我会超级大暴力
    暴力枚举两个点判断是否合法
  • Part2
    可以暴力枚举每个点 u u 那么就是求出对于以 u u 为LCA的点对有多少个是满足条件的
    则可求出每个点的深度 d e p [ i ] dep[i] (相对于 u u
    sort一下 尺取法可以线性求出
    但是这样递归的层数却是不确定的
    那么总的复杂度也就不能保证
  • Part3
    这时候树的重心出现了…
    我们每次暴力枚举的点可以是重心
    那么这样就可以将递归层数控制在log层

2

上面的是只有一个限制条件的
当有两个限制条件的时候
一维排序 另外一维可以用树桩数组去维护即可

3


不知道这道题的标算是什么…
这是道最近的cf题
感觉也可以用点分治去做
就把每个数进行质因数分解 然后记录一下每个质因数能够到达的最大深度
维护最大值即可

(代码感觉也都差不多

猜你喜欢

转载自blog.csdn.net/LLL_Amazing/article/details/86377138