点分 TREE
/*
1468: Tree
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 774 Solved: 412
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
Sample Output
#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的更多相关文章
- zju1610Count the Colors
ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting s ...
- 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 ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- 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 ...
- 【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 ...
- 【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 ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- 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 ...
- bzoj 1468 Tree 点分
Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1972 Solved: 1101[Submit][Status][Discuss] Desc ...
随机推荐
- ACM集训
2019-07-18 09:06:10 emmm.... 昨天5个小时做了一道题,心情复杂,不着急慢慢来 Ivan recently bought a detective book. The book ...
- Scala 类型参数
介绍 类型参数是什么?类型参数其实就类似于Java中的泛型.先说说Java中的泛型是什么,比如我们有List a = new ArrayList(),接着a.add(1),没问题,a.add(&quo ...
- Python完成迪杰斯特拉算法并生成最短路径
def Dijkstra(network,s,d):#迪杰斯特拉算法算s-d的最短路径,并返回该路径和代价 print("Start Dijstra Path……") path=[ ...
- react组件懒加载
组件懒加载方式-:react新增的lazy const Alert = lazy(() => import('./components/alert')); export default func ...
- 原生 js 录屏功能
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- eval函数和isNaN函数
(一)eval函数定义:eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. (二)语法:eval(string)string必需. (三)返回值:通过计算 string ...
- HTTP协议超级详解
HTTP协议简介 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数据通信的基础. ...
- RZ70注册SLD
本文的将S4 abap系统向PO JAVA系统中注册. S4 QASERPAP01 NR=60 ASCS=61 PO QASPISAP01 NR=60 SCS=61 http://qaspisap01 ...
- HTML5 - 初识
众所周知,我们现在的手机APP开发模式分为一下几大类: 一.原生APP 二.纯HTML5 三.原生+HTML5 四.React Native 公司的职位划分: 1.平面设计师 : 作图.切图.HTM ...
- ABAP Code Inspector那些隐藏的功能,您都知道吗?
最近有粉丝在后台给我留言,说新知识太多,"学不动了".所谓温故而知新,今天我们就来重温下ABAP里的Code Inspector的用法. 2015年6月,我在SAP社区上写了一篇博 ...