Link:

P3806 传送门

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的更多相关文章

  1. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  2. [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 ...

  3. [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 ...

  4. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  5. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  6. 算法上机题目mergesort,priority queue,Quicksort,divide and conquer

    1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...

  7. The Divide and Conquer Approach - 归并排序

    The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...

  8. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  9. [算法]分治算法(Divide and Conquer)

    转载请注明:http://www.cnblogs.com/StartoverX/p/4575744.html 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...

随机推荐

  1. zabbix的命令执行

    1.对于低版本的可用下列exp直接打到用户 http://119.29.48.232/zabbix/httpmon.php?applications=2 and (select 1 from (sel ...

  2. csc_滤镜filter和实现透明的两种方式

    有这样一个需求,给一个地图实现半透明效果. 使用css滤镜属性可以实现:filter. 下面是属性的所以值 filter: none | blur() | brightness() | contras ...

  3. perl发送post数据

    把post数据写进一个匿名数组里就行 #!/usr/bin/env perl -w use strict; use LWP::UserAgent; my $ua = LWP::UserAgent-&g ...

  4. JSON对象与字符串之间的相互转换

    <html> <head> <meta name="viewport" content="width=device-width" ...

  5. 【LOJ6201】【bzoj4939】【YNOI2016】掉进兔子洞

    一道比较简单的莫队…… 用bitset维护三个区间的交元素. #include<bits/stdc++.h> ; ; #define UI unsigned int #define rep ...

  6. c json实战引擎五 , 优化重构

    引言 scjson是一个小巧的纯c跨平台小巧引擎. 适用于替换老的cJSON引擎的场景. 数据结构和代码布局做了大量改进.优势体现在以下几个方面: 1) 跨平台 (window 10 + VS2017 ...

  7. centos7 安装 NVIDIA Docker

    安装环境: 1.centos7.3 2.NVIDIA Corporation GP106 [GeForce GTX 1060 6GB] 安装nvidia-docker a.安装docker 可参考ce ...

  8. google fcm 推送的流程

    总结:1.给一个人推,能成功,2.给多个人推,有两种,一种是给组推,一种是给主题推,之前用的是组推,但是不成功,这里换成主题推: <?phpnamespace App\Http\Controll ...

  9. thinkphp模板常用的方法

    thinkphp模板我是看了3.2的文档,对里面的东西过了一遍,然后在写到需要用到模板的东西的时候就有印象,有的能直接回顾,但是有的就可能只知道有这个东西,但是不知道怎么用,所以就重新查手册,这个的话 ...

  10. SPOJ 375

    默默一看提交时间 -- 这是我以前的代码风格-- #include <cstdio> #include <cstring> #include <vector> #i ...