1468: Tree

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1025  Solved: 534
[Submit][Status][Discuss]

Description

给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K

Input

N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k

Output

一行,有多少对点之间的距离小于等于k

Sample Input

7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10

Sample Output

5

HINT

Source

LTC男人八题系列

Solution

写LTC还不如直接写ACRush,楼教主的男人八题.暑假看过什么都看不懂,现在似乎可以做了?

裸的树分治,统计一下路径的权值和,排序计算答案即可

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 80010
int n,root,siz,ans,stack[maxn],top,K;
struct Edgenode{int to,next,val;}edge[maxn<<];
int head[maxn],cnt=;
void add(int u,int v,int w)
{cnt++; edge[cnt].val=w; edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt;}
void insert(int u,int v,int w)
{add(u,v,w);add(v,u,w);}
int gcd(int a,int b)
{if (b==) return a; return gcd(b,a%b);}
int size[maxn],maxx[maxn],val[maxn]; bool visit[maxn];
void DFSroot(int now,int last)
{
size[now]=; maxx[now]=;
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].to!=last && !visit[edge[i].to])
{
DFSroot(edge[i].to,now);
size[now]+=size[edge[i].to];
maxx[now]=max(maxx[now],size[edge[i].to]);
}
maxx[now]=max(maxx[now],siz-size[now]);
if (maxx[now]<maxx[root]) root=now;
}
void DFSval(int now,int last)
{
stack[++top]=val[now];
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].to!=last && !visit[edge[i].to])
{
val[edge[i].to]=val[now]+edge[i].val;
DFSval(edge[i].to,now);
}
}
int Getans(int now,int va)
{
val[now]=va; top=; DFSval(now,);
sort(stack+,stack+top+);
int re=,l=,r=top;
while (l<r) if (stack[l]+stack[r]<=K) re+=r-l,l++; else r--;
return re;
}
void work(int now)
{
ans+=Getans(now,);
visit[now]=;
for (int i=head[now]; i; i=edge[i].next)
if (!visit[edge[i].to])
{
ans-=Getans(edge[i].to,edge[i].val);
root=; siz=size[edge[i].to];
DFSroot(edge[i].to,); work(root);
}
}
int main()
{
n=read();
for (int u,v,w,i=; i<=n-; i++)
u=read(),v=read(),w=read(),insert(u,v,w);
K=read();
maxx[]=siz=n;
DFSroot(,);
work(root);
printf("%d\n",ans);
return ;
}

吃饭前想10分钟Rush出来...手速不够TAT

【BZOJ-1468】Tree 树分治的更多相关文章

  1. bzoj 1468 Tree(点分治模板)

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1527  Solved: 818[Submit][Status][Discuss] ...

  2. BZOJ.1468.Tree(点分治)

    BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...

  3. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  4. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  5. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  6. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  7. HDU4871 Shortest-path tree(树分治)

    好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...

  8. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

  9. HDU 4812 D Tree 树分治

    题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...

随机推荐

  1. TinyFrame再续篇:整合Spring AOP实现日志拦截

    上一篇中主要讲解了如何使用Spring IOC实现依赖注入的.但是操作的时候,有个很明显的问题没有解决,就是日志记录问题.如果手动添加,上百个上千个操作,每个操作都要写一遍WriteLog方法,工作量 ...

  2. vbs http

    get请求 模拟发送http请求到百度Dim httpSet http = CreateObject("Msxml2.ServerXMLHTTP")http.open " ...

  3. opencv6.4-imgproc图像处理模块之直方图与模板

    接opencv6.3-imgproc图像处理模块之边缘检测 九.直方图的相关操作 直方图是图像中像素强度分布的图形表达方式:它统计了每一个强度值所具有的像素个数 上图是一个灰色图像,通过对图像的每个不 ...

  4. WPF ListView和ListBox等双击事件问题

    上两篇文章中说双击行获取不到当前数据对象问题, http://www.cnblogs.com/ligl/p/5636899.html http://www.cnblogs.com/ligl/p/562 ...

  5. 读懂IL代码就这么简单(二)

    一 前言 IL系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致.个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 ...

  6. ADO.NET 学习笔记 入门教程

    本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=57&page=1&extra=#pid63 这是本人在 ...

  7. TF2ZP函数

    TF2ZP 中TF是什么意思?   Transfer function   tf 就是传递函数的意思,简称传函 tf2zp是将传递函数转换为零极点形式的一个转换函数   [Z,P,K] = TF2ZP ...

  8. 前端框架——BootStrap学习

    BootStrap简单总结下:1.栅格系统,能够很好的同时适应手机端和PC端(及传说中的响应式布局) 2.兼容性好 接下来是对BootStrap学习的一些基础案例总结和回顾: 首先引入:bootstr ...

  9. php 解析json

    今天做项目的时候需要用到json数组,解析时遇到了个小小的麻烦,特此将解决办法记下: json数据如下: { "code":200, "message":&qu ...

  10. HashTable、HashMap、HashSet

    1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key的hash ...