2019年ICPC南昌网络赛 J.Distance on the tree

 1000ms 262144K

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 andvv has a value w, you're asked to answer the number of edge whose value is no more than kkduring 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

 

给你一棵树,n个节点(n<1e5),m次询问(m<1e5),每次询问<u,v,k>

即,从节点u到节点v的路上,有多少条边的长度小于等于k

 

 

我本以为是主席树+树剖+边权化点权,没想到可以主席树+lca,关键是没想到可以树上建主席树

 

对于答案来说,一定是

ans\ =\ query(root,u)+query(root,v)-2*query(root,lca(u,v))

因为这个要求的数量是可以加减的,所以可以这样做,然后就是dfs求lca,并且在dfs的时候

把主席树建起来就好,考虑一个点的父亲是其前驱建立主席树就可以了

这里求lca用的是tarjan算法,t了好几发,然后发现自己的并查集没有压缩路径垃圾玩意你退群吧

 

 

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
int n,m,cnt,root[maxn],a[maxn],x,y,k;
//主席树
struct node{int l,r,sum;}T[maxn*40];
vector<int> v;
vector<pair<int,int> > E[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 k){
    T[++cnt] = T[y],T[cnt].sum++,x=cnt;
    if(l == r)return;
    int mid = (l+r)/2;
    if(k<=mid)update(l,mid,T[x].l,T[y].l,k);
    else update(mid+1,r,T[x].r,T[y].r,k);
}
int query(int l,int r,int x,int y,int k){
    if(r<=k)return T[y].sum-T[x].sum;
    int mid = (l+r)/2;
    if(k<=mid)return query(l,mid,T[x].l,T[y].l,k);
    return T[T[y].l].sum-T[T[x].l].sum+query(mid+1,r,T[x].r,T[y].r,k);
}
//tarjan求lca
bool vis[maxn];
int p[maxn],ask[maxn][4];
int Find(int x){return x==p[x]?x:p[x] = Find(p[x]);}
vector<int> Q[maxn];
void tarjan(int u){
    vis[u] = true;
    for(auto i:Q[u]){
        if((ask[i][0] == u&&vis[ask[i][1]])||(ask[i][1] == u&&vis[ask[i][0]])){
            ask[i][2] = Find(ask[i][0]==u?ask[i][1]:ask[i][0]);
        }
    }
    for(auto i:E[u])if(!vis[i.first]){
        update(1,n,root[i.first],root[u],getid(i.second));
        tarjan(i.first);
        p[i.first] = u;
    }
}
int main(){
    for(int i=0;i<maxn;i++)p[i] = i;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n-1;i++){
        scanf("%d%d%d",&x,&y,&k);
        E[x].push_back(make_pair(y,k));
        E[y].push_back(make_pair(x,k));
        v.push_back(k);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    for(int i=1;i<=m;i++){//离线储存
        scanf("%d%d%d",&x,&y,&k);
        Q[x].push_back(i),Q[y].push_back(i);
        ask[i][0] = x,ask[i][1] = y,ask[i][3] = k;
    }
    tarjan(1);
    for(int i=1;i<=m;i++){
        x = ask[i][0],y = ask[i][1],k = ask[i][3];
        int lca = ask[i][2];
        k = upper_bound(v.begin(),v.end(),k)-v.begin();
        if(!k){
            printf("0\n");
            continue;
        }
        int ans = query(1,n,root[1],root[x],k)
                 +query(1,n,root[1],root[y],k)
               -2*query(1,n,root[1],root[lca],k);
        printf("%d\n",ans);
    }
	return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值