[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 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...
随机推荐
- HDU 1026 Ignatius and the Princess I (广搜)
题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...
- pythonTensorFlow实现yolov3训练自己的目标检测探测自定义数据集
1.数据集准备,使用label标注好自己的数据集. https://github.com/tzutalin/labelImg 打开连接直接下载数据标注工具, 2.具体的大师代码见下链接 https:/ ...
- MySQL多线程复制故障(slave_pending_jobs_size_max)
MySQL多线程复制故障(slave_pending_jobs_size_max) http://www.xuchanggang.cn/archives/1079.html
- 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权. 要求支持两种操作: 操作1:更改某个节点的 ...
- 动画基础--基于Core Animation(3)
参考:https://zsisme.gitbooks.io/ios-/content/ 前面的文章动画基础--基于Core Animation(1),动画基础--基于Core Animation(2) ...
- 【Android开发日记】之入门篇(十五)——ViewPager+自定义无限ViewPager
ViewPager 在 Android 控件中,ViewPager 一直算是使用率比较高的控件,包括首页的banner,tab页的切换都能见到ViewPager的身影. viewpager 来源自 v ...
- python初学--文件操作、字典
文件读写 1.先打开文件 2.读取/写入内容 3.保存文件 文件的open模式有三种 1.w 写模式,它是不能读的 只要用w打开文件,文件中的东西都会被清空 w+, 写读模式,只要沾上w 就会清空 ...
- Leetcode 之Binary Tree Postorder Traversal(45)
层序遍历,使用队列将每层压入,定义两个队列来区分不同的层. vector<vector<int>> levelorderTraversal(TreeNode *root) { ...
- elasticsearch批量删除(查询删除)
注:delete by query只适用于低于elasticsearch2.0的版本(不包含2.0).有两种形式: 1.无请求体 curl -XDELETE 'localhost:9200/twitt ...
- python_day4学习笔记
一.内置函数