传送门: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. 计算重心位置
  2. 计算答案
  3. 分治子问题继续求解(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 点分治模板的更多相关文章

  1. POJ1741 tree (点分治模板)

    题目大意: 给一棵有 n 个顶点的树,每条边都有一个长度(小于 1001 的正整数).定义 dist(u,v)=节点 u 和 v 之间的最小距离.给定一个整数 k,对于每一对 (u,v) 顶点当且仅当 ...

  2. 洛谷P2634 [国家集训队]聪聪可可 点分治模板

    题意 在一棵树上任意选两个点,求它们距离模3为0的概率. 分析 树分治模板 Code #include<bits/stdc++.h> #define fi first #define se ...

  3. (模板)luoguP3806(树上点分治模板题)

    点分治的写法1: 题目链接:https://www.luogu.org/problem/P3806 题意:给出一颗带边权的树,结点数n<=1e4,每条边有权值<=1e4,有m组询问(m&l ...

  4. [POJ1741]Tree(点分治模板)

    传送门 良心解析 其实以前在求某段序列上的区间统计问题时就碰到过类似于这样的思想. 当时的区间统计问题思路大致是这样: 选取一个点作为中间点,从这个点的左边和右边统计出满足条件的点对.然后当前的中间点 ...

  5. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

  6. 点分治模板(洛谷P4178 Tree)(树分治,树的重心,容斥原理)

    推荐YCB的总结 推荐你谷ysn等巨佬的详细题解 大致流程-- dfs求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...

  7. Luogu P3806 点分治模板1

    题意: 给定一棵有n个点的树询问树上距离为k的点对是否存在. 分析: 这个题的询问和点数都不多(但是显然暴力是不太好过的,即使有人暴力过了) 这题应该怎么用点分治呢.显然,一个模板题,我们直接用套路, ...

  8. POJ - 1741 - Tree - 点分治 模板

    POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...

  9. 【Luogu】P3806点分治模板(点分治)

    题目链接 wc听不懂lca讲的高等数学专场(一个字都听不懂),然后就自学了点分治. 点分治就是我先处理完跟根有关的东西,然后把根标记掉,把原树拆成若干个联通块,然后分别对每个联通块(每个小树)搞一模一 ...

随机推荐

  1. 深入理解spring注解之@ComponentScan注解

    今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1,@ComponentScan注解是什么 其实 ...

  2. oracle表内连接和外连接

    n  概述 表连接分为内连接和外连接 n  内连接 内连接实际上就是利用where子句对两张表形成的笛卡尔集进行筛选,我们前面学习的查询都是内连接,也是在开发过程中用的最多的连接查询. 基本语法: s ...

  3. UVA_10071:Back to High School Physics

    Language:C++ 4.8.2 #include<stdio.h> int main(void) { int v, t; while(scanf("%d%d", ...

  4. C++讲课总结 标签: c++总结 2015-02-28 14:48 671人阅读 评论(25) 收藏

    昨天老师算是给串了一本C++ 的课本,根据自己的理解,赶紧记录一下,也好作为自己学习时候的根据. C++编程简介:每本讲语言的书,第一章总是简介,内容无非是发展历史,语言特色等东西,专业的东西不多,都 ...

  5. SDUT-2116_数据结构实验之链表一:顺序建立链表

    数据结构实验之链表一:顺序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入N个整数,按照输入的顺序建立单链 ...

  6. 蓝牙(3)蓝牙UUID与SDP

    1.服务发现协议 (SDP) SDP = Service Discovery Protocol  主要用来根据已分配编号(UUID)搜索服务.浏览群组列表.文档 URL 和图标 URL等. 详细见: ...

  7. 创建我的flask第一个应用(一)

    本地python版本 (venv) dongge@dongge-PC:/data/webroot$ python --version Python 3.6.5 创建flask命令 $mkdir myp ...

  8. redhat6.5安装oracle11_2R

    参照前人一步一步操作: http://leihenzhimu.blog.51cto.com/3217508/1685164 遇到如下错误: This is a prerequisite conditi ...

  9. python初识参数

    1. 什么是函数? f(x) = x + 1 y = x + 1 函数是对功能或者动作的封装 2. 函数的语法和定义 def 函数名(): 函数体 调用: 函数名() 3. 关于函数的返回值 retu ...

  10. 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的差别和联系

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/weasleyqi/article/details/33396809 1.首先先了解下NSNumber ...