求一棵树上是否存在路径长度为K的点对。

POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径。

刚开始我以为只要在分治过程中出现过长度为K的就算是找到了,其实不然,因为可能是相同子树里面的两个结点,这个结果显然是错误的。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
int v, l;
node() {};
node(int _v, int _l):v(_v), l(_l) {};
};
#define N 10015
int n, m, K, size, root, s[N], f[N], d[N], ans;
bool done[N], ok;
vector<int> dep;
vector<node> g[N]; void getroot(int now, int fa) {
int u;
s[now] = 1; f[now] = 0;
for (int i=0; i<g[now].size(); i++)
if ((u = g[now][i].v) != fa && !done[u]) {
getroot(u, now);
s[now] += s[u];
f[now] = max(f[now], s[u]);
}
f[now] = max(f[now], size-s[now]);
if (f[now] < f[root]) root = now;
}
void getdep(int now, int fa) {
dep.push_back(d[now]);
int u; s[now] = 1;
for (int i=0; i<g[now].size(); i++)
if ((u = g[now][i].v) != fa && !done[u]) {
d[u] = d[now] + g[now][i].l;
getdep(u, now);
s[now] += s[u];
}
}
int calc(int now, int init) {
d[now] = init; dep.clear();
getdep(now, 0);
sort(dep.begin(), dep.end());
int ret = 0;
for (int l=0, r=dep.size()-1; l<r; ) {
if (dep[l] + dep[r] == K) {
if (dep[l] == dep[r]) {
ret += (r-l+1)*(r-l)/2; break;
}
int i=l, j=r;
while (dep[i] == dep[l]) i++;
while (dep[j] == dep[r]) j--;
ret += (i-l)*(r-j);
l = i, r = j;
} else if (dep[l] + dep[r] < K) l++;
else r--;
}
return ret;
}
void work(int now) {
ans += calc(now, 0);
int u;
done[now] = true;
for (int i=0; i<g[now].size(); i++)
if (!done[u = g[now][i].v]) {
ans -= calc(u, g[now][i].l);
f[0] = size = s[u];
getroot(u, root=0);
work(root);
}
}
void solve() {
memset(done, false, sizeof(done));
f[0] = size = n;
getroot(1, root=0);
ans = 0;
work(root);
puts(ans ? "AYE" : "NAY");
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while (scanf("%d", &n) == 1 && n) {
for (int i=0; i<=n; i++) g[i].clear();
int a, b;
for (int i=1; i<=n; i++) {
while (scanf("%d", &a) && a) {
scanf("%d", &b);
g[i].push_back(node(a, b));
g[a].push_back(node(i, b));
}
}
while (scanf("%d", &K) == 1 && K) solve();
puts(".");
} return 0;
}

POJ 2114 Boatherds【Tree,点分治】的更多相关文章

  1. poj 2114 Boatherds (树分治)

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

  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 树分治

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

  5. POJ 2114 - Boatherds

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

  6. POJ 2114 Boatherds 划分树

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

  7. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

  8. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  9. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

随机推荐

  1. CPU原理

    cpu map 1.CPU的整体架构: 2.从CPU向内存 3.CPU和内存的关系图 4.CPU指令集 5.A+B 6.结果输入寄存器 7.寄存器中的临时存储,用来暂存B 8.将B传入寄存器 9.A会 ...

  2. SVN四部曲之SVN命令精通

    SVN 常用命令一览表 命令 功能 使用格式 checkout 检出 svn  co  URL up 更新到当前URL的末端 svn  up switch 更新到某一tag/branch svn  s ...

  3. 深入PHP EOF(heredoc)用法详解

    介绍下使用EOF heredoc方式,输出长段内容的方法, <?php $name = '姓名'; print <<<EOT <html> <head> ...

  4. 2016 系统设计第一期 (档案一)jQuery checkbox 取值赋值

    <div class="form-group"> <label for="IsActive" class="col-sm-2 con ...

  5. windows鼠标消息处理与键盘模拟函数

    1.鼠标坐标问题 BOOL GetWindowRect(   HWND hWnd,   LPRECT lpRect  ); RECT x;//定义一个二维数组x ::GetWindowRect(hwn ...

  6. cocos3.2中如何创建一个场景

    1.可以将一些比较通用的东西放到Common.h中,这是一个.h文件,必须手动添加,且保证在classes目录里 #ifndef __COMMON_H__ #define __COMMON_H__ # ...

  7. C++中的虚函数(类的向上转换,和向下转换)

    1.C++中的封装时为了代码的模块化,继承是为了代码的重用,而多态则是为了接口的重用. 2.C++中的多态是用虚函数来实现的. 3.子类对象向父类指针的转换(向上转换)是安全的,隐式的,而父类对象向子 ...

  8. Linux - 升级+编译kernel

    For upgrading present kernel to linux-next kernel, we need to follow below steps. 1. Check present k ...

  9. Valid format values for declare-styleable/attr tags[转]

    http://chaosinmotion.com/blog/?p=179 reference string color dimension boolean integer float fraction ...

  10. Ruby 语法快速入门

    作用域:指的是类,模块,方法 常量:无需指定类型,全大写 FANS = 100 puts "We have" + FANS.to_s + "fans" 变量 局 ...