2019 icpc南昌邀请赛网络赛 J - Distance on the tree

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

题意就是 带权树上任意两点之间边权值不超过k的边个数

树上dfs建主席树,记录根结点到当前结点的边权,查询就是在root[u]和root[v]之间0~w的权值和 加起来,减去u和v的LCA的2倍   (这个其实类似求树上两点之间的距离,u的深度+v的深度-2*lca(u,v)的深度) 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn =1e5+10;
const int INF=0x3f3f3f3f;
int dep[maxn],Fa[maxn][22],root[maxn],sum[maxn*40],ls[maxn*40],rs[maxn*40],cnt,n,m;
struct  node
{
	int p,w;
};
std::vector<node> G[maxn];
void update(int pre,int &o,int l,int r,int k)
{
	o=++cnt;ls[o]=ls[pre],rs[o]=rs[pre];sum[o]=sum[pre]+1;
	if(l==r) return ;
	int m=(1ll*l+r)>>1;
	if(k<=m) update(ls[pre],ls[o],l,m,k);
	else update(rs[pre],rs[o],m+1,r,k);
}
int query(int root,int l,int r,int k)
{
	if(l==r) return sum[root];
	int m=(1ll*l+r)>>1;
	if(k<=m) return query(ls[root],l,m,k);
	else return sum[ls[root]]+query(rs[root],m+1,r,k);
}

void dfs(int u,int fa)
{
	dep[u]=dep[fa]+1;
	Fa[u][0]=fa;
	for(int i=1;(1<<i)<=dep[u];i++)  Fa[u][i]=Fa[Fa[u][i-1]][i-1];
	for(auto &v : G[u])
	{
		if(v.p==fa) continue;
		update(root[u],root[v.p],0,INF,v.w);
		dfs(v.p,u);
	}
}
int lca(int x,int y)
{
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=20;i>=0;i--) if((1<<i)<=dep[x]-dep[y]) x=Fa[x][i];
	if(x==y) return x;
    for(int i=20;i>=0;i--) if(Fa[x][i]!=Fa[y][i]) x=Fa[x][i],y=Fa[y][i];
    return Fa[x][0];
}
int main(int argc, char const *argv[])
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		int u,v,w; scanf("%d%d%d",&u,&v,&w);
		G[u].emplace_back((node){v,w});
		G[v].emplace_back((node){u,w});
	}
	dfs(1,0);
	while(m--)
	{
		int u,v,w;scanf("%d%d%d",&u,&v,&w);
		printf("%d\n",query(root[u],0,INF,w)+query(root[v],0,INF,w)-2*query(root[lca(u,v)],0,INF,w));
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值