直接点分治, 用平衡树(set就行了...)维护.

-------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<set>
 
using namespace std;
 
typedef long long ll;
 
const int maxn = 10009;
const int maxm = 109;
 
int read() {
char c = getchar();
int ret = 0;
for(; !isdigit(c); c = getchar());
for(; isdigit(c); c = getchar()) ret = ret * 10 + c - '0';
return ret;
}
 

struct edge {

int to, w;
edge* next;
} E[maxn << 1], *pt = E, *head[maxn];
 
void AddEdge(int u, int v, int w) {
pt->to = v; pt->w = w; pt->next = head[u]; head[u] = pt++;
}
 
int n, Rt, Min, size[maxn];
int N, qn;
ll q[maxm];
bool ans[maxm], vis[maxn];
set<ll> Bst;
 
void init() {
N = read(); qn = read();
for(int i = 1; i < N; i++) {
int u = read() - 1, v = read() - 1, w = read();
AddEdge(u, v, w);
AddEdge(v, u, w);
}
for(int i = 0; i < qn; i++)
scanf("%lld", q + i);
n = N;
}
 
void dfs(int x, int fa) {
size[x] = 1;
int Max = 0;
for(edge* e = head[x]; e; e = e->next) if(e->to != fa && !vis[e->to]) {
dfs(e->to, x);
size[x] += size[e->to];
Max = max(size[e->to], Max);
}
Max = max(Max, n - size[x]);
if(Max < Min)
Min = Max, Rt = x;
}
 
void Insert(int x, int fa, ll w) {
Bst.insert(w);
for(edge* e = head[x]; e; e = e->next) if(!vis[e->to] && fa != e->to)
Insert(e->to, x, w + e->w);
}
 
void update(int x, int fa, ll w) {
for(int i = 0; i < qn; i++) if(!ans[i])
ans[i] |= (Bst.find(q[i] - w) != Bst.end());
for(edge* e = head[x]; e; e = e->next) if(!vis[e->to] && fa != e->to)
update(e->to, x, w + e->w);
}
 
void solve(int x) {
Min = maxn;
dfs(x, -1);
x = Rt;
Bst.clear();
Bst.insert(0);
for(edge* e = head[x]; e; e = e->next) if(!vis[e->to]) {
update(e->to, x, e->w);
Insert(e->to, x, e->w);
}
vis[x] = true;
for(edge* e = head[x]; e; e = e->next) if(!vis[e->to]) {
n = size[e->to];
solve(e->to);
}
}
 
int main() {
init();
memset(vis, 0, sizeof vis);
memset(ans, 0, sizeof ans);
solve(0);
for(int i = 0; i < qn; i++)
if(!q[i]) ans[i] = true;
for(int i = 0; i < qn; i++)
puts(ans[i] ? "Yes" : "No");
return 0;
}

-------------------------------------------------------------------------------------

1316: 树上的询问

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 417  Solved: 103
[Submit][Status][Discuss]

Description

一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No.

Input

第一行两个整数n, p分别表示点的个数和询问的个数. 接下来n-1行每行三个数x, y, c,表示有一条树边x→y,长度为c. 接下来p行每行一个数Len,表示询问树中是否存在一条长度为Len的路径.

Output

输出有p行,Yes或No.

Sample Input

6 4
1 2 5
1 3 7
1 4 1
3 5 2
3 6 3
1
8
13
14

Sample Output

Yes
Yes
No
Yes

HINT

30%的数据,n≤100. 
100%的数据,n≤10000,p≤100,长度≤1000000.

做完此题可看下POJ 3237 Tree

Source

BZOJ 1316: 树上的询问( 点分治 + 平衡树 )的更多相关文章

  1. BZOJ 1316: 树上的询问 (点分治+set)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1316 因为只要求存在某条路径长度为K,所以点分,然后用set判断差值是否在set中就可以了. ...

  2. BZOJ 1316: 树上的询问

    挺裸的点分治 刚开始想用map水过去,然后做p次点分治,然后T到自闭 最后发现可以sort一遍,然后去重,记录每个数出现的次数,这样就可以双指针,不会漏掉了 #include <bits/std ...

  3. [BZOJ1316]树上的询问 点分治

    1316: 树上的询问 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1017  Solved: 287[Submit][Status][Discus ...

  4. 【BZOJ1316】树上的询问 点分治+set

    [BZOJ1316]树上的询问 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整数n, ...

  5. BZOJ.3784.树上的路径(点分治 贪心 堆)

    BZOJ \(Description\) 给定一棵\(n\)个点的带权树,求树上\(\frac{n\times(n-1)}{2}\)条路径中,长度最大的\(m\)条路径的长度. \(n\leq5000 ...

  6. [POJ 1316] 树上的询问

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1316 [算法] 点分治 由于边权较大,笔者在计算时使用了STL-set 注意当询问为 ...

  7. BZOJ 3784: 树上的路径 点分治+二分+set

    很容易想出二分这个思路,但是要想办法去掉一个 $log$. 没错,空间换时间. 双指针的部分错了好几次~ Code: #include <set> #include <queue&g ...

  8. BZOJ_1316_树上的询问_点分治

    BZOJ_1316_树上的询问_点分治 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整 ...

  9. [bzoj1316]树上的询问_点分治

    树上的询问 bzoj-1316 题目大意:一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 注释:$1\le n\le 10^4$,$1\ ...

随机推荐

  1. CC++初学者编程教程(1) Visual Stduio2010开发环境搭建

    Visual Studio是微软公司推出的开发环境.是目前最流行的Windows平台应用程序开发环境. Visual Studio 2010版本于2010年4月12日上市,其集成开发环境(IDE)的界 ...

  2. chroot 的用途

    http://www.ibm.com/developerworks/cn/linux/l-cn-chroot/ http://liyongxian.blog.51cto.com/432519/1126 ...

  3. 赵雅智_BroadcastReceiver

    BroadcastReceiver  用于接收程序(包含用户开放的程序和系统内建程序)所发出的Broadcast intent 耗电量 开机启动 窃取别人短信 窃取别人电话 开发: 创建须要启动的Br ...

  4. 全互联结构DVPN综合配置示例

    以下内容摘自正在全面热销的最新网络设备图书“豪华四件套”之一<H3C路由器配置与管理完全手册>(第二版)(其余三本分别是:<Cisco交换机配置与管理完全手册>(第二版).&l ...

  5. springMvc 支持hibernate validator

    SpringMVC 支持Hibernate Validator 发表于9个月前(2014-08-04 11:34)   阅读(1780) | 评论(0) 11人收藏此文章, 我要收藏 赞0 5月23日 ...

  6. MD5和sha1加密算法

    在很多电子商务和社区应用中,我们都要存放很多的客户的资料,其中包括了很多的隐私信息和客户不愿被别人看到的信息,当然好有客户执行各种操作的密码,此时就需要对客户的信息进行加密再存储,目前有两种比较好的加 ...

  7. 更改DataTable列名方法

    1.通过DataAdapter将查询的结果填充到DataSet的表(DataTable)中: 如:dataAdapter.Fill(dataSet),这时dataSet的表名默认为Table 如果使用 ...

  8. Oracle存储过程返回一张表数据

    在oracle数据库中你要在程序里得到一张表的数据就必须先创建游标和SQL Service不一样. --创建游标create or replace package pkg_Dataas type re ...

  9. python正则表达式实例

    1.将"(332.21)luck李."中(332.21)抽取出来同时能够 将”(23)luck李.“中的(23)抽取出来 pp = re.compile('(\(\d*(.\d*) ...

  10. WCF创建到使用到发布

    1,在VS里面新建一个类库项目 2,向类库项目里添加WCF服务文件 3.按照WCF约束规范编写接口和实现类 using System; using System.Collections.Generic ...