Distance on the tree
Distance on the tree
https://nanti.jisuanke.com/t/38229
DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.
The experienced and knowledgeable teacher had known about him even before the first class. However, she didn't wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.
This day, the teacher teaches about trees." A tree with nn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n-1n−1 edges..." DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. "" A tree with nn nodes, which is numbered from 11 to nn. Edge between each two adjacent vertexes uuand vv has a value w, you're asked to answer the number of edge whose value is no more than kk during the path between uu and vv."" If you can't solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."
The problem seems quite easy for DSM. However, it can hardly be solved in a break. It's such a disgrace if DSM can't solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?
Input
In the first line there are two integers n,mn,m, represent the number of vertexes on the tree and queries(2 \le n \le 10^5,1 \le m \le 10^52≤n≤105,1≤m≤105)
The next n-1n−1 lines, each line contains three integers u,v,wu,v,w, indicates there is an undirected edge between nodes uu and vv with value ww. (1 \le u,v \le n,1 \le w \le 10^91≤u,v≤n,1≤w≤109)
The next mm lines, each line contains three integers u,v,ku,v,k , be consistent with the problem given by the teacher above. (1 \le u,v \le n,0 \le k \le 10^9)(1≤u,v≤n,0≤k≤109)
Output
For each query, just print a single line contains the number of edges which meet the condition.
样例输入1复制
3 3
1 3 2
2 3 7
1 3 0
1 2 4
1 2 7
样例输出1复制
0
1
2
样例输入2复制
5 2
1 2 1000000000
1 3 1000000000
2 4 1000000000
3 5 1000000000
2 3 1000000000
4 5 1000000000
样例输出2复制
2
4 题意:给一颗树,有m次询问,每次询问 u v w,求u到v的路径中有多少边权值<=w。
思路:第一次遇到这种题目(还是太菜= =),lca+主席树
以1为根结点,在跑dfs时建立主席树,查询某点时查询根结点到该点有多少条小于等于k的路径,像求lca一样,答案就是查询的那两条路径上的答案 减去 两倍的 根节点到它们最近公共祖先的答案
#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define pb push_back
#define pii pair<int,int>
using namespace std; vector<pii>ve[maxn];
int fa[maxn][],deep[maxn];
int n;
struct sair{
int l,r,v;
}tree[maxn*];
int root[maxn],cnt; void add(int pre,int now,int val,int l,int r){
tree[now].v=tree[pre].v+;
if(l==r){
return;
}
int mid=l+r>>;
if(val<=mid){
tree[now].l=++cnt;
tree[now].r=tree[pre].r;
add(tree[pre].l,tree[now].l,val,l,mid);
}
else{
tree[now].r=++cnt;
tree[now].l=tree[pre].l;
add(tree[pre].r,tree[now].r,val,mid+,r);
}
} int query(int now,int val,int l,int r){
if(l==r){
return tree[now].v;
}
int mid=l+r>>;
if(val<=mid){
return query(tree[now].l,val,l,mid);
}
else{
return tree[tree[now].l].v+query(tree[now].r,val,mid+,r);
}
} void dfs(int now,int pre,int dep){
deep[now]=dep;
fa[now][]=pre;
if(pre==) fa[now][]=now;
for(int i=;i<ve[now].size();i++){
if(ve[now][i].first!=pre){
if(!root[now]) root[now]=++cnt;
if(!root[ve[now][i].first]) root[ve[now][i].first]=++cnt;
add(root[now],root[ve[now][i].first],ve[now][i].second,,1e9+);
dfs(ve[now][i].first,now,dep+);
}
}
} void Init(){
for(int i=;i<=;i++){
for(int j=;j<=n;j++){
fa[j][i]=fa[fa[j][i-]][i-];
}
}
} int lca(int x,int y){
if(deep[x]<deep[y]) swap(x,y);
for(int i=;i>=;i--){
if(deep[fa[x][i]]>=deep[y]){
x=fa[x][i];
}
}
if(x==y) return x;
for(int i=;i>=;i--){
if(fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
} int main(){
int m;
scanf("%d %d",&n,&m);
int u,v,w;
for(int i=;i<n;i++){
scanf("%d %d %d",&u,&v,&w);
ve[u].pb({v,w});
ve[v].pb({u,w});
}
dfs(,,);
Init();
while(m--){
scanf("%d %d %d",&u,&v,&w);
int ans=query(root[u],w,,1e9+)+query(root[v],w,,1e9+);
ans-=query(root[lca(u,v)],w,,1e9+)*;
printf("%d\n",ans);
} }
/*
3 3
1 3 2
2 3 7
1 3 0
1 2 4
1 2 7
*/
Distance on the tree的更多相关文章
- ural1471 Distance in the Tree
Distance in the Tree Time limit: 1.0 secondMemory limit: 64 MB A weighted tree is given. You must fi ...
- 南昌网络赛J. Distance on the tree 树链剖分+主席树
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...
- 南昌网络赛J. Distance on the tree 树链剖分
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...
- 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)
传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
- Distance on the tree(数剖 + 主席树)
题目链接:https://nanti.jisuanke.com/t/38229 题目大意:给你n个点,n-1条边,然后是m次询问,每一次询问给你u,v,w然后问你从u -> v 的路径上有多少边 ...
- 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)
https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...
- 2019南昌邀请赛网络赛:J distance on the tree
1000ms 262144K DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...
- 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...
- 【树形dp】【CF161D】distance on a tree + 【P1352】没有上司的舞会
T1题面: 输入点数为N一棵树 求树上长度恰好为K的路径个数 (n < 1e5, k < 500) 这是今天的考试题,也是一道假的紫题,因为我一个根本不会dp的蒟蒻只知道状态就一遍A掉了- ...
随机推荐
- 用最简单的话告诉你什么是ElasticSearch
介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 L ...
- altera DDR2 IP核之仿真
在生成的IP核文件夹下,有一个testbench文件夹,里面包含了一个example测试激励和DDR2仿真模型. 如下 20 -rw-r--r-- 1 Administrator 197121 171 ...
- @property的使用
1.可以将某个函数变为属性 class Name(): @property def name(self): print('xiaoming') Name().name这里name已经可以当做属性来调用 ...
- Redis深入学习笔记(五)Redis阻塞原因
在实际使用Redis中,有时会碰到客户端timeout异常,或者没有可用连接异常等等异常,总结大概有如下原因: 内部阻塞原因: 1)大对象存取. 2)Fork阻塞. 3)Aof刷盘阻塞(距离上次刷盘大 ...
- Java进程&线程(整理)
Java进程&线程 程序:程序员写的代码,就是代码,不运行好像不会发生什么: 进程:一个进程可以理解为"运行的"一个程序,当我们启动一个java程序后,对应的jvm就会创建 ...
- 直达核心的快速学习PHP入门技巧
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广泛,是目前最火的 ...
- 网络协议,socket模块
"""网络通讯要素:1.物理介质2.通讯协议 osi五层模型 应用层 传输层 网络层 数据链路层 物理层 物理层能传输010101二进制单纯的二进制是没有意义的,必须得知 ...
- Java 8 日期时间 API
转自:https://www.runoob.com/java/java8-datetime-api.html Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与 ...
- 500G !!史上最全的JAVA全套教学视频网盘分享 (JEECG开源社区)
500 G JAVA视频网盘分享(JEECG开源社区) [涵盖从java入门到深入架构,Linux.云计算.分布式.大数据Hadoop.ios.Android.互联网技术应有尽有] JEECG开源社区 ...
- Navicat远程连接不上mysql解决方案
一.can‘t connect to MySql server on ‘47.93.X.X’ 这是因为mysql端口被防火墙拦截,需用linux执行如下指令: 1.#/sbin/iptables -I ...