Tree

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

Description

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

Input

  第一行一个n,接下来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

  n<=40000

Solution

  树上路径统计问题,运用点分。
  每次处理与重心相关的路径,发现如果直接处理两点之间比较困难,我们想到了将所有点加入一个数组,用指针判断加起来<=K的个数,这样的话不一定全是简单路径,但是我们只要减去每个子树中这样操作的条数就一定只剩下简单路径了。
  点分大概的步骤:
  1.找出重心;
  2.计算经过该重心的路径相关需要求的;
  3.去掉重心对于每棵子树继续做以上过程。

Code

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std; const int ONE=; int n,K;
int x,y,z;
int next[ONE],first[ONE],go[ONE],w[ONE],tot;
int center_vis[ONE];
int Max,dist[ONE],num;
int d[ONE];
int center,Ans; int get()
{
int res,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} int Add(int u,int v,int z)
{
next[++tot]=first[u]; first[u]=tot; go[tot]=v; w[tot]=z;
next[++tot]=first[v]; first[v]=tot; go[tot]=u; w[tot]=z;
} namespace PointF
{
struct power
{
int maxx,size;
}S[ONE]; void Getsize(int u,int father)
{
S[u].size=;
S[u].maxx=;
for(int e=first[u];e;e=next[e])
{
int v=go[e];
if(v==father || center_vis[v]) continue;
Getsize(v,u);
S[u].size+=S[v].size;
S[u].maxx=max(S[u].maxx,S[v].size);
}
} void Getcenter(int u,int father,int total)
{
S[u].maxx=max(S[u].maxx,total-S[u].size);
if(Max>S[u].maxx)
{
Max=S[u].maxx;
center=u;
} for(int e=first[u];e;e=next[e])
{
int v=go[e];
if(v==father || center_vis[v]) continue;
Getcenter(v,u,total);
}
} void Getdist(int u,int father,int value)
{
dist[++num]=value;
for(int e=first[u];e;e=next[e])
{
int v=go[e];
if(v==father || center_vis[v]) continue;
Getdist(v,u,value+w[e]);
}
} int Calc(int u,int value)
{
int res=;
num=; Getdist(u,,value);
sort(dist+,dist+num+); int l=,r=num; while(l<r)
{
while(dist[l]+dist[r]>K && l<r) r--;
res+=r-l;
l++;
}
return res;
} void Dfs(int u)
{
Max=n;
Getsize(u,); center=u;
Getcenter(u,,S[u].size);
center_vis[center]=true; Ans+=Calc(center,);
for(int e=first[center];e;e=next[e])
{
int v=go[e];
if(center_vis[v]) continue;
Ans-=Calc(v,w[e]);
Dfs(v);
}
}
} int main()
{
n=get();
for(int i=;i<n;i++)
{
x=get(); y=get(); z=get();
Add(x,y,z);
} K=get();
PointF::Dfs(); printf("%d",Ans);
}

【BZOJ1468】Tree [点分治]的更多相关文章

  1. BZOJ1468:Tree(点分治)

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

  2. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  3. 【BZOJ-1468】Tree 树分治

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

  4. [bzoj1468][poj1741]Tree[点分治]

    可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...

  5. 洛谷4178 BZOJ1468 Tree题解点分治

    点分治的入门练习. 题目链接 BZOJ的链接(权限题) 关于点分治的思想我就不再重复了,这里重点说一下如何判重. 我们来看上图,假设我们去除了1节点,求出d[2]=1,d[3]=d[4]=2 假设k为 ...

  6. 【点分治】bzoj1468 Tree

    同poj1741. 换了个更快的姿势,不会重复统计然后再减掉什么的啦~ #include<cstdio> #include<algorithm> #include<cst ...

  7. bzoj1468 Tree

    最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案. #include<iostream> #include<cstdio> #include<cstring ...

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

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

  9. POJ 1741 Tree 树分治

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

随机推荐

  1. RabbitMQ ddemo 费元星

    http://blog.csdn.net/lmj623565791/article/details/37607165 转载请标明出处:http://blog.csdn.net/lmj623565791 ...

  2. LeetCode高频题目(100)汇总-Java实现

    LeetCode高频题目(100)汇总-Java实现       LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...

  3. 剑指offer-链表中倒数第K个结点14

    题目描述 输入一个链表,输出该链表中倒数第k个结点. class Solution: def FindKthToTail(self, head, k): # write code here res=[ ...

  4. 传输控制层协议TCP概述---抄书

    1.TCP的主要特点 TCP是TCP/IP体系中非常复杂的一个协议.下面介绍TCP的最主要的特点. (1)TCP是面向连接的运输层协议.也就是说,应用程序在使用TCP协议之前,必须先建立TCP连接.在 ...

  5. 无缘无故出现npm 解析异常的的问题 解决方案

    npm cache clean --force try if false delete package.lock.json try again if false npm set registry ht ...

  6. linux备忘录-账号管理与ACL权限设定

    知识 账号管理中的一些文件结构 /etc/passwd 每一行的内容都为下面结构 账号名称:密码:UID:GID:用户信息说明:家目录:shell ---- UID ---- -- 0 -> 代 ...

  7. 爬虫:Scrapy17 - Common Practices

    在脚本中运行 Scrapy 除了常用的 scrapy crawl 来启动 Scrapy,也可以使用 API 在脚本中启动 Scrapy. 需要注意的是,Scrapy 是在 Twisted 异步网络库上 ...

  8. NO1——线段树

    /* 数组存储 */ /* 预处理 */ #include <iostream> #include <cstdio> #include <algorithm> #i ...

  9. java多线程二之线程同步的三种方法

          java多线程的难点是在:处理多个线程同步与并发运行时线程间的通信问题.java在处理线程同步时,常用方法有: 1.synchronized关键字. 2.Lock显示加锁. 3.信号量Se ...

  10. java文件的I/O

    [原创] java文件的I/O操作,简单来说就是向文件中写入数据以及从文件中读出数据,这是我们平日做的最多的操作,这里给出两种文件I/O操作,当然还有许多的操作方法,各种流的使用,可谓是高深莫测:不管 ...