树链剖分-点的分治(dis[i]+dis[j]==k的点对数量)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 1195 | Accepted: 387 |
Description
river flows to the sea. Moreover, the Trabantian villages are exactly at the rivers' springs, junctions and at the mouth of the largest river. Please note that more than 2 rivers can join at a junction. However, the rivers always form a tree (with villages
as vertices).
The pricing policy of the Boatherds is very simple: each segment of each river between two villages is assigned a price (the price is same in both directions), so if a tourist requests a journey between any two villages, the ticket office clerks just add the
prices of the segments along the only path between the villages.
One day, a very strange tourist appeared. She told the clerks that she returns to her country on the next day and she wants to spend all the remaining money on a boat trip, so they should find a route with exactly this cost. Being just poor (ahem) businessmen,
they have asked the Abacus Calculator Makers for help.
You are given a description of the river network with costs of river segments and a sequence of integers x1,..., xk. For each xi, you should determine if there is a pair of cities (a, b) in the river network such that the cost of the trip between a and b is
exactly xi.
Input
- A single line containing a single integer: the number of villages N (1 <= N <= 10 000).
- N lines describing the villages. The i-th of these lines (1 <= i <= N) describes the village with number i. It contains space separated integers d1, c1, d2, c2, , dki, cki, 0. The dj's are numbers of villages from which the rivers
flow directly to the village i (with no other villages in between), each cj is the price of the journey between villages i and dj. Moreover, 2 <= dj <= N and 0 <= cj <= 1 000. Village 1 always corresponds to the mouth of the largest river, therefore no di
can ever be equal to 1. - M <= 100 lines describing the queries. The i-th of these lines corresponds to the i-th query and contains a single integer xi (1 <= xi <= 10 000 000).
- The instance is finished by a single line containing the number 0.
The whole input is ended by a single line containing the number 0.
Output
xi, or the word "NAY" otherwise.
Output for each instance must be followed by a single line containing just the dot character.
Sample Input
6
2 5 3 7 4 1 0
0
5 2 6 3 0
0
0
0
1
8
13
14
0
0
Sample Output
AYE
AYE
NAY
AYE
.
题目大意:
求一棵树上是否存在路径长度为K的点对。
POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径。
刚开始我以为只要在分治过程中出现过长度为K的就算是找到了,其实不然,因为可能是相同子树里面的两个结点,这个结果显然是错误的。
修改内容:例如一个序列0,1,2,2,3,3,3,4,4,4,6,8,9
设k=6对于该子树,先找到0,1,2,2,3,3,3,4,4,4,6,sum+=1*1,
然后:搜到2,2,3,3,3,4,4,4,sum+=2*3(2个2与3个4)
最后搜到3,3,3,sum+=3*2/2;
return sum=9;
程序:
#include"string.h"
#include"stdio.h"
#include"stdlib.h"
#include"queue"
#include"stack"
#include"iostream"
#include"algorithm"
#include"vector"
#define inf 1000000000
#define M 51111
using namespace std;
struct node
{
int u,v,w,next;
}edge[M*3];
int t,head[M],use[M],dis[M],son[M],limit[M],k,cnt,MN,ID,ans;
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void dfs_size(int u,int f)
{
son[u]=1;
limit[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(f!=v&&!use[v])
{
dfs_size(v,u);
son[u]+=son[v];
limit[u]=max(limit[u],son[v]);
}
}
}
void dfs_root(int root,int u,int f)
{
if(son[root]-son[u]>limit[u])
limit[u]=son[root]-son[u];
if(MN>limit[u])
{
ID=u;
MN=limit[u];
}
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(f!=v&&!use[v])
dfs_root(root,v,u);
}
}
void dfs_dis(int u,int f,int id)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(f!=v&&!use[v])
{
dfs_dis(v,u,id+edge[i].w);
}
}
dis[cnt++]=id;
}
int cal(int u,int f,int id)
{
cnt=0;
int sum=0,i,j;
dfs_dis(u,f,id);
sort(dis,dis+cnt);
i=0;
j=cnt-1;
while(i<j)//需要特殊注意的地方
{
if(dis[i]+dis[j]<k)
i++;
else if(dis[i]+dis[j]>k)
j--;
else
{
if(dis[i]==dis[j])
{
sum+=(j-i+1)*(j-i)/2;
break;
}
int st=i,ed=j;
while(dis[st]==dis[i])st++;
while(dis[ed]==dis[j])ed--;
sum+=(st-i)*(j-ed);
i=st,j=ed;
}
}
return sum;
}
void dfs_ans(int root,int u,int f)
{
dfs_size(root,f);
MN=inf;
dfs_root(root,root,f);
ans+=cal(ID,ID,0);
use[ID]=1;
for(int i=head[ID];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!use[v])
{
ans-=cal(v,v,edge[i].w);
dfs_ans(v,v,v);
}
}
}
void slove()
{
ans=0;
memset(use,0,sizeof(use));
dfs_ans(1,1,1);
//printf("%d\n",ans);
if(ans>0)
printf("AYE\n");
else
printf("NAY\n");
}
int main()
{
int n,i,j,w;
while(scanf("%d",&n),n)
{
init();
for(i=1;i<=n;i++)
{
while(scanf("%d",&j),j)
{
scanf("%d",&w);
add(i,j,w);
add(j,i,w);
}
}
while(scanf("%d",&k),k)
{
slove();
}
printf(".\n");
}
return 0;
}
树链剖分-点的分治(dis[i]+dis[j]==k的点对数量)的更多相关文章
- 树链剖分-点的分治(点数为k且距离最长的点对)
hdu4871 Shortest-path tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 130712/130712 K ( ...
- 树链剖分-点的分治(链的点的个数为k的点对数)
hdu4760 Cube number on a tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 ...
- [2016北京集训试题7]thr-[树形dp+树链剖分+启发式合并]
Description Solution 神仙操作orz. 首先看数据范围,显然不可能是O(n2)的.(即绝对不是枚举那么简单的),我们考虑dp. 定义f(x,k)为以x为根的子树中与x距离为k的节点 ...
- BZOJ4012[HNOI2015]开店——树链剖分+可持久化线段树/动态点分治+vector
题目描述 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现她们面临着一个 ...
- 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分
树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...
- [luogu4886] 快递员(点分治,树链剖分,lca)
dwq推的火题啊. 这题应该不算是点分治,但是用的点分治的思想. 每次找重心,算出每一对询问的答案找到答案最大值,考虑移动答案点,使得最大值减小. 由于这些点一定不能在u的两颗不同的子树里,否则你怎么 ...
- 树分治&树链剖分相关题目讨论
预备知识 树分治,树链剖分 poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs ...
- dsu+树链剖分+树分治
dsu,对于无修改子树信息查询,并且操作支持undo的问题 暴力dfs,对于每个节点,对所有轻儿子dfs下去,然后再消除轻儿子的影响 dfs重儿子,然后dfs暴力恢复轻儿子们的影响,再把当前节点影响算 ...
- [集训队作业2018]蜀道难——TopTree+贪心+树链剖分+链分治+树形DP
题目链接: [集训队作业2018]蜀道难 题目大意:给出一棵$n$个节点的树,要求给每个点赋一个$1\sim n$之内的权值使所有点的权值是$1\sim n$的一个排列,定义一条边的权值为两端点权值差 ...
随机推荐
- javascript实现记录文本框内文字个数
最近在做一个项目中遇到这样一个问题,要对文本框中用户输入的文字进行记数,在下面显示出来,因为我们做的是一个短信发送平台,现在我们国家的短信服务,如果你的信息超过了70个字符,短信就会按二条给你下发.所 ...
- Spring4 Web开发新特性
基于Servlet3开发. 针对RESTful开发,提供了@RestController,加在Controller上面,免除了每个@RequestMapping method上面的@ResponseB ...
- Can't connect to MySQL server on '192.168.7.175' (10060)
原因: 1.你的ip没有被授权,无法访问. 2.端口没有打开(如:3306端口没有打开). 解决方法: 授权(http://www.cnblogs.com/SZxiaochun/p/6401424.h ...
- Java中的内存处理机制和final、static、final static总结
Java中的内存处理机制和final.static.final static总结 装载自:http://blog.csdn.net/wqthaha/article/details/20923579 ...
- Javascript农历与公历相互转换
/**用法 * Lunar.toSolar(2016, 6, 3); 农历转化公历 * Lunar.toLunar(2016, 7, 6); 公历转化农历 */ var Lunar = { MIN_Y ...
- 怎么快速了解自己的MySQL服务器?
From: http://www.cnblogs.com/benshan/archive/2013/01/09/2853097.html 1.查看数据库服务器状态:status Linux 下的MyS ...
- Oracle之唯一性约束(UNIQUE Constraint)使用方法具体解释
Oracle | PL/SQL唯一索引(Unique Constraint)使用方法 1 目标 用演示样例演示怎样创建.删除.禁用和使用唯一性约束. 2 什么是唯一性约束? 唯一性约束指表中一个字段或 ...
- Powershell 的自己主动部署
工作中反复性的版本号移植,一天上线10几次,让我痛不欲生,频繁的操作也可能出现疲劳性失误,导致严重的生产故障.于是乎,闲暇时间.我開始研究使用powershell自己主动部署程序到Linuxse ...
- TODO的用法
visual studio提供//TODO标记,不过不会在右边标记处明显标识,需要你选择菜单栏的视图进行查看.方法如下: 1.首先在你还未完成的地方打上TODO标记,以下方式均可: 1)//TODO: ...
- 什么是mysql的元数据呢?
需求描述: 什么是mysql的元数据?metadata? 概念解释: 简单来说:任何描述数据库的数据就是数据库的元数据库(与数据库中存在的内容相对) 比如,用于描述一个冰箱是如何构成,怎么使用的就是冰 ...