/*

1468: Tree

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 774  Solved: 412
[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*/

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int head[40008],next[80008],v[80006],u[80006],n,m,son[40008],zi[40008],f[40008];
int cnt,sum,root,d[40008],sum1,d1[40008],k;
void bian(int a1,int a2,int a3)
{
cnt++;
next[cnt]=head[a1];
head[a1]=cnt;
u[cnt]=a2;
v[cnt]=a3;
return;
}
void dfs1(int a1,int a2)
{
zi[a1]=0;
son[a1]=1;
for(int i=head[a1];i;i=next[i])
if(u[i]!=a2&&!f[u[i]])
{
dfs1(u[i],a1);
son[a1]+=son[u[i]];
zi[a1]=max(zi[a1],son[u[i]]);
}
zi[a1]=max(zi[a1],sum-son[a1]);
if(zi[a1]<zi[root])
root=a1;
}
void dfs2(int a1,int a2)
{
d[++d[0]]=d1[a1];
for(int i=head[a1];i;i=next[i])
{
if(u[i]==a2||f[u[i]])continue;
d1[u[i]]=d1[a1]+v[i];
dfs2(u[i],a1);
}
}
int gal(int a1,int a2)
{
d1[a1]=a2;
d[0]=0;
dfs2(a1,0);
sort(d+1,d+d[0]+1);
int t=0,l,r;
for(l=1,r=d[0];l<r;)
{
if(d[l]+d[r]<=k){t+=r-l;l++;}
else r--;
}
return t;
}
void work(int a1)
{
sum1+=gal(a1,0);
f[a1]=1;
for(int i=head[a1];i;i=next[i])
{
if(f[u[i]])
continue;
sum1-=gal(u[i],v[i]);
root=0;
sum=son[u[i]];
dfs1(u[i],0);
work(root);
}
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
int a1,a2,a3;
scanf("%d%d%d",&a1,&a2,&a3);
bian(a1,a2,a3);
bian(a2,a1,a3);
}
scanf("%d",&k);
zi[0]=sum=n;
dfs1(1,0);
work(root);
printf("%d\n",sum1);
return 0;
}

点分 TREE的更多相关文章

  1. zju1610Count the Colors

    ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting s ...

  2. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  3. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  4. 1127 ZigZagging on a Tree (30 分)

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  5. 【PAT 甲级】1151 LCA in a Binary Tree (30 分)

    题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...

  6. 【PAT】1043 Is It a Binary Search Tree(25 分)

    1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  7. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  8. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

  9. bzoj 1468 Tree 点分

    Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1972  Solved: 1101[Submit][Status][Discuss] Desc ...

随机推荐

  1. 【并发】6、借助FQueue 实现多线程生产消费队列

    1.这里先要说一下为什么会想到fqueue,因为这个是一个轻量级的消息队列框架,并且速度很快,用起来很方便,就是这样 当然后期考虑使用redis,这里先上一个fqueue的版本,后面有时间我再吧他改成 ...

  2. THUPC2019/CTS2019/APIO2019/PKUSC2019游记

    THUPC2019/CTS2019/APIO2019/PKUSC2019游记 5.10 中铺,火车好晃啊 5.11 打了THUPC2019的练习赛,华容道好评(四个小兵,杠鸭!) 5.12 打了THU ...

  3. hdu 2555

    Problem Description 杭州师范大学第29届田径运动会圆满的闭幕了,本届运动会是我校规模最大,参赛人数最多的一次运动会.在两天半时间里,由学生.教工组成的61支代表队共2664名运动员 ...

  4. java influx DB工具类

    配置 application-properties: spring.influxdb.url=${influxdb_host:127.0.0.1} spring.influxdb.port=${inf ...

  5. threejs CameraHelper 查看照相机的观察范围

    简单例子 这个例子,是在一个视图中,看到照相机的辅助线,也就是,一个照相机的观察访问 这样,就需要两个照相机,一个是主照相机,一个是加有辅助线的照相机(有两种,正交和透视,这里辅助的使用的是正交的) ...

  6. 利用python爬取王者荣耀英雄皮肤图片

    前两天看到同学用python爬下来LOL的皮肤图片,感觉挺有趣的,我也想试试,于是决定来爬一爬王者荣耀的英雄和皮肤图片. 首先,我们找到王者的官网http://pvp.qq.com/web201605 ...

  7. .net core的startup中配置读取

    public class Startup { public Startup(IHostingEnvironment env) { Configuration = new ConfigurationBu ...

  8. VBA嵌套if语句

    一个If或ElseIf语句可以嵌套在另一个If或ElseIf语句中.内部的If语句是根据最外层的If语句执行的.这使得VBScript能够轻松处理复杂的条件. 语法 以下是VBScript中嵌套的If ...

  9. echarts字体适配

    var html = document.getElementsByTagName("html")[0]; var width = html.clientWidth; var too ...

  10. vue的mescroll搜索运用以及各种填坑处理

    父组件处理: <template> <div class="wrap"> <!-- 搜索框 --> <div class="se ...