直接点分治, 用平衡树(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. poj2390

    #include <stdio.h> #include <stdlib.h> int main() { int r,m,y,i; scanf("%d %d %d&qu ...

  2. poj2027简单题

    #include <stdio.h> #include <stdlib.h> int main() { int n,x,y; scanf("%d",& ...

  3. i美股投资研报--Michael Kors(IPO版) _Michael Kors(KORS) _i美股

    i美股投资研报--Michael Kors(IPO版) _Michael Kors(KORS) _i美股 i美股投资研报--Michael Kors(IPO版)

  4. python 【第四篇】:面向对象(一)

    1.前言 提笔忘字,感慨良多!python自习前前后后有一年多了吧,貌似花了不少时间,其实没学到啥东西,都是在面向对象编程之前基础知识这块一直打转转,每次到了面向对象这块就感觉很蒙,看两天直接放弃,从 ...

  5. favicon

    <link rel="icon" href="Images/wangyi.ico" type="text/plain" />

  6. JBoss+Ant实现EJB无状态会话bean实例

    EJB分为session bean.entity bean.message-driven bean,session bean又分为无状态会话bean和有状态会话bean. session bean负责 ...

  7. WebService测试工具SoapUI

    尊重原著作:本文转载自http://gqsunrise.iteye.com/blog/1958480 1. 简介 SoapUI是用java开发的测试web service的工具. 2. 安装 2.1. ...

  8. ios不同版本下的UILabel自动换行问题

    @interface ThreadPreviewCell : UITableViewCell @property (nonatomic, strong) UILabel *titleLabel; @e ...

  9. MQ学习(一)----JMS规范(转发整合)

    最近进行ActiveMQ的学习,总结下已被不时之需. JMS规范: JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的A ...

  10. 将窗口置顶的方法:SetWindowPos、AttachThreadInput、SwitchToThisWindow

    将窗口置顶的方法:SetWindowPos.AttachThreadInput.SwitchToThisWindow [转]http://hi.baidu.com/neil_danky/item/f9 ...