树的点分治 板题 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 ...
随机推荐
- 通过Fastdfs进行文件上传服务(文件和图片的统一处理)
1.文件上传简单流程分析图: 2.Fastdfs介绍: Fastdfs由两个角色组成: Tracker(集群):调度(帮你找到有空闲的Storage) Storage(集群):文件存储(帮你保存文件或 ...
- Python之路【第二十七篇】:web服务器django
Django 一.web框架 框架,即时framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单的说,就是用别人搭建好的舞台来表演你的才艺 ...
- Spring整合Hibernate的两种方式
在使用spring注解整合hibernate时出现"org.hibernate.MappingException: Unknown entity: com.ssh.entry.Product ...
- Etcd 使用入门
etcd简介 etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库.etcd内部采用raft协议作为一致性算法,etcd基于Go语 ...
- Win8.1下Flash Builder 提示: 找不到所需的Adobe Flash Player调试器版本,解决办法
系统:Win8.1 x64 [解决]安装Flash DeBug最新版本补丁,注意下载对应系统的调试版本 下载地址:http://www.adobe.com/support/flashplayer/do ...
- CSS-服务器端字体笔记
服务器端字体 在CSS3中可以使用@font-face属性来利用服务器端字体. @font-face 属性的使用方法: @font-face{ font-family:webFont; src:ur ...
- MySQL Percona Toolkit--pt-osc执行SQL命令
pt-osc执行日志 在对数据量为100000的表tb004做DROP COLUMN操作,pt-osc工具日志为: Operation, tries, wait: analyze_table, , c ...
- 了解 npm install -S -D 的区别,看这篇就完事了
一.npm install -S -D 的区别 npm install module_name -S 即 npm install module_name --save 写入dependencies n ...
- BS开发扫盲篇-前端技术风云录
BS开发扫盲篇-前端技术风云录 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML诞生 HTML(HyperText Markup Language)超文本标记语言 ...
- 调用百度API返回经纬度
后台调用百度API接口生成: import java.io.BufferedReader; import java.io.IOException;import java.io.InputStreamR ...