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-1n1 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^52n105,1m105)

The next n-1n1 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^91u,vn,1w109)

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)(1u,vn,0k109)

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一样,答案就是查询的那两条路径上的答案 减去 两倍的 根节点到它们最近公共祖先的答案
  1 #include<bits/stdc++.h>
  2 #define ll long long
  3 #define maxn 100005
  4 #define lson l,mid,rt<<1
  5 #define rson mid+1,r,rt<<1|1
  6 #define pb push_back
  7 #define pii pair<int,int>
  8 using namespace std;
  9 
 10 vector<pii>ve[maxn];
 11 int fa[maxn][25],deep[maxn];
 12 int n;
 13 struct sair{
 14     int l,r,v;
 15 }tree[maxn*40];
 16 int root[maxn],cnt;
 17 
 18 void add(int pre,int now,int val,int l,int r){
 19     tree[now].v=tree[pre].v+1;
 20     if(l==r){
 21         return;
 22     }
 23     int mid=l+r>>1;
 24     if(val<=mid){
 25         tree[now].l=++cnt;
 26         tree[now].r=tree[pre].r;
 27         add(tree[pre].l,tree[now].l,val,l,mid);
 28     }
 29     else{
 30         tree[now].r=++cnt;
 31         tree[now].l=tree[pre].l;
 32         add(tree[pre].r,tree[now].r,val,mid+1,r);
 33     }
 34 }
 35 
 36 int query(int now,int val,int l,int r){
 37     if(l==r){
 38         return tree[now].v;
 39     }
 40     int mid=l+r>>1;
 41     if(val<=mid){
 42         return query(tree[now].l,val,l,mid);
 43     }
 44     else{
 45         return tree[tree[now].l].v+query(tree[now].r,val,mid+1,r);
 46     }
 47 }
 48 
 49 void dfs(int now,int pre,int dep){
 50     deep[now]=dep;
 51     fa[now][0]=pre;
 52     if(pre==0) fa[now][0]=now;
 53     for(int i=0;i<ve[now].size();i++){
 54         if(ve[now][i].first!=pre){
 55             if(!root[now]) root[now]=++cnt;
 56             if(!root[ve[now][i].first]) root[ve[now][i].first]=++cnt;
 57             add(root[now],root[ve[now][i].first],ve[now][i].second,0,1e9+7);
 58             dfs(ve[now][i].first,now,dep+1);
 59         }
 60     }
 61 }
 62 
 63 void Init(){
 64     for(int i=1;i<=20;i++){
 65         for(int j=1;j<=n;j++){
 66             fa[j][i]=fa[fa[j][i-1]][i-1];
 67         }
 68     }
 69 }
 70 
 71 int lca(int x,int y){
 72     if(deep[x]<deep[y]) swap(x,y);
 73     for(int i=20;i>=0;i--){
 74         if(deep[fa[x][i]]>=deep[y]){
 75             x=fa[x][i];
 76         }
 77     }
 78     if(x==y) return x;
 79     for(int i=20;i>=0;i--){
 80         if(fa[x][i]!=fa[y][i]){
 81             x=fa[x][i];
 82             y=fa[y][i];
 83         }
 84     }
 85     return fa[x][0];
 86 }
 87 
 88 int main(){
 89     int m;
 90     scanf("%d %d",&n,&m);
 91     int u,v,w;
 92     for(int i=1;i<n;i++){
 93         scanf("%d %d %d",&u,&v,&w);
 94         ve[u].pb({v,w});
 95         ve[v].pb({u,w});
 96     }
 97     dfs(1,0,1);
 98     Init();
 99     while(m--){
100         scanf("%d %d %d",&u,&v,&w);
101         int ans=query(root[u],w,0,1e9+7)+query(root[v],w,0,1e9+7);
102         ans-=query(root[lca(u,v)],w,0,1e9+7)*2;
103         printf("%d\n",ans);
104     }
105 
106 }
107 /*
108 3 3
109 1 3 2
110 2 3 7
111 1 3 0
112 1 2 4
113 1 2 7
114 */
View Code

猜你喜欢

转载自www.cnblogs.com/Fighting-sh/p/10752596.html
今日推荐