求一棵树上是否存在路径长度为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. Android中关于日期时间与时区的使用总结

    在开发Android的过程中,出现过几次由于日期时间导致的问题,而且主要是由于时区的原因导致,所以一直想总结一下,形成一个良好的开发规范.   一.Unix时间戳   Unix时间戳(Unix tim ...

  2. css 动画效果

    要搞就搞明白,一知半解时停止研究 损失最大     css3意义: CSS3 动画 通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片.Flash 动画以及 JavaScript. 重点 ...

  3. 模仿易信的UI

    易信,它的UI还是很简洁,因此本人想模仿一下它,用了一天的时候来研究它的资源文件,终于被我写出来.先看下效果图吧.     (一)首页的标题     main_title.xml <?xml v ...

  4. Django 学习笔记之三 数据库输入数据

    假设建立了django_blog项目,建立blog的app ,在models.py里面增加了Blog类,同步数据库,并且建立了对应的表.具体的参照Django 学习笔记之二的相关命令. 那么这篇主要介 ...

  5. C# type - IsPrimitive

    Type t = typeof(string); if (t.IsPrimitive)//not { Console.WriteLine("string is a Primitive&quo ...

  6. 2014 Multi-University Training Contest 3

    官方解题报告http://blog.sina.com.cn/s/blog_a19ad7a10102uyiq.html Wow! Such Sequence! http://acm.hdu.edu.cn ...

  7. MemSQL Start[c]UP 2.0 - Round 1

    A. Eevee http://codeforces.com/contest/452/problem/A 字符串水题 #include<cstdio> #include<cstrin ...

  8. CSS function--功能样式

    功能样式,从常用样式方法中抽离,按需使用,使用前请先阅读 CSS规范 中相关条列. /* function */ .f-cb:after,.f-cbli li:after{display:block; ...

  9. PHP 性能分析第二篇: Xhgui In-Depth

    [前言]这是国外知名博主 Davey Shafik 撰写的 PHP 应用性能分析系列的第二篇,第一篇介绍 Xhprof/Xhgui,第三篇则关注于性能调优实践. 在第一篇中,我们初步介绍了 xhpro ...

  10. 在 tornado 中异步无阻塞的执行耗时任务

    在 tornado 中异步无阻塞的执行耗时任务 在 linux 上 tornado 是基于 epoll 的事件驱动框架,在网络事件上是无阻塞的.但是因为 tornado 自身是单线程的,所以如果我们在 ...