链接:http://poj.org/problem?id=2114

题意:

求树上距离为k的点对数量;

思路:

点分治。。

实现代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x7fffffff
const int M = 1e5+; struct node{
int to,next,w;
}e[M<<]; int n,m;
int vis[M],dis[M],d[M],siz[M],f[M],cnt,head[M],sum,root,k,ans; void init(){
cnt = ;
ans = ;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
} void add(int u,int v,int w){
e[++cnt].to = v;e[cnt].w = w;e[cnt].next = head[u];head[u] = cnt;
} void get_root(int u,int fa){
siz[u] = ; f[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa&&!vis[v]){
get_root(v,u);
siz[u] += siz[v];
f[u] = max(f[u],siz[u]);
}
}
f[u] = max(f[u],sum - siz[u]);
if(f[u] < f[root]) root = u;
return ;
} void get_dis(int u,int fa){
if(d[u] <= k) dis[++dis[]] = d[u];
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa&&!vis[v]){
d[v] = d[u] + e[i].w;
get_dis(v,u);
}
}
return ;
} int cal(int u,int c){
d[u] = c; dis[] = ;
get_dis(u,);
sort(dis+,dis+dis[]+);
int l = ,r = dis[],ans = ;
while(l < r){
if(dis[l] + dis[r] < k) l++;
else if(dis[l] + dis[r] > k) r--;
else{
if(dis[l] == dis[r]){
ans += (r-l+)*(r-l)/;
break;
}
else {
int i = l,j = r;
while(dis[i] == dis[l]) i++;
while(dis[j] == dis[r]) j--;
ans += (i-l)*(r-j);
l = i; r = j;
}
}
}
return ans;
} void solve(int u){
ans += cal(u,);
vis[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(vis[v]) continue;
ans -= cal(v,e[i].w);
sum = siz[v];
root = ;
get_root(v,);
solve(root);
}
} int main()
{
int u,v,w;
while(scanf("%d",&n)&&n){
init();
for(int i = ;i <= n;i ++){
int x,v;
while(scanf("%d",&x)&&x){
scanf("%d",&v);
add(i,x,v); add(x,i,v);
}
}
int x;
while(scanf("%d",&x)&&x){
memset(vis,,sizeof(vis));
k = x; ans = root = ;
sum = n; f[] = inf;
get_root(,);
solve(root);
if(ans) printf("AYE\n");
else printf("NAY\n");
}
printf(".\n");
}
}

poj 2114 Boatherds (树分治)的更多相关文章

  1. POJ 2114 Boatherds 树分治

    Boatherds     Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...

  2. poj 2114 Boatherds 树的分治

    还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...

  3. Poj 2114 Boatherds(点分治)

    Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...

  4. POJ 2114 - Boatherds

    原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...

  5. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  6. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  7. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  8. POJ 2114 Boatherds【Tree,点分治】

    求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...

  9. POJ 2114 Boatherds 划分树

    标题效果:鉴于一棵树,问有两点之间没有距离是k的. 数据的多组 思维:和IOI2011的Race喜欢.不是这么简单.阅读恶心,我是在主要功能的别人的在线副本. CODE: #include <c ...

随机推荐

  1. TravelPort官方API解读

    TravelPort Ping通使用教程 Unit1 Lesson 1: 标签(空格分隔): 完成第1单元的三个课程后,您可以使用Travelport Universal API来提出服务请求并了解响 ...

  2. 洛谷 4823 [TJOI2013]拯救小矮人

    题目链接-> 噔楞 题解: 贪心 按个高+臂长排序. 个矮臂长的先走,个高臂短的后走 #include <cstdio> #include <cstring> #incl ...

  3. vue 首页背景图片加载完成前增加 loading 效果 -- 使用 new Image() 实现

    1. 创建 loading 公用组件 <template> <div class="load-container"> <div class=" ...

  4. python语法基础笔记

    本篇笔记基于博主自己的的学习,理解,总结所写.很多东西可能存在误解,不能保证百分之百的正确. 1. 数据表达1.1 常量和变量1.2 数据类型1.2.1 基本数据元素1.2.1.1 数字1.2.1.2 ...

  5. Nginx中防盗链(下载防盗链和图片防盗链)及图片访问地址操作记录

    日常运维工作中,设置防盗链的需求会经常碰到,这也是优化网站的一个必要措施.今天在此介绍Nginx中设置下载防盗链和图片防盗链的操作~ 一.Nginx中下载防盗链的操作记录对于一些站点上的下载操作,有很 ...

  6. bootstrap是什么

    Bootstrap,来自 Twitter,是目前最受欢迎的前端框架. Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷. 本教程将向您讲解 ...

  7. 树莓派3代b型静态IP设置,和ssh的wlan配置

    https://blog.csdn.net/qq_36305492/article/details/78607557

  8. Eddy's mistakes HDU

    链接 [http://acm.hdu.edu.cn/showproblem.php?pid=1161] 题意 把字符串中大写字母变为小写 . 分析 主要是含有空格的字符串如何读入,用getline(c ...

  9. C. Party Lemonade

    链接 [http://codeforces.com/group/1EzrFFyOc0/contest/913/problem/C] 分析 看代码,巧妙的贪心 代码 #include<bits/s ...

  10. 必应词典案例分析——个人博客作业week3

    案例分析 ——必应词典客户端 软件缺陷常常又被叫做Bug,即为计算机软件或程序中存在的某种破坏正常运行能力的问题.错误,或者隐藏的功能缺陷. 缺陷的存在会导致软件产品在某种程度上不能满足用户的需要.I ...