poj 2114 Boatherds (树分治)
链接:http://poj.org/problem?id=2114
题意:
求树上距离为k的点对数量;
思路:
点分治。。
实现代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x7fffffff
const int M = 1e5+; struct node{
int to,next,w;
}e[M<<]; int n,m;
int vis[M],dis[M],d[M],siz[M],f[M],cnt,head[M],sum,root,k,ans; void init(){
cnt = ;
ans = ;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
} void add(int u,int v,int w){
e[++cnt].to = v;e[cnt].w = w;e[cnt].next = head[u];head[u] = cnt;
} void get_root(int u,int fa){
siz[u] = ; f[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa&&!vis[v]){
get_root(v,u);
siz[u] += siz[v];
f[u] = max(f[u],siz[u]);
}
}
f[u] = max(f[u],sum - siz[u]);
if(f[u] < f[root]) root = u;
return ;
} void get_dis(int u,int fa){
if(d[u] <= k) dis[++dis[]] = d[u];
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa&&!vis[v]){
d[v] = d[u] + e[i].w;
get_dis(v,u);
}
}
return ;
} int cal(int u,int c){
d[u] = c; dis[] = ;
get_dis(u,);
sort(dis+,dis+dis[]+);
int l = ,r = dis[],ans = ;
while(l < r){
if(dis[l] + dis[r] < k) l++;
else if(dis[l] + dis[r] > k) r--;
else{
if(dis[l] == dis[r]){
ans += (r-l+)*(r-l)/;
break;
}
else {
int i = l,j = r;
while(dis[i] == dis[l]) i++;
while(dis[j] == dis[r]) j--;
ans += (i-l)*(r-j);
l = i; r = j;
}
}
}
return ans;
} void solve(int u){
ans += cal(u,);
vis[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(vis[v]) continue;
ans -= cal(v,e[i].w);
sum = siz[v];
root = ;
get_root(v,);
solve(root);
}
} int main()
{
int u,v,w;
while(scanf("%d",&n)&&n){
init();
for(int i = ;i <= n;i ++){
int x,v;
while(scanf("%d",&x)&&x){
scanf("%d",&v);
add(i,x,v); add(x,i,v);
}
}
int x;
while(scanf("%d",&x)&&x){
memset(vis,,sizeof(vis));
k = x; ans = root = ;
sum = n; f[] = inf;
get_root(,);
solve(root);
if(ans) printf("AYE\n");
else printf("NAY\n");
}
printf(".\n");
}
}
poj 2114 Boatherds (树分治)的更多相关文章
- 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(点分治)
Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...
- POJ 2114 - Boatherds
原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- POJ 2114 Boatherds【Tree,点分治】
求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...
- POJ 2114 Boatherds 划分树
标题效果:鉴于一棵树,问有两点之间没有距离是k的. 数据的多组 思维:和IOI2011的Race喜欢.不是这么简单.阅读恶心,我是在主要功能的别人的在线副本. CODE: #include <c ...
随机推荐
- 大牛blog
分布式: 分布式基础学习[一] —— 分布式文件系统 分布式基础学习[二] —— 分布式计算系统(Map/Reduce) Java分布式应用技术架构介绍
- day85
频率校验 源码分析 声明:基于rest_framework的频率校验 1.首先我们进入到APIView下的dispatch,因为由此方法开始分发的 2.可以看到dispatch方法下有一个initia ...
- RDLC报表显示存储于数据库的图片
图片以二进制存储于数据库表中.在显示RDLC报表时,把图片呈现出来. 好吧. 把存储过程写好: CREATE PROCEDURE [dbo].[usp_File_Select] AS SELECT [ ...
- React 开发注意事项
引用自定义组件的时候,组件名称首字母大写 import CustomComponent from "./customComponent "; render(){ return ( ...
- centos6下ActiveMQ+Zookeeper消息中间件集群部署记录
由于最近一个项目并发请求压力比较大,所以考虑改进架构,引入消息中间件集群作为一个缓冲消息队列,具体需求:1)将大量的WebService请求报文发送到mq集群之中,并保持消息先后顺序2)保证每个消息的 ...
- 我的Android之路——底部菜单栏的实现
底部菜单栏的实现 底部菜单栏两种实现方法:ViewPager:可滑动的界面:Fragment:固定的界面. 首先,页面布局,在除去顶部toolbar之后,将主界面分为两部分,一部分为界面显示区,另一部 ...
- <四则运算>第二次冲刺
这一次冲刺的主要内容是完善我们的界面,是我们的APP界面更规划更标准一点, 然后还要添加一些新算法. 距离客户的需求已经一半了. 代码正在完善中,稍后上传...
- asp.net webform设计思路的思考
我使用asp.net的webform框架进行web应用程序的开发已经差不多四年了,在整个开发生涯中,也使用过一年asp.net的mvc框架.因为网上经常有讨论webform框架和mvc框架的优劣,所以 ...
- Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
- socket.io框架
socket.io框架 一.问题背景 目前公司在互联网产品上需要程序与前端部分要进行一个实时交互,在进行一定程度上的选型后,决定使用socket.io框架进行一个实践,算是公司的一个新的 尝试,也算是 ...