POJ1741 点分治模板
传送门:http://poj.org/problem?id=1741
题意:
求树上两点间路径长度小于k的点对个数
题解:
参考资料
守望的淀粉质略解:https://www.luogu.org/blog/user9012/dian-fen-zhi-lve-xie
粉红兔大佬的淀粉质:https://www.cnblogs.com/PinkRabbit/p/8593080.html
算法步骤
- 计算重心位置
- 计算答案
- 分治子问题继续求解(1~3)
代码:
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
int n, k, Ans;
struct EDGE {
int v, nxt, w;
} edge[maxn << 1];
int head[maxn], tot;
void add_edge(int u, int v, int w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
bool vis[10001];
int Root, Tsiz, siz[10001], wt[10001];
int arr[10001], cnt;
void GetRoot(int u, int f) {
siz[u] = 1; wt[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if(v != f && !vis[v]) {
GetRoot(v, u);
siz[u] += siz[v];
wt[u] = max(wt[u], siz[v]);
}
}
wt[u] = max(wt[u], Tsiz - siz[u]);
if(wt[Root] > wt[u]) Root = u;
}
void Dfs(int u, int D, int f) {
arr[++cnt] = D;
for(int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if(v != f && !vis[v]) {
Dfs(v, D + edge[i].w, u);
}
}
}
int calc(int u, int D) {
cnt = 0; Dfs(u, D, 0); int l = 1, r = cnt, sum = 0;
sort(arr + 1, arr + cnt + 1);
for(;; ++l) {
while(r && arr[l] + arr[r] > k) --r;
if(r < l) break;
sum += r - l + 1;
}
return sum;
}
void DFS(int u) {
Ans += calc(u, 0); vis[u] = 1;
for(int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if(!vis[v]) {
Ans -= calc(v, edge[i].w);
Root = 0, Tsiz = siz[v], GetRoot(v, 0);
DFS(Root);
}
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
while(~scanf("%d%d", &n, &k) && n && k) {
tot = Ans = 0;
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));
for(int i = 2, u, v, w; i <= n; i++) {
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
}
wt[0] = INF;
Tsiz = n;
GetRoot(1, 0);
DFS(Root);
printf("%d\n", Ans - n);
}
return 0;
}
POJ1741 点分治模板的更多相关文章
- POJ1741 tree (点分治模板)
题目大意: 给一棵有 n 个顶点的树,每条边都有一个长度(小于 1001 的正整数).定义 dist(u,v)=节点 u 和 v 之间的最小距离.给定一个整数 k,对于每一对 (u,v) 顶点当且仅当 ...
- 洛谷P2634 [国家集训队]聪聪可可 点分治模板
题意 在一棵树上任意选两个点,求它们距离模3为0的概率. 分析 树分治模板 Code #include<bits/stdc++.h> #define fi first #define se ...
- (模板)luoguP3806(树上点分治模板题)
点分治的写法1: 题目链接:https://www.luogu.org/problem/P3806 题意:给出一颗带边权的树,结点数n<=1e4,每条边有权值<=1e4,有m组询问(m&l ...
- [POJ1741]Tree(点分治模板)
传送门 良心解析 其实以前在求某段序列上的区间统计问题时就碰到过类似于这样的思想. 当时的区间统计问题思路大致是这样: 选取一个点作为中间点,从这个点的左边和右边统计出满足条件的点对.然后当前的中间点 ...
- POJ1741 Tree 树分治模板
http://poj.org/problem?id=1741 题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数. dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...
- 点分治模板(洛谷P4178 Tree)(树分治,树的重心,容斥原理)
推荐YCB的总结 推荐你谷ysn等巨佬的详细题解 大致流程-- dfs求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...
- Luogu P3806 点分治模板1
题意: 给定一棵有n个点的树询问树上距离为k的点对是否存在. 分析: 这个题的询问和点数都不多(但是显然暴力是不太好过的,即使有人暴力过了) 这题应该怎么用点分治呢.显然,一个模板题,我们直接用套路, ...
- POJ - 1741 - Tree - 点分治 模板
POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...
- 【Luogu】P3806点分治模板(点分治)
题目链接 wc听不懂lca讲的高等数学专场(一个字都听不懂),然后就自学了点分治. 点分治就是我先处理完跟根有关的东西,然后把根标记掉,把原树拆成若干个联通块,然后分别对每个联通块(每个小树)搞一模一 ...
随机推荐
- Vagrant-安装教程及常见问题
http://ju.outofmemory.cn/entry/346215 前言: Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境. 它的主要意义是让所有开发人员都使用和线上服务 ...
- Selenium-----wait的三种等待
在UI自动化测试中,必然会遇到环境不稳定,网络慢的情况,这时如果你不做任何处理的话,代码会由于没有找到元素,而报错.这时我们就要用到wait(等待),而在Selenium中,我们可以用到一共三种等待, ...
- dataframe添加元素指定为列表,不同for循环命名空间下的变量重复问题
split=pd.DataFrame({'data':[0],'len':0,'count':0},index=[0])for i_t in range(over_128.shape[0]): ct= ...
- Java“封装”的例子
/*功能:Java"封装"的典型例子*/ public class Demo3_5{ public static void main(String args[]){ ...
- Java练习 SDUT-2746_大小写转换
大小写转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description X现在要学习英文以及各种稀奇古怪的字符的了.现在他想把一串字 ...
- 【Flask源码分析——请求上下文与应用上下文】
Flask中有两种上下文,请求上下文和应用上下文.两者的作用域都处于一个请求的局部中. 查看源代码,上下文类在flask.ctx模块中定义 AppContext类定义应用上下文,app是当前应用Web ...
- oracle函数 sys_guid()
[功能]生产32位的随机数,不过中间包括一些大写的英文字母. [返回]长度为32位的字符串,包括0-9和大写A-F [示例] select sys_guid() from dual
- oracle函数 localtimestamp
[功能]:返回会话中的日期和时间 [参数]:没有参数,没有括号 [返回]:日期 [示例]select localtimestamp from dual; 返回:14-11月-08 12.35.37.4 ...
- CSS3表达式calc( )
CSS3表达式calc( ) 第一次看到calc( )时,不太相信calc()是css中的部分.因为看其外表像个函数,但是CSS里为啥会有表达式我也不太清楚,偶然机会在网页里看到的,自己切片写自适应时 ...
- canvas+js实现验证码功能
转载自:https://blog.csdn.net/qq_42463851/article/details/90755734<!DOCTYPE html> <html> < ...