[poj1741][tree] (树/点分治)
Description
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
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
Sample Input
Sample Output
Source
Solution
1.点分治+排序
先找出重心,求解答案。对于每个重心,计算出所有过该点的最短路径长度小于或等于k的点对,记此答案为ans1
由于这些点对中会出现如下情况:
即,设任意分治出的子树重心的儿子为p,可能出现两个p的儿子共用了p到重心的路径,不符合最短路径要求
为了减去这种情况,我们可以递归算出所有关于p的重复答案,计为ans2
ans1-sum(ans2)即为最后答案
16456848
| ksq2013 | 1741 | Accepted | 760K | 172MS | C++ | 1547B | 2017-01-07 13:50:58 |
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 10010
#define inf ~0U>>1
using namespace std;
int fst[N],ecnt,ans;
struct edge{
int v,w,nxt;
}e[N<<];
inline void link(int x,int y,int w){
e[++ecnt].v=y;
e[ecnt].w=w;
e[ecnt].nxt=fst[x];
fst[x]=ecnt;
}
bool vis[N];
int n,m,root,f[N],size[N],d[N],deep[N],sum,top;
void getroot(int x,int fa){
f[x]=;
size[x]=;
for(int j=fst[x];j;j=e[j].nxt)
if(e[j].v^fa&&!vis[e[j].v])
getroot(e[j].v,x),
size[x]+=size[e[j].v],
f[x]=max(f[x],size[e[j].v]);
f[x]=max(f[x],sum-size[x]);
if(f[x]<=f[root])root=x;
}
void getdeep(int x,int fa){
deep[++top]=d[x];
for(int j=fst[x];j;j=e[j].nxt)
if(e[j].v^fa&&!vis[e[j].v])
d[e[j].v]=d[x]+e[j].w,
getdeep(e[j].v,x);
}
int cal(int x,int v){
d[x]=v;top=;
getdeep(x,);
sort(deep+,deep++top);
int t=;
for(int l=,r=top;l<r;)
if(deep[l]+deep[r]<=m)
t+=r-l,l++;
else r--;
return t;
}
void solve(int x){
vis[x]=;
ans+=cal(x,);
for(int j=fst[x];j;j=e[j].nxt)
if(!vis[e[j].v])
ans-=cal(e[j].v,e[j].w),
root=,sum=size[e[j].v],
getroot(e[j].v,root),
solve(root);
}
int main(){
while(scanf("%d%d",&n,&m)&&n){
ans=ecnt=;memset(fst,,sizeof(fst));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
link(x,y,w);link(y,x,w);
}
root=;f[]=inf;sum=n;
getroot(,);
solve(root);
printf("%d\n",ans);
}
return ;
}
[poj1741][tree] (树/点分治)的更多相关文章
- POJ1741——Tree(树的点分治)
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- POJ1741 Tree 树分治模板
http://poj.org/problem?id=1741 题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数. dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- POJ1741 tree 【点分治】
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25286 Accepted: 8421 Description ...
- POJ 1741 Tree 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- [poj1741 Tree]树上点分治
题意:给一个N个节点的带权树,求长度小于等于K的路径条数 思路:选取一个点作为根root,假设f(root)是当前树的答案,那么答案来源于两部分: (1)路径不经过root,那么就是完全在子树内,这部 ...
- E. Alternating Tree 树点分治|树形DP
题意:给你一颗树,然后这颗树有n*n条路径,a->b和b->a算是一条,然后路径的权值是 vi*(-1)^(i+1) 注意是点有权值. 从上头往下考虑是点分治,从下向上考虑就是树形DP, ...
- POJ1741 tree (点分治模板)
题目大意: 给一棵有 n 个顶点的树,每条边都有一个长度(小于 1001 的正整数).定义 dist(u,v)=节点 u 和 v 之间的最小距离.给定一个整数 k,对于每一对 (u,v) 顶点当且仅当 ...
随机推荐
- 从零开始编写自己的C#框架(14)——T4模板在逻辑层中的应用(三)
原本关于T4模板原想分5个章节详细解说的,不过因为最近比较忙,也不想将整个系列时间拉得太长,所以就将它们整合在一块了,可能会有很多细节没有讲到,希望大家自己对着代码与模板去研究. 本章代码量会比较大, ...
- js模块定义——支持CMD&AMD&直接加载
/* animate */ //直接加载 (function() { var animate = {} //balabala window.animate = animate; })(); //AMD ...
- android官方下拉刷新控件SwipeRefreshLayout的使用
可能开发安卓的人大多数都用过很多下拉刷新的开源组件,但是今天用了官方v4支持包的SwipeRefreshLayout觉得效果也蛮不错的,特拿出来分享. 简介:SwipeRefreshLayout组件只 ...
- vertical-align浅析
一直以来都搞不懂vertical-align,它适用于什么元素,它的对齐规则是什么样的.索性查了下w3c相关规范,发现行高和基线对齐的规范说明里有如下内容: This section is being ...
- ASP.NET Core 中文文档 第二章 指南 (09) 使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档
原文:ASP.NET Web API Help Pages using Swagger 作者:Shayne Boyer 翻译:谢炀(kiler) 翻译:许登洋(Seay) 对于开发人员来说,构建一个消 ...
- WinServer2008R2 + IIS 7.5 + .NET4.0 经典模式 运行WebAPI程序报404错误的解决方案
在Windows Server 2008 R2系统下,IIS 7.5 + .NET Framework 4.0的运行环境,以经典模式(Classic Mode)部署一个用.NET 4.0编译的 Web ...
- 【Java每日一题】20161223
package Dec2016; public class Ques1223 { public static void main(String[] args){ Integer obj = Integ ...
- 来玩Play框架06 用户验证
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 用户验证(User Authentification)复合的使用Play框架的数个 ...
- 【JS基础】算法
Math 对象 Math.sqrt() //返回一个数的平方根
- 解决motools和jquery之间的冲突
在同一个页面需要同时使用motools和jquery,对于$,发生了冲突,以下是解决的办法. <head> <script src="./Scripts/lib/jquer ...