POJ 2114 Boatherds 划分树
标题效果:鉴于一棵树,问有两点之间没有距离是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 划分树的更多相关文章
- poj 2114 Boatherds (树分治)
链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...
- poj 2104 (划分树模板)
Description You are working for Macrohard company in data structures department. After failing your ...
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- poj 2114 Boatherds 树的分治
还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...
- POJ 2114 - Boatherds
原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...
- POJ 2114 Boatherds【Tree,点分治】
求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...
- Poj 2114 Boatherds(点分治)
Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...
- [划分树] POJ 2104 K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51732 Accepted: 17722 Ca ...
- 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 ...
随机推荐
- CSDN上看到的一篇有关Spring JDBC事务管理的文章(内容比较全) (转)
JDBC事务管理 Spring提供编程式的事务管理(Programmatic transaction manage- ment)与声明式的事务管理(Declarative transaction ma ...
- Java线(一个):线程安全的和不安全
当我们看JDK API什么时候,总是找一些类描述说:,线程安全或线程安全,例如StringBuilder在,么一句,"将StringBuilder 的实例用于多个线程是不安全的.假设须要这种 ...
- SynchronousQueue、LinkedBlockingQueue、ArrayBlockingQueue性能测试(转)
听说JDK6对SynchronousQueue做了性能优化,避免对竞争资源加锁,所以想试试到底平时是选择SynchronousQueue还是其他BlockingQueue. 对于容器类在并发环境下的比 ...
- Android手机定位技术的发展
基于以下三种方式的移动位置:1. 网络位置 :2. 基站定位. 3. GPS定位 1 网络位置 前提是连接到网络:Wifi.3G.2G 到达IP址 比如:彩虹版QQ,珊瑚虫版QQ,就有一个功能显示对 ...
- ExtJS学习笔记:定义extjs类别
类的定义 Ext.define('Cookbook.Vehicle', { Manufacturer: 'Aston Martin', Model: 'Vanquish', getDetails: f ...
- Team Foundation Server 2015使用教程--团队项目创建
- SQL Server 2008 R2 性能计数器详细列表(四)
原文:SQL Server 2008 R2 性能计数器详细列表(四) SQL Server Latches 对象: 监视称为闩锁的内部 SQL Server 资源锁.通过监视闩锁来确定用户活动和资源使 ...
- quick-cocos2d-x游戏开发【5】——创建菜单
一个菜单是游戏中的一个基本要素,quick在里面menuItem有两个包.一个是图片菜单.一个文本菜单. 一个.图片菜单ui.newImageMenuItem(params) 參数: image: 正 ...
- B/S 类项目改善
B/S 类项目改善的一些建议 要分享的议题 性能提升:在访问量逐渐增大的同时,如何增大单台服务器的 PV2 上限,增加 TPS3 ? RESTful:相较于传统的 SOAP1,RESTful 风格 ...
- Js 正则表达式 写了一个正整数或小数点或分数前两个正则表达式
写了一个正整数或小数点或分数前两个正则表达式 /^[0-9]+([.]{1}[0-9]{1,2})? $/ 版权声明:本文博客原创文章.博客,未经同意,不得转载.