[P3806] Divide and Conquer on Tree
Link:
Solution:
询问树上是否存在两点间的距离为$k$,共有$m$次询问($m\le 100,k\le 1e7$)
预处理出所有距离的可能性再$O(1)$出解的复杂度为$O(n^2*log(n))$,明显TLE(但好像并不会)
而如果直接在线处理要分治$m$次,找$m$次完全相同的重心,完全没有必要
因此最好采用离线处理的方式
在每个点运用$set$对于每一个$k$查询$k-dist(i)$在之前的子树中是否出现过
预估复杂度和在线其实没什么区别,都为$O(n*m*log(n)^2)$,但少了$m-1$次递归和求重心常数就小了很多
Code:
#include <bits/stdc++.h> using namespace std;
const int MAXN=;
struct edge{int nxt,to,w;}e[MAXN<<];
set<int> s;
int n,m,x,y,z,st[MAXN],head[MAXN],q[MAXN],res[MAXN],tot,top;
int sz[MAXN],mxsub[MAXN],vis[MAXN],vsum,root; void add_edge(int from,int to,int w)
{
e[++tot].nxt=head[from];e[tot].to=to;e[tot].w=w;head[from]=tot;
e[++tot].nxt=head[to];e[tot].to=from;e[tot].w=w;head[to]=tot;
} void getroot(int x,int anc)
{
sz[x]=;mxsub[x]=;
for(int i=head[x];i;i=e[i].nxt)
{
if(e[i].to==anc||vis[e[i].to]) continue;
getroot(e[i].to,x);sz[x]+=sz[e[i].to];
mxsub[x]=max(mxsub[x],sz[e[i].to]);
}
mxsub[x]=max(mxsub[x],vsum-sz[x]);
if(mxsub[x]<mxsub[root]) root=x;
} void dfs(int x,int anc,int dist)
{
st[++top]=dist;
for(int i=head[x];i;i=e[i].nxt)
{
if(e[i].to==anc||vis[e[i].to]) continue;
dfs(e[i].to,x,dist+e[i].w);
}
} void solve(int x)
{
vis[x]=true;s.clear();s.insert();
for(int i=head[x];i;i=e[i].nxt)
{
if(vis[e[i].to]) continue;
top=;dfs(e[i].to,x,e[i].w);
for(int j=;j<=top;j++)//对m个结果更新
for(int k=;k<=m;k++)
res[k]|=s.count(q[k]-st[j]);
for(int j=;j<=top;j++)
s.insert(st[j]);
}
for(int i=head[x];i;i=e[i].nxt)
{
if(vis[e[i].to]) continue;
vsum=sz[e[i].to];getroot(e[i].to,root=);
solve(root);
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%d%d%d",&x,&y,&z),add_edge(x,y,z);
for(int i=;i<=m;i++) scanf("%d",&q[i]); vsum=mxsub[]=n;
getroot(,root=);solve(root);
for(int i=;i<=m;i++)
printf(res[i]?"AYE\n":"NAY\n");
return ;
}
Review:
如果点分治问题有多次询问,最好离线
减少递归和求重心的次数
[P3806] Divide and Conquer on Tree的更多相关文章
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- 算法与数据结构基础 - 分治法(Divide and Conquer)
分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- 算法上机题目mergesort,priority queue,Quicksort,divide and conquer
1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...
- The Divide and Conquer Approach - 归并排序
The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
- [算法]分治算法(Divide and Conquer)
转载请注明:http://www.cnblogs.com/StartoverX/p/4575744.html 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...
随机推荐
- 在前端发起ajax遇到问题
1.请注意设置datatype的类型. 如下图:
- bufferd对象详解
使用buffer类处理二进制数据 在客户端javascript脚本代码中,对于二进制数据并没有提供一个很好的支持.然后在nodejs中需要处理像TCP流或文件流时,必须要处理二进制数据.因此在node ...
- 8.0docker的客户端和守护进程
C/S 链接的方式 unix:///var/run/docker.sock 默认的 tcp://host:port fd://socketfd 在linux上进行socket 模拟 nc -U /va ...
- Spring Boot提供的特性
一.导览 本文主要按以下模块介绍spring Boot(1.3.6.RELEASE)提供的特性. SpringApplication类 外部化配置 Profiles 日志 开发WEB应用 Securi ...
- 【Windows使用笔记】Windows科研软件
1 Anaconda Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.主要是内置有jupyter notebook和jupyter ...
- 虚拟机出现intel vt -x 处于禁用状态打不开处理方式
处理方式 . 1 进入bios 以华硕主板为例 进入高级模式找到cpu虚拟技术 打开虚拟技术支持 其它电脑找到这个
- Keil MDK 5.14 仿真时System Viewer菜单显示空白和Peripherals菜单无外设寄存器
keil mdk5.14新建工程进行仿真时,进入Debug环境发现System Viewer菜单显示空白,Peripherals菜单没有外设寄存器.如图1和图2所示.打开Oprons for Targ ...
- Ubuntu终端里面显示路径名称太长,怎么设置变短【转】
转自:http://blog.csdn.net/id19870510/article/details/8276914 $: sudo vi ~/.bashrc 这个文件记录了用户终端配置 找到 if ...
- 【bzoj4459】JSOI2013丢番图
某JSOI夏令营出题人啊,naive! 你还是得学习个,搬这种原题不得被我一眼看穿? 求个n^2的约数除以二,向上取整. #include<bits/stdc++.h> using nam ...
- springmvc中输出字符串
/** * 输出文字 * @param response * @param s */ public static void responseOut(HttpServletResponse respon ...