Tree 点分治
题目描述
给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K
输入输出格式
输入格式:
N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k
输出格式:
一行,有多少对点之间的距离小于等于k
输入输出样例
7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
5
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline int read() {
int res=;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) res=(res<<)+(res<<)+(ch^),ch=getchar();
return res;
}
#define reg register
#define N 200005
int n, tot, k;
long long ans; int head[N], cnt = ;
struct edge {
int nxt, to, val;
}ed[N*];
inline void add(int x, int y, int z)
{
ed[++cnt] = (edge){head[x], y, z};
head[x] = cnt;
} bool cut[N*]; int siz[N], root, mrt = 1e9;
void dfs(int x, int fa)
{
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;
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, );
mrt = 1e9;
tot = siz[x];
root = n;
efs(x, );
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 = ;
top = ;
Work(x, , d);
sort (a + , a + + top);
int l = , 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, );
for (reg int i = head[root] ; i ; i = ed[i].nxt)
{
int to = ed[i].to;
if (cut[i]) continue;
cut[i] = cut[i ^ ] = ;
ans -= Calc(to, ed[i].val);
solve(to);
}
} int main()
{
n = read();
for (reg int i = ; i < n ; i ++)
{
int x = read(), y = read(), z = read();
add(x, y, z), add(y, x, z);
} k = read();
solve();
printf("%d\n", ans);
return ;
}
Tree 点分治的更多相关文章
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- 【CF434E】Furukawa Nagisa's Tree 点分治
[CF434E]Furukawa Nagisa's Tree 题意:一棵n个点的树,点有点权.定义$G(a,b)$表示:我们将树上从a走到b经过的点都拿出来,设这些点的点权分别为$z_0,z_1... ...
- POJ 1741 Tree(点分治点对<=k)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- CF1039D You Are Given a Tree 根号分治,贪心
CF1039D You Are Given a Tree LG传送门 根号分治好题. 这题可以整体二分,但我太菜了,不会. 根号分治怎么考虑呢?先想想\(n^2\)暴力吧.对于每一个要求的\(k\), ...
- BZOJ1468:Tree(点分治)
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
随机推荐
- NOIP2008复赛 提高组 第一题
描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词中出现次数最多的 ...
- mybatis动态拼接条件的技巧 where 1=1 或者where标签
/** * 根据输入的学生信息进行条件检索 * 1. 当只输入用户名时, 使用用户名进行模糊检索: * 2. 当只输入邮箱时, 使用性别进行完全匹配 * 3. 当用户名 ...
- java获取电脑mac物理地址
import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import ...
- 从零开始入门 K8s| 详解 Pod 及容器设计模式
作者|张磊 阿里云容器平台高级技术专家,CNCF 官方大使 一.为什么需要 Pod 容器的基本概念 我们知道 Pod 是 Kubernetes 项目里面一个非常重要的概念,也是非常重要的一个原子调度单 ...
- Android 使用URLConnection下载音频文件
本文链接: Android 使用URLConnection下载音频文件 使用MediaPlayer播放在线音频,请参考Android MediaPlayer 播放音频 有时候我们会需要下载音频文件.这 ...
- ASP.NET Core 3.0 : 二十五. TagHelper
什么是TagHelper?这是ASP.NET Core 中新出现的一个名词,它的作用是使服务器端代码可以在Razor 文件中参与创建和呈现HTML 元素.(ASP.NET Core 系列目录) 一.概 ...
- WordPress对接微信小程序遇到的问题
1.文章内容中的“<”和“>”字符显示问题 小程序是使用“wxPares工具来实现html转wxml的,如果你的文本包含了代码比如xml会携带<>符号,程序会将其转化,造成解析 ...
- jquery的api以及用法总结-数据/操作/事件
数据 .data() 在匹配元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值 .data(obj) 一个用于更新数据的键/值对 .data()方法允许我们再dom元素上 ...
- springboot启动后自动退出
有时新建的springboot启动后自动退出运行,如图所示: 此种情况大都数是因为pom文件加入了tomcat的依赖,与springboot内嵌的tomcat冲突导致,所以只需将pom文件中的tomc ...
- linux 装composer的出现的问题
curl -sS https://getcomposer.org/installer | php php 值得是php的liux下的安装目录 php环境变量 当装compser 的时候,出现 ...