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行边描述管道,按照题目中写的输入 接下来是 ...
随机推荐
- Redis常用命令(key、string、List)
1.Key 1.keys * 查询所有数据 2.exists key名 判断key名是否存在 3.move key名 数据库号(0-15) 移动数据key名到相应的数据库 4.expire ...
- SqlServer 2014 还原数据库时提示:操作系统返回了错误5,,拒绝访问
场景 在进行数据库还原时提示: System.Data.SqlError:在对”“尝试”“时,操作系统返回了错误5(拒绝访问) 实现 第一种方案是修改要还原的数据库备份文件的权限. 找到备份文件右击属 ...
- maven下载jar包源码配置
两个依赖,就想下mail的源码包,因该怎么 <dependencies> <dependency> <groupId>javax.mail</groupId& ...
- 关于纯xmlhttprequest请求服务器数据
今天我们的web技术已经相当的完善, 各种前端框架如jquery或者再深一点的工具APIcloud 的使用极大的方便了我们的开发工作. 今天我要分享一个纯javascript的方式来解决请求服务器数据 ...
- Python Flask高级编程之RESTFul API前后端分离精讲 (网盘免费分享)
Python Flask高级编程之RESTFul API前后端分离精讲 (免费分享) 点击链接或搜索QQ号直接加群获取其它资料: 链接:https://pan.baidu.com/s/12eKrJK ...
- Hibernate 之 @Query查询
注解 @Query 允许在方法上使用 JPQL. 列如: @Query("select u from User u where u.name=?1 and u.department_id= ...
- 反射的应用、获取Class类实例
*准备工作: 包结构:* ======================= Person.java代码: package com.atguigu.java; public class Person { ...
- [Code] 变态之人键合一
目的也比较单纯,选一门语言,走向人键合一. 选了两本书作为操练场:<精通Python设计模式>.<Data Structure and Algorithm in Python> ...
- [AI] 深度数据 - Data
Data Engineering Data Pipeline Outline [DE] How to learn Big Data[了解大数据] [DE] Pipeline for Data Eng ...
- Python日志产生器
Python日志产生器 写在前面 有的时候,可能就是我们做实时数据收集的时候,会有一个头疼的问题就是,你会发现,你可能一下子,没有日志的数据源.所以,我们可以简单使用python脚本来实现产生实时的数 ...