题目: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 uu and 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
题意,一棵树,问你从节点u到v的途中经历的边里有几条小于等于k的。
思路:用主席树维护没条边的权值,因为是树,所以可以DFS遍历边建树,这样查询的时候就是u到LCA(u,v)和v到LCA(u,v)满足题意边数之和。
代码:
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; const int maxnbit=20; struct edge{ int to; int w; }; struct Tree{ int l,r; int sum; }tree[maxn*20]; vector<edge> G[maxn]; vector<int> v; int n,m,cnt; int depth[maxn],father[maxn][maxnbit],lg[maxn],root[maxn]; int getid(int x){ return lower_bound(v.begin(),v.end(),x)-v.begin()+1; } void update(int l,int r,int &x,int y,int pos){ tree[++cnt]=tree[y]; tree[cnt].sum++; x=cnt; if(l==r) return ; int mid=(l+r)>>1; if(mid>=pos) update(l,mid,tree[x].l,tree[y].l,pos); else update(mid+1,r,tree[x].r,tree[y].r,pos); } int query(int l,int r,int x,int y,int k){ if(r<=k) return tree[x].sum-tree[y].sum;///l-r都满足,则直接全部加上 int mid=(l+r)>>1; if(mid>=k) return query(l,mid,tree[x].l,tree[y].l,k);///(mid+1,r)都不满足,则接着往(l,mid)找 else return tree[tree[x].l].sum-tree[tree[y].l].sum+query(mid+1,r,tree[x].r,tree[y].r,k);///(l,mid)都满足,加上这段接着往(mid+1,r)找 } int dfs(int nowp,int fa){ depth[nowp]=depth[fa]+1; father[nowp][0]=fa; for(int i=1;i<=lg[depth[nowp]];i++){ father[nowp][i]=father[father[nowp][i-1]][i-1]; } for(int i=0;i<G[nowp].size();i++){ edge v=G[nowp][i]; if(v.to!=fa){ update(1,n,root[v.to],root[nowp],getid(v.w));///建树 dfs(v.to,nowp); } } } int LCA(int x,int y){ if(depth[x]<depth[y]){ swap(x,y); } while(depth[x]!=depth[y]){ x=father[x][lg[depth[x]-depth[y]]]; } if(x==y) return x; for(int i=lg[depth[x]];i>=0;i--){ if(father[x][i]!=father[y][i]){ x=father[x][i]; y=father[y][i]; } } return father[x][0]; } int main(){ lg[0]=-1; for(int i=1;i<maxn;i++){ lg[i]=lg[i>>1]+1; } int x,y,w; scanf("%d%d",&n,&m); for(int i=0;i<n-1;i++){ scanf("%d%d%d",&x,&y,&w); G[x].push_back({y,w}); G[y].push_back({x,w}); v.push_back(w); } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); dfs(1,0);///建图 while(m--){ scanf("%d%d%d",&x,&y,&w); int h=upper_bound(v.begin(),v.end(),w)-v.begin();///由于建树的时候是通过离散化,用下标建的,所以找的时候也用下标 if(h){ int u=LCA(x,y); printf("%d ",query(1,n,root[y],root[u],h)+query(1,n,root[x],root[u],h)); }else{///如果h是0,则就没有比它还小的数了 printf("0 "); } } return 0; }