[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 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...
随机推荐
- IE浏览器Bug总结
每每在网上搜索IE浏览器Bug时,总是骂声一片,特别是前端工程师,每天都要面对,IE浏览器特别是IE6,存在很多Bug,对Web标准的支持也拖后腿,但不可否认,IE浏览器是曾经的霸主,它的贡献也是巨大 ...
- linux进程管理-定时定期执行任务
0.计划任务的命令: at 安排作业在某一时刻执行 batch 安排作业在系统负载不重时执行 crontab 安排周期性运行的作业 1.at命令用法: 安排命令或者多个命令在指定的时间运行一次 语法 ...
- 结合BeautyEye开源UI框架实现的较美观的Java桌面程序
BeautyJavaSwingRobot 结合BeautyEye开源UI框架实现的较美观的Java桌面程序,主要功能就是图灵机器人和一个2345网站万年历的抓取.... 挺简单而且实用的一个项目,实现 ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 【转】Android - Binder机制
以下几篇文章是分析binder机制里讲得还算清楚的 目录 1. Android - Binder机制 - ServiceManager 2. Android - Binder机制 - 普通servic ...
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...
- axios使用
axios 基于promise用于浏览器和node.js的http客户端 特点 支持浏览器和node.js 支持promise 能拦截请求和响应 能转换请求和响应数据 能取消请求 自动转换JSON数据 ...
- Canvas开发库封装
一.Canvas第三方类库 1.常见的第三方类库 konva.js <style> body{ margin:0; } </style> </head> <b ...
- android intent 传数据
1. 基本数据类型 Intent intent = new Intent(); intent.setClass(activity1.this, activity2.class); //描述起点和目标 ...
- Python构造函数使用
1. 子类不定义构造函数时候,默认引用父类构造函数 class A(object): def __init__(self,name): self.name = name def run(self): ...