ST和LCA和无根树连接

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <string>
#include<cstdio>  
#include<vector>  
#include<cmath>  
#include <queue>
#include <map>
#include <vector>
#define ll __int64
#define mp make_pair<int, int>
using namespace std;
#define de(x) cout << #x << "=" << x << endl
const int maxn=50000+10;
int fa[maxn]={0};
int f[25][maxn]={0};
int len[25][maxn]={0};
int depth[maxn]={0};
vector<pair<int, int> > edge[maxn*2];
void dfs(int a)
{
	depth[a]=depth[f[0][a]]+1;
	for(int i=1;(1<<i)<=depth[a];i++)
	{
		f[i][a]=f[i-1][f[i-1][a]];
		len[i][a]=len[i-1][f[i-1][a]]+len[i-1][a];
	}
	fa[a]=1;
	for(int i=0;i<edge[a].size();i++)
	{
		int j=edge[a][i].first;
		int k=edge[a][i].second;
		if(!fa[j])
		{
			fa[j]=1;
			f[0][j]=a;
			len[0][j]=k;
			dfs(j);
		}
	}
}
int ST(int a,int b)
{
	int ans=0;
	 
	if(depth[a]<depth[b]) swap(a,b);
	for(int i=19,d=depth[a]-depth[b];i>=0;i--)
	{
		//cout<<"djfhjaksfhjkasfjk";
		if(d>>i&1)//是一个&不是两个,因为这个wa了一个下午 ,ri!
		{
			//de(i);
			ans+=len[i][a];
			a=f[i][a];	
		}
	}
	//cout<<a<<"  "<<b<<endl;
	//cout<<ans<<endl;
	if(a==b) return ans;
	//cout<<ans<<endl;
	//s=ceil(log(dep[x])/log(2));
	for(int i=19;i>=0;i--)
	{
		if(f[i][a]!=f[i][b])
		{
			ans+=len[i][a];
			a=f[i][a];
			ans+=len[i][b];
			b=f[i][b];
		}
	}
	return ans+len[0][a]+len[0][b];
}
int main()
{
	int n,m;
	memset(fa,0,sizeof(fa));
	memset(depth,0,sizeof(depth));
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		int u,v,len;
		scanf("%d %d %d",&u,&v,&len);
		//cout<<u<<" "<<v<<" "<<len<<endl;
		edge[u].push_back(mp(v,len));
		//cout<<u<<" "<<v<<endl;
		edge[v].push_back(mp(u,len));
		//cout<<u<<" "<<v<<endl;
	}
	dfs(1);//因为是无根树,所以可以任取一个节点,作为根
	//for(int i=1;i<=n;i++) cout<<depth[i]<<endl;
	//for(int j=1;j<=n;j++) cout<<len[1][j]<<endl; 
	/*for(int i=19;(1<<i)<=depth[j];i--)
	{
		cout<<len[i][j]<<endl;
	}*/
	scanf("%d",&m);
	for(int i=0;i<m;i++)
	{
		int u,v;
		scanf("%d %d",&u,&v);
		int ans=ST(u,v);
		printf("%d\n",ans);
	}
	return 0;
}

 题目连接:http://codevs.cn/problem/2370/

转载于:https://www.cnblogs.com/chinacwj/p/7183425.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值