给定一棵有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. flask源码系列

    更新中 HTML文档中元素存在,但是在浏览器中不显示.一般用于配合JavaScript代码使用. 04 LocalStack和Local对象实现栈的管理 05 Flask源码之:配置加载 06 Fla ...

  2. Html设置问题(设置浏览器上面的图标,移动设备上面页面保存为图标)

    最近开发了一个新的项目,项目完成之后:要求把页面在移动设备上面保存为图标,通过图标直接进入系统入口(这样看着就想APP一样):刚开始通过百度直接设置了,发现有两个问题,第一.图标直接是页面的截图:第二 ...

  3. mysql 导入sql大文件

    引自:https://dba.stackexchange.com/questions/83125/mysql-any-way-to-import-a-huge-32-gb-sql-dump-faste ...

  4. c#mysql数据库备份还原

    1:引用dll MySql.Data.dll,   MySqlbackup.dll 2:建一个数据连接静态类 public static class mysql { public static str ...

  5. Windows7平台下gitblit服务器安装

    在日常开发工作中,我们通常使用版本控制软件管理团队的源代码,常用的SVN.Git.与SVN相比,Git有分支的概念,可以从主分支创建开发分支,在开发分支测试没有问题之后,再合并到主分支上去,从而避免了 ...

  6. js 取得数组中的最大值和最小值(含多维数组)

    转自:http://www.dewen.org/q/433 方法一: var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math. ...

  7. ajax请求处理概要

    /** *不关心参数传递与参数返回的形式. */ url = ctxPath + '/ccb/xxx '; $.get(url); $.post(url); /** * 常见形式. */ var ur ...

  8. Android笔记(七十五) Android中的图片压缩

    这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...

  9. RS232、RS485和TTL电平与串行通信

    RS232.RS485和TTL 作为一个底层软件开发工程师,经常会碰到RS232.RS485和TTL这一类的问题. 之前总是碰到问题之后Google一下,把当下的问题解决了之后就不管了,过个一两天就忘 ...

  10. 数据库开发-pymysql详解

    数据库开发-pymysql详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Python支持的MySQL驱动 1>.什么是驱动 与MySQL通信就是典型的CS模式.Se ...