【POJ 1741】 Tree (树的点分治)
TreeDescription
Give a tree with n vertices,each edge has a length(positive integer less than 1001).
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.Input
The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l.
The last test case is followed by two zeros.Output
For each test case output the answer on a single line.Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0Sample Output
8Source
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 10010
#define INF 0xfffffff struct node
{
int x,y,c,next;
}t[Maxn*];int len;
int first[Maxn];
int n,k; int mymax(int x,int y) {return x>y?x:y;} void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
}
int rt;
bool q[Maxn];
int sm[Maxn],mx[Maxn],dep[Maxn];
int v[Maxn],vl; void dfs(int x,int h,int f)
{
mx[x]=-;sm[x]=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f&&q[t[i].y])
{
int y=t[i].y;//dep[y]=dep[x]+t[i].c;
dfs(y,h,x);
sm[x]+=sm[y];
mx[x]=mymax(mx[x],sm[y]);
}
mx[x]=mymax(mx[x],h-sm[x]);
if(mx[x]<mx[rt]) rt=x;
} int get_ans()
{
int now=,ans=;
sort(v+,v++vl);
for(int i=vl;i>=;i--)
{
if(now>i) now=i;
while(v[i]+v[now]<=k&&now<i) now++;
ans+=now-;
}
return ans;
} void dfs2(int x,int f)
{
v[++vl]=dep[x];
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f&&q[t[i].y])
{
int y=t[i].y;
dep[y]=dep[x]+t[i].c;
dfs2(y,x);
}
} int fans; void ffind(int x,int f)
{
vl=;dep[x]=;dfs2(x,);
fans+=get_ans();
q[x]=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f&&q[t[i].y])
{
int y=t[i].y;
vl=;dfs2(y,x);
fans-=get_ans();
}int i;
for(i=first[x];i;i=t[i].next)
if(t[i].y!=f&&q[t[i].y])
{
rt=;
dfs(t[i].y,x,sm[t[i].y]);
ffind(rt,x);
}
} int main()
{
while()
{
scanf("%d%d",&n,&k);
if(n==&&k==) break;
memset(first,,sizeof(first));
len=;
for(int i=;i<n;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}
fans=;
memset(q,,sizeof(q));
mx[]=INF;
rt=;vl=;dfs(,n,);
ffind(rt,);
printf("%d\n",fans);
}
return ;
}
[POJ 1741]
其实不知道我打的点分治标不标准,感觉很丑。这题就有3个dfs,ffind是求分治的子树答案,dfs是求子树重心,dfs2是求dis以及把子树的dis加入到v中排序。
2016-10-16 22:16:44
【POJ 1741】 Tree (树的点分治)的更多相关文章
- POJ 1741 Tree(树的点分治,入门题)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21357 Accepted: 7006 Description ...
- POJ 1741 Tree 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- poj 1741(树的点分治)
Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...
- POJ 1741/1987 树的点分治
树的点分治,主要思想是每次找子树的重心,计算经过根节点的情况数,再减去点对属于同一子树的情况. #include <iostream> #include <vector> #i ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- POJ 1741 Tree 树形DP(分治)
链接:id=1741">http://poj.org/problem?id=1741 题意:给出一棵树,节点数为N(N<=10000),给出N-1条边的两点和权值,给出数值k,问 ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
随机推荐
- Java类加载及实例化的调用顺序
标题起得略拗口,大概意思就是说在一个Java类中,域和构造方法的调用顺序. 1. 没有继承的情况 单独一个类的场景下,初始化顺序为依次为 静态数据,继承的基类的构造函数,成员变量,被调用的构造函数. ...
- Lucene/Solr开发经验
1.开篇语2.概述3.渊源4.初识Solr5.Solr的安装6.Solr分词顺序7.Solr中文应用的一个实例8.Solr的检索运算符 [开篇语]按照惯例应该写一篇技术文章了,这次结合Lucene/S ...
- scrapy抓取到中文,保存到json文件为unicode,如何解决.
http://scrapy-chs.readthedocs.org/zh_CN/latest/intro/overview.html 以上链接是很好的scrapy学些资料.感谢marchtea的翻译. ...
- CSS Masking(翻译)
原文地址:http://www.html5rocks.com/en/tutorials/masking/adobe/ 关于计算机图形,两种常见的操作是:cliping(裁剪) and masking ...
- nagios 完全配置手册
Linux下Nagios的安装与配置 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机 ...
- uglifyjs 压缩js
第一步 安装nodejs dos下执行 node -v npm -v
- .NET MVC 依赖注入 来龙去脉
找了很久,找到一篇好文章,细读很有收获: 点击打开链接http://www.codeproject.com/Articles/560798/ASP-NET-MVC-Controller-Depende ...
- NPOI2.0
今天在使用NPOI2.0读取上传excel文件(excel2010)时,报了一个很奇怪的错误:无效的 URI: 未能分析主机名. 在网上查找了下找到的出错情况跟这个完全不着边际,没有办法只有自己去测试 ...
- DIV+CSS 网页布局之:一列布局
1.网页布局 布局(layout)即对事物的全面规划和安排,页面布局是对页面的文字.图像或表格进行格式化版式排列.网页布局对改善网站的外观非常重要,又称版式布局,大多数网站会把内容安排到多个列中,就像 ...
- win7的IE11降到IE8
我为什么要降低IE版本呢?...... 下载了抓包工具HttpWatch,下载地址http://pan.baidu.com/s/1kVhPMmj 版本如下: 用IE11打开抓包时流内没有内容,可能跟 ...