树的点分治 板题 Luogu P3806
给定一棵有n个点的树
询问树上距离为k的点对是否存在。
AC code:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
const int MAXM = 105;
const int MAXK = 10000005;
int n, m, q[MAXM];
int fir[MAXN], to[MAXN<<1], nxt[MAXN<<1], wt[MAXN<<1], cnt;
inline void read(int &num)
{
char ch; int flag=1;
while(!isdigit(ch=getchar()))if(ch=='-')flag=-flag;
for(num=ch-'0';isdigit(ch=getchar());num=num*10+ch-'0');
num*=flag;
}
inline void Add(int u, int v, int w) { to[++cnt] = v; nxt[cnt] = fir[u]; fir[u] = cnt; wt[cnt] = w; }
int total, root, mx[MAXN], dis[MAXN], sz[MAXN];
bool vis[MAXN], Ans[MAXK], Exist[MAXK];
inline bool chkmax(int &x, int y) { return y > x ? x = y, 1 : 0; }
void getroot(int u, int ff)
{
sz[u] = 1, mx[u] = 0;
for(int i = fir[u]; i; i = nxt[i])
if(to[i] != ff && !vis[to[i]])
getroot(to[i], u), sz[u] += sz[to[i]], chkmax(mx[u], sz[to[i]]);
chkmax(mx[u], total-sz[u]);
if(mx[u] < mx[root]) root = u;
}
int stk[MAXN], indx;
inline void dfs(int u, int ff)
{
stk[++indx] = dis[u];
for(int i = fir[u]; i; i = nxt[i])
if(to[i] != ff && !vis[to[i]])
dis[to[i]] = dis[u] + wt[i], dfs(to[i], u);
}
int bin[MAXN], Cnt;
inline void solve(int u)
{
Exist[0] = 1;
for(int i = fir[u]; i; i = nxt[i])
if(!vis[to[i]])
{
dis[to[i]] = wt[i], dfs(to[i], u);
for(int j = 1; j <= indx; j++)
for(int k = 1; k <= m; k++)
if(q[k] >= stk[j]) Ans[k] |= Exist[q[k]-stk[j]];
while(indx) Exist[stk[indx]] = 1, bin[++Cnt] = stk[indx--];
}
while(Cnt) Exist[bin[Cnt--]] = 0;//注意这里memset会超时
}
inline void divide(int u)
{
solve(u);
vis[u] = 1;
for(int i = fir[u]; i; i = nxt[i])
if(!vis[to[i]])
{
total = sz[to[i]], root = 0;
getroot(to[i], u), divide(to[i]);
}
}
int main ()
{
read(n), read(m);
int x, y, z;
for(int i = 1; i < n; i++)
read(x), read(y), read(z), Add(x, y, z), Add(y, x, z);
for(int i = 1; i <= m; i++) read(q[i]);
total = n; mx[root=0] = n;
getroot(1, 0); divide(root);
for(int i = 1; i <= m; i++)
puts(Ans[i] ? "AYE" : "NAY");
}
树的点分治 板题 Luogu P3806的更多相关文章
- poj1741_Tree(树的点分治入门题)
题目链接:poj1741_Tree 题意: 给你一颗n个节点的树,每条边有一个值,问有多少点对(u,v),满足u->v的最短路径小于k. 题解: 典型的树的分治,板子题. #include< ...
- POJ1741 Tree(树的点分治基础题)
Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...
- H - 覆盖的面积(线段树-线段扫描 + 离散化(板题))
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1 ...
- 多项式求逆入门 板题(Luogu P4238)
下面是代码,推导详见 传送门 模板Code #include <cstdio> #include <cstring> #include <algorithm> us ...
- bzoj 2152: 聪聪可可 树的点分治
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 485 Solved: 251[Submit][Status] Descripti ...
- 【poj1741】Tree 树的点分治
题目描述 Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...
- Bipartite Checking CodeForces - 813F (线段树按时间分治)
大意: 动态添边, 询问是否是二分图. 算是个线段树按时间分治入门题, 并查集维护每个点到根的奇偶性即可. #include <iostream> #include <sstream ...
- [luogu P3806] 【模板】点分治1
[luogu P3806] [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条 ...
- Luogu P3690【模板】Link Cut Tree (LCT板题)
省选前刷道LCT板题(话说之前没做这道题-) CODE #include<bits/stdc++.h> using namespace std; inline void read(int ...
随机推荐
- 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用
一般来说,删除节点可分为两个步骤: 首先找到需要删除的节点: 如果找到了,删除它. 说明: 要求算法时间复杂度为 O(h),h 为树的高度. 示例: root = [5,3,6,2,4,null,7] ...
- VB.NET 读写XML
Public Class CSysXML Dim mXmlDoc As New System.Xml.XmlDocument Public XmlFile As String Public Sub N ...
- bootstrap-paginator + ajax 实现动态翻页功能
1.下载http://plugins.jquery.com/bootstrap-paginator/ 2.引用<script src="/static/bootstrap/js/boo ...
- python_三目运算
首先确定三目运算的使用条件, if只有两个才能用三目 只有 if:else: 先写个if else的小例子: if push == "lpush": self.conn.l ...
- golang --写test测试用例
安装gotests插件自动生成测试代码: go get -u -v github.com/cweill/gotests/... 如何编写测试用例 由于go test命令只能在一个相应的目录下执行所有文 ...
- 记一次node爬虫经历,手把手教你爬虫
今天业务突然来了个爬虫业务,爬出来的数据以Excel的形式导出,下班前一个小时开始做,加班一个小时就做好了.因为太久没做爬虫了!做这个需求都是很兴奋! 需求说明 访问网站 (循环)获取页面指定数据源 ...
- Java程序员必了解的JVM原理以及虚拟机的运行过程
JVM概念 虚拟机:指以软件的方式模拟具有完整硬件,VM概念 虚拟机:指以软件的方式模拟具有完整硬件系统功能.运行在一个完全隔离环境中的完整计算机系统 ,是物理机的软件实现.常用的虚拟机有VMWare ...
- springcolud 的学习(四)服务治理. Eureka
什么是服务治理在传统rpc远程调用中,服务与服务依赖关系,管理比较复杂,所以需要使用服务治理,管理服务与服务之间依赖关系,可以实现服务调用.负载均衡.容错等,实现服务发现与注册.服务注册与发现 在服务 ...
- el-select和el-cascader的visible-change下拉框隐藏时触发相关事件(下拉框下拉显示时不触发)
原文:https://blog.csdn.net/CarryBest/article/details/79959389 今天做项目时,用elementUI框架,需要下拉框隐藏时出发某个函数,用了vis ...
- 为什么UDP有时比TCP更有优势?
随着网络技术飞速发展,网速已不再是传输的瓶颈,UDP协议以其简单.传输快的优势,在越来越多场景下取代了TCP.1.网速的提升给UDP稳定性提供可靠网络保障 CDN服务商Akamai(NASDAQ: A ...