给定一棵有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的更多相关文章

  1. poj1741_Tree(树的点分治入门题)

    题目链接:poj1741_Tree 题意: 给你一颗n个节点的树,每条边有一个值,问有多少点对(u,v),满足u->v的最短路径小于k. 题解: 典型的树的分治,板子题. #include< ...

  2. POJ1741 Tree(树的点分治基础题)

    Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...

  3. H - 覆盖的面积(线段树-线段扫描 + 离散化(板题))

    给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1 ...

  4. 多项式求逆入门 板题(Luogu P4238)

    下面是代码,推导详见 传送门 模板Code #include <cstdio> #include <cstring> #include <algorithm> us ...

  5. bzoj 2152: 聪聪可可 树的点分治

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 485  Solved: 251[Submit][Status] Descripti ...

  6. 【poj1741】Tree 树的点分治

    题目描述 Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...

  7. Bipartite Checking CodeForces - 813F (线段树按时间分治)

    大意: 动态添边, 询问是否是二分图. 算是个线段树按时间分治入门题, 并查集维护每个点到根的奇偶性即可. #include <iostream> #include <sstream ...

  8. [luogu P3806] 【模板】点分治1

    [luogu P3806] [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条 ...

  9. Luogu P3690【模板】Link Cut Tree (LCT板题)

    省选前刷道LCT板题(话说之前没做这道题-) CODE #include<bits/stdc++.h> using namespace std; inline void read(int ...

随机推荐

  1. PHP 去一定范围随机小数 随机浮点数

    例如取2到3中的 随机小数(一位)或整数 mt_rand(20,30)/10 mt_rand()是随机取整函数 先扩大一定倍数,再缩小相应倍数,倍数代表精确到哪一位

  2. Python之路【第三十篇】:django 模型层-多表关系

    多表操作 文件为 ---->  orm2 数据库表关系之关联字段与外键约束 一对多Book id title price publish email addr 1 php 100 人民出版社 1 ...

  3. Java的含义

    Java是一种广泛使用的计算机编程语言,拥有跨平台.面向对象.泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发. Java语言它不是软件,这里给各位初学者们详细解释一下.简单来说计算机语言 ...

  4. go编译运行说明

    二  编译运行说明 1.1 编译 1)有了go源文件,通过编译器将其编译成机器可以识别的二进制码文件. 2)在该源文件目录下,通过 go build 对hello.go 文件进行编译.可以指定生成的可 ...

  5. T100 GR 报表常见知识点 (含套版制作)

    轉載至赫非域 > T100 GR 报表常见知识点 前端操作 bron1984 7小时前 5浏览 0评论 8.9.1 注意事项 字体: 如果字型没选对,会造成没设对字型的数据汇出 PDF 格式乱掉 ...

  6. cocos creator图片渲染问题!

    问题:游戏项目需要添加一个开场剧情(); 第一时间使用了cc.component.scheduleOnce (), 里面的回调函数为 cc.loader.loadRes(). 进入游戏时,渲染主场景后 ...

  7. HttpClient参观记:.net core 2.2 对HttpClient到底做了神马

    .net core 于 10月17日发布了 ASP.NET Core 2.2.0 -preview3,在这个版本中,我看到了一个很让我惊喜的新特性:HTTP Client Performance Im ...

  8. Huber Loss 介绍

    Huber Loss 是一个用于回归问题的带参损失函数, 优点是能增强平方误差损失函数(MSE, mean square error)对离群点的鲁棒性. 当预测偏差小于 δ 时,它采用平方误差,当预测 ...

  9. Flanne

    容器面临的问题 物理机A上的应用A看到的IP地址是容器A的,是172.17.0.2,在物理机B上的应用B看到的IP地址是容器B的,不巧也是172.17.0.2,当它们都注册到注册中心的时候,注册中心就 ...

  10. MVC中Model BLL层Model模型互转

    MVC中Model BLL层Model模型互转 一. 模型通常可以做2种:充血模型和失血模型,一般做法是模型就是模型,不具备方法来操作,只具有属性,这种叫做失血模型(可能不准确):具备对模型一定的简单 ...