/*

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. Error while launching application Error: spawn ENOMEM 解决

    当NodeJs PM2 无法启动应用 出现 Error while launching application Error: spawn ENOMEM 错误时     执行一下  pm2 update ...

  2. 常用算法之排序(Java)

    一.常用算法(Java实现) 1.选择排序(初级算法) 原理:有N个数据则外循环就遍历N次并进行N次交换.内循环实现将外循环当前的索引i元素与索引大于i的所有元素进行比较找到最小元素索引,然后外循环进 ...

  3. 翻译-在10行代码之内创建容器化的.net core应用

    本文翻译自Hans Kilian的文章 Creating a containerized .NET core application in less than 10 lines of code htt ...

  4. js 遍历树的层级关系的实现

    1.遍历树的层级关系 1)先整理数据 2)找到id和数据的映射关系 3)然后找到父节点的数据,进行存储 test() { const list = [ { id: ", parentId: ...

  5. 怎样修改element-ui中的样式?

    方法一 方法二 使用  /deep/ .homePage /deep/ .el-main { padding: 0;  } .homePage为我们要修改组件类名的父级组件样式类名..即使定义一个空的 ...

  6. R_基础_01

    R语言介绍:R是一种区分大小写的解释型语言.R中有多种数据类型,包括向量.矩阵.数据框(与数据集类似)以及列表(各种对象的集合),广泛用于数据统计. R的特点:一次交互式会话期间的所有数据对象都被保存 ...

  7. jQuery效果之滑动

    jQuery 滑动方法有三种:slideDown().slideUp().slideToggle(). jQuery slideDown() 方法用于向下滑动元素, 语法:$(selector).sl ...

  8. Python 使用gevent实现多任务

    import gevent import time # 如果需要默认的 time.sleep(0.5) 需要打补丁 from gevent import monkey monkey.patch_all ...

  9. FreeRTOS 任务创建和删除(动态)

    TaskHandle_t taskhandle; TaskHandle_t taskhandle1; void vTask(void *t) { int i = 0; while(1) { i++; ...

  10. shell 三剑客之 sed 命令详解

    sed 编辑命令 sed 编辑命令对照表 把 /etc/passwd 文件赋值到当前路径下,进行操作 cp /etc/passwd ./ cat -n passwd sed 删除操作 删除 passw ...