标题效果:鉴于一棵树,问有两点之间没有距离是k的。

数据的多组

思维:和IOI2011的Race喜欢。不是这么简单。阅读恶心,我是在主要功能的别人的在线副本。

CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 10010
#define INF 0x3f3f3f3f
using namespace std; int points,k;
int head[MAX],total;
int next[MAX << 1],aim[MAX << 1],length[MAX << 1]; int _total,_size,size[MAX];
int ans,root;
bool v[MAX];
int dis[MAX],p; inline void Initialize();
inline void Add(int x,int y,int len); void Work(int x);
void GetRoot(int x,int last);
inline int Count(int x,int last,int len);
void GetDis(int x,int last,int len); int main()
{
while(scanf("%d",&points) != EOF && points) {
Initialize();
for(int y,z,i = 1;i <= points; ++i) {
while(scanf("%d",&y) != EOF && y) {
scanf("%d",&z);
Add(i,y,z),Add(y,i,z);
}
}
while(scanf("%d",&k) != EOF && k) {
ans = 0;
size[1] = points;
memset(v,false,sizeof(v));
Work(1);
if(ans) puts("AYE");
else puts("NAY");
}
puts(".");
}
return 0;
} inline void Initialize()
{
total = 0;
memset(head,0,sizeof(head));
} inline void Add(int x,int y,int len)
{
next[++total] = head[x];
aim[total] = y;
length[total] = len;
head[x] = total;
} void Work(int x)
{
_size = INF,_total = size[x];
GetRoot(x,0);
x = root;
v[x] = true;
ans += Count(x,0,0);
for(int i = head[x];i;i = next[i]) {
if(v[aim[i]]) continue;
ans -= Count(aim[i],x,length[i]);
Work(aim[i]);
}
} void GetRoot(int x,int last)
{
size[x] = 1;
int max_size = 0;
for(int i = head[x];i;i = next[i]) {
if(aim[i] == last || v[aim[i]]) continue;
GetRoot(aim[i],x);
size[x] += size[aim[i]];
max_size = max(max_size,size[aim[i]]);
}
max_size = max(max_size,_total - size[x]);
if(max_size < _size)
_size = max_size,root = x;
} inline int Count(int x,int last,int len)
{
p = 0;
GetDis(x,last,len);
sort(dis + 1,dis + p + 1);
int l = 1,r = p,re = 0;
while(l < r) {
if(dis[l] + dis[r] > k) --r;
else if(dis[l] + dis[r] < k) ++l;
else {
if(dis[r] == dis[l]) {
re += (r - l) * (r - l + 1) >> 1;
break;
}
int _l = l,_r = r;
while(dis[l] == dis[_l]) _l++;
while(dis[r] == dis[_r]) _r--;
re += (_l - l) * (r - _r);
l = _l,r = _r;
}
}
return re;
} void GetDis(int x,int last,int len)
{
dis[++p] = len;
for(int i = head[x];i;i = next[i]) {
if(aim[i] == last || v[aim[i]]) continue;
GetDis(aim[i],x,len + length[i]);
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 2114 Boatherds 划分树的更多相关文章

  1. poj 2114 Boatherds (树分治)

    链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...

  2. poj 2104 (划分树模板)

    Description You are working for Macrohard company in data structures department. After failing your ...

  3. POJ 2114 Boatherds 树分治

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

  4. poj 2114 Boatherds 树的分治

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

  5. POJ 2114 - Boatherds

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

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

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

  7. Poj 2114 Boatherds(点分治)

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

  8. [划分树] POJ 2104 K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51732   Accepted: 17722 Ca ...

  9. POJ 2761 Feed the dogs(平衡树or划分树or主席树)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

随机推荐

  1. Machine Learning—Linear Regression

    Evernote的同步分享:Machine Learning-Linear Regression 版权声明:本文博客原创文章.博客,未经同意,不得转载.

  2. DEMO阶段已完成,今天,要深入钻

    今天老师整理我的代码,发现,当时我没搞清楚这是正常的,由于我没有在一开始发挥到其翻译,而没有分析. 只要,研究底部是正确的.为了更好地理解代码. 上午:OSGEARTH视频教程. 上午,DX11机械仿 ...

  3. STL 清除模板容器 clear.h

    #pragma once #include "GeometricMacro.h" #include "GeometricEnum.h" #include &qu ...

  4. Python补充04 Python简史

    原文:Python简史 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python是我喜欢的语言,简洁,优美,容易使用.前两天, ...

  5. C# DateTime结构的常用方法

    在项目开发中,经常会碰到日期处理.比如查询中,可能会经常遇到按时间段查询,有时会默认取出一个月的数据.当我们提交数据时,会需要记录当前日期,等等.下面就看看一些常用的方法. 首先,DateTime是一 ...

  6. Javascript入门视频教程

    1,第一节 http://pan.baidu.com/play/video#video/path=%2F%E6%95%99%E5%AD%A61.mov&t=-1 2,第二节 http://pa ...

  7. top使用命令

    top命令 第一行和uptime命令一样,都有系统的负载. 10:36:19 当前时间 up 1day .7min 系统执行时间.格式为天 时:分 1 user 当前登录用户数 load averag ...

  8. Android特效 五种Toast具体解释

    Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,并且Toast显示的时间有限,过一定的时间就会自己主动消失. 1.默认效果: 代码: Toas ...

  9. PHP 模板方法模式使用

    模板方法模式 用于各个子类均需实现类似的步骤,但是在这些步骤过程中,有各个子类不同的实现方法,也有他们公共的实现方法. 示例代码: //==================== //模板方法模式 // ...

  10. zoj 3659 并检查集合

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...