2756 树上的路径

 时间限制: 3 s     空间限制: 128000 KB     题目等级 : 大师 Master
 
 
题目描述 Description

给出一棵树,求出最小的k,使得,且在树中存在路径P,使得k>= S 且 k <=E. (k为路径P上的边的权值和)

输入描述 Input Description

第一行给出N,S,E,N代表树的点数,S,E如题目描述一致

下面N-1行给出这棵树的相邻两个节点的边及其权值W

输出描述 Output Description

输出一个整数k,表示存在路径P,并且路径上的权值和 K>=S , k<=E,若无解输出-1

样例输入 Sample Input

5 10 40

2 4 80

2 3 57

1 2 16

2 5 49

样例输出 Sample Output

16

数据范围及提示 Data Size & Hint

边权W<=10000,

保证答案在int(longint)范围内,且|E-S|<=50,

树上点的个数N<=30000

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 30009
using namespace std;
int n,A,B,K,la,head[N],next[N<<],ans=*1e9,size[N];
int dep[N],maxson[N],root,tot,lls,num,ls[N];
bool v[N];
struct node
{
int fr,to,len;
}a[N<<];
void addedge(int x,int y,int z)
{
a[++la].fr=x,a[la].to=y,a[la].len=z;
next[la]=head[x],head[x]=la;
}
void get_root(int x,int from)
{
size[x]=;
maxson[x]=;
for(int i=head[x];i;i=next[i])
if (!v[ a[i].to ]&&a[i].to!=from)
{
get_root(a[i].to,x);
size[x]+=size[ a[i].to ];
maxson[x]=max(maxson[x],size[ a[i].to ]);
}
maxson[x]=max( maxson[x],tot-size[x] );
if (!root||maxson[x]<maxson[root]) root=x; }
void get_dep(int x,int from)
{
for (int i=head[x];i;i=next[i])
if (!v[ a[i].to ]&&a[i].to!=from)
{
ls[++lls]=dep[ a[i].to ]=dep[x]+a[i].len;
get_dep(a[i].to,x); }
}
int get_num(int x,int jian)
{
int i,j,k,rt=;
ls[ lls= ]=dep[x]=jian;
get_dep(x,); sort(ls+,ls+lls+);
for (i=,j=lls;i<=lls;i++)
{
while (j>&&ls[i]+ls[j]>K) j--;
if (j>i)rt+=j-i;
}
return rt;
}
void divide(int x)
{
num+=get_num(x,);
v[x]=;
for (int i=head[x];i;i=next[i])
if (!v[ a[i].to ])
{
num-=get_num(a[i].to,a[i].len);
tot=size[ a[i].to ];
root=,get_root(a[i].to,);
divide( root );
}
}
int main()
{
int i,j,k,x,y,z,last;
scanf("%d%d%d",&n,&A,&B);
for(i=;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
addedge(x,y,z),addedge(y,x,z);
}
last=1e9;
for(K=A-;K<=B;K++)
{
num=;
memset(v,,sizeof(v));
tot=n,root=;
get_root(,);
divide(root);
if(num>last)
{
printf("%d\n",K);
return ;
}
last=num;
}
printf("-1\n");
}

备注:引用自Codevs 题解

Codevs 2756 树上的路径的更多相关文章

  1. codevs 2756树上的路径

    题意: 2756 树上的路径  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master    题目描述 Description 给出一棵树,求出最小的k,使得,且在树 ...

  2. 【BZOJ-3784】树上的路径 点分治 + ST + 堆

    3784: 树上的路径 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 462  Solved: 153[Submit][Status][Discuss ...

  3. bzoj 3784: 树上的路径 堆维护第k大

    3784: 树上的路径 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 27[Submit][Status][Discuss] ...

  4. 【BZOJ3784】树上的路径 点分治序+ST表

    [BZOJ3784]树上的路径 Description 给定一个N个结点的树,结点用正整数1..N编号.每条边有一个正整数权值.用d(a,b)表示从结点a到结点b路边上经过边的权值.其中要求a< ...

  5. 树上的路径 BZOJ 3784

    树上的路径 [问题描述] 给定一个N个结点的树,结点用正整数1..N编号.每条边有一个正整数权值.用d(a,b)表示从结点a到结点b路边上经过边的权值.其中要求a<b.将这n*(n-1)/2个距 ...

  6. BZOJ 3784: 树上的路径

    Description 问一棵树上前 \(k\) 大路径的边权. Sol 边分治. 非常感谢数据没有菊花图. 为了写写边分治试试然后就开了这道题. 边分治非常好想,选一条重边,分成两部分,然后分别求最 ...

  7. SPOJ COT2 树上找路径上不同值的个数

    题目大意 给出多个询问u , v , 求出u-v路径上点权值不同的个数 开始做的是COT1,用主席树写过了,理解起来不难 很高兴的跑去做第二道,完全跟普通数组区间求k个不同有很大区别,完全没思路 膜拜 ...

  8. CTSC模拟题 树上的路径

    Description 给定一棵\(N\)个结点的树,结点用正整数\(1 \dots N\)编号,每条边有一个正整数权值.用\(d(a, b)\)表示从结点\(a\)到结点\(b\)路径上经过边的权值 ...

  9. BZOJ.3784.树上的路径(点分治 贪心 堆)

    BZOJ \(Description\) 给定一棵\(n\)个点的带权树,求树上\(\frac{n\times(n-1)}{2}\)条路径中,长度最大的\(m\)条路径的长度. \(n\leq5000 ...

随机推荐

  1. HttpServletResponse 的状态码

    public static final int  SC_ACCEPTED  202 public static final int  SC_BAD_GATEWAY  502 public static ...

  2. 【转载】Hierarchal Temporal Memory (HTM)

    最近在看机器学习,看能否根据已有的历史来预测Hardware的故障发生概率.下文是一篇很有意思的文章,转自 http://numenta.org/htm.html. NuPIC是一个开源项目,用来实现 ...

  3. H3C S5024P交换机互连实验

    第一次周二网络管理实验报告 交换机互联实验 实验接线图: 交换机全貌: 可以通过超级终端和telnet来配置交换机       控制电缆连交换机console口与计算机主机(只可以传送命令不可以通信, ...

  4. python小随笔

    关于pip安装 .\pip.exe install --ignore-installed --upgrade tensorflow-gpu python可视化库 Seaborn:是一个基于matplo ...

  5. 初探node.js

    一.定义及优势 定义:Node.js是一个基于 Chrome V8 引擎 的 JavaScript 运行时,它以事件驱动为基础实现了非阻塞模型. 优势:由于Web场景下的大多数任务(静态资源读取.数据 ...

  6. 重载操作符 'operator'

    operator 是 C++ 的(运算符的)重载操作符.用作扩展运算符的功能. 它和运算符一起使用,表示一个运算符函数,理解时应将  [operator+运算符] 整体上视为一个函数名. 要注意的是: ...

  7. poj3710 Christmas Game

    题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...

  8. l5-repository基本使用--结合使用artisan

    一.从头开始创建 1.执行以下artisan: php artisan make:entity Student 如果某个文件已经存在,则不会创建新的文件去覆盖原有的文件,案例如下: 2.修改model ...

  9. 第二讲:vcs debugging basics

    要求: 1.describe three methods of debugging verilog code using vcs 2.invoke ucli debugger(不重要) 3.debug ...

  10. Python爬虫-Scrapy-CrawlSpider与ItemLoader

    一.CrawlSpider 根据官方文档可以了解到, 虽然对于特定的网页来说不一定是最好的选择, 但是 CrwalSpider 是爬取规整的网页时最常用的 spider, 而且有很好的可塑性. 除了继 ...