POJ 2114 Boatherds【Tree,点分治】
求一棵树上是否存在路径长度为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,点分治】的更多相关文章
- poj 2114 Boatherds (树分治)
链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...
- poj 2114 Boatherds 树的分治
还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...
- Poj 2114 Boatherds(点分治)
Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- POJ 2114 - Boatherds
原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...
- POJ 2114 Boatherds 划分树
标题效果:鉴于一棵树,问有两点之间没有距离是k的. 数据的多组 思维:和IOI2011的Race喜欢.不是这么简单.阅读恶心,我是在主要功能的别人的在线副本. CODE: #include <c ...
- poj 1741 树的点分治(入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18205 Accepted: 5951 Description ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
随机推荐
- OpenNMS界面图 .
http://demo.opennms.org/opennms/login.jsp;jsessionid=zibykal1cw4b1cir8wgn0a8b0 这个是opennms的demo websi ...
- zip压缩包密码破解
有一种破解方法叫做Known plaintext attack.市面上的密码破解软件几乎都带有这个功能.操作方法就是找到加密压缩包中的任意一个文件,用同样的压缩软件同样的压缩方式压缩成一个不加密的包, ...
- define的用法
define的用法小结 define的用法只是一种纯粹的替换功能,宏定义的替换是预处理器处理的替换. 一:简单的宏定义用法 格式:#define 标识符 替换内容 替换的内容可以是数字,字符,字符串, ...
- Daject初探之Record模型
上一篇博文我简单介绍了Daject以及Daject的Table模型,Table模型是对一张数据表的抽象,从数据表的级别处理数据,而Record模型是对单条数据记录的抽象,从记录的级别处理数据. 这一篇 ...
- sql Mirroring
http://www.codeproject.com/Articles/109236/Mirroring-a-SQL-Server-Database-is-not-as-hard-as http:// ...
- DOM生成XML文档与解析XML文档(JUNIT测试)
package cn.liuning.test; import java.io.File; import java.io.IOException; import javax.xml.parsers.D ...
- 第一个Nodejs程序
我的第一个Nodejs程序:Hello World var http = require("http"); http.createServer(function(request, ...
- AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(二)
上次已经建立了可运行的基本框架,这篇就说说怎么把我们自定义的View自动加载并添加到AvalonDock里面,AvalonDock里有3种类型的UI部件,Document, DockableConte ...
- 原型prototype -- 深入理解javascript
/* 原型Prototype */ //一.原型 //原型使用一 var calculator = function (dlg, tax) { this.dlg = dlg; this.tax = t ...
- POJ 3723 Conscription 最小生成树
题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...