直接点分治, 用平衡树(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. jquery插件tab——小试牛刀

    <html> <head> <script type="text/javascript" src="http://ajax.googleap ...

  2. Seinfeld(栈模拟)

    Seinfeld Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. ECSHOP 模版文件里的编辑区域

    Ecshop 中的模板能够有可编辑区域,在模板中是通过 <!-- TemplateBeginEditable name="左边区域" --> <!-- Templ ...

  4. 提高你的Java代码质量吧:使用构造函数协助描述枚举项

    一.分析 一般来说,我们经常使用的枚举项只有一个属性,即排序号,其默认值是从0.1.2... ....但是除了排序号外,枚举还有一个(或多个)属性. 二.场景 比如,可以通过枚举构造函数声明业务值,定 ...

  5. TabHost 简单用法

    package com.google.tabhost;    import android.app.TabActivity;  import android.os.Bundle;  import an ...

  6. 关于ECMAScript6一些知识

    ECMAScript 是当前 JavaScript 语言规范的最新标准,一般称为 es6, 但是因为 该标准规范是在 2015年6月份发布的,所以也叫作 ECMAScript 2015 let 变量声 ...

  7. 制作U盘启动盘(以CentOS6.3为例)

    借助UltraISO(软碟通),自己百度下载一个即可(同样适用于制作Windows启动盘). 选择文件→打开,选择ISO镜像所在目录,如下两幅图所示:

  8. C# Attribute(特性)之---数据契约 [DataContract]

    服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送,如下所 ...

  9. .Net平台-MVP模式初探(一)

    为什么要写这篇文章 笔者当前正在负责研究所中一个项目,这个项目基于.NET平台,初步拟采用C/S部署体系,所以选择了Windows Forms作为其UI.经过几此迭代,我们发现了一个问题:虽然业务逻辑 ...

  10. webService接口大全

    中文<->英文双向翻译WEB服务 获得标准数据 Endpoint: http://fy.webxml.com.cn/webservices/EnglishChinese.asmx Disc ...