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讲的高等数学专场(一个字都听不懂),然后就自学了点分治. 点分治就是我先处理完跟根有关的东西,然后把根标记掉,把原树拆成若干个联通块,然后分别对每个联通块(每个小树)搞一模一 ...
随机推荐
- Length of Last Word输出最后单词的字母个数
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Maven学习总结--maven入门(一)
一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理.
- win7 64位下如何安装配置mysql-5.7.4-m14-winx64(安装记录)
1. mysql-5.7.4-m14-winx64.zip下载 官方网站下载地址:http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17 ...
- docker查看运行容器详细信息
使用docker ps命令可以查看所有正在运行中的容器列表, 使用docker inspect命令我们可以查看更详细的关于某一个容器的信息. $ docker inspect 容器id/image
- GIT 公钥配置
1.下载git 2.ssh-keygen -t rsa -C "xxx@xxx.com" 3.cd ~/.ssh 4.ls 5.cat id_rsa.pub 或者C:\User\x ...
- 2018-9-30-C#-从零开始写-SharpDx-应用-画三角
title author date CreateTime categories C# 从零开始写 SharpDx 应用 画三角 lindexi 2018-09-30 18:30:14 +0800 20 ...
- php 位运算 3<<2;
- 宝塔linux
宝塔linux linux 定时任务管理
- @51nod - 1196/1197/1198@ 字符串的数量
目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...
- Pytorch实现MNIST(附SGD、Adam、AdaBound不同优化器下的训练比较) adabound实现
学习工具最快的方法就是在使用的过程中学习,也就是在工作中(解决实际问题中)学习.文章结尾处附完整代码. 一.数据准备 在Pytorch中提供了MNIST的数据,因此我们只需要使用Pytorch提供 ...