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

 

 

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

 

对于答案来说,一定是

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

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

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

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

 

 

 

#include 
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 v;
vector > 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 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

 

 

 

你可能感兴趣的:(思维(抖啊,抖啊,抖个机灵儿),模板类(什么,这也是模板))