hdu 2586 LCA
来源:互联网 发布:南风知我意txt下载西州 编辑:程序博客网 时间:2024/06/04 01:40
链接:戳这里
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
Input
First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
Sample Output
10
25
100
100
题意:
n个点的树,边带权值,m组询问u,v。输出u到v之间的路径权值总和
思路:
LCA裸的题
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define mst(ss,b) memset((ss),(b),sizeof(ss))///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double lb;#define INF (1ll<<60)-1#define Max 1e9using namespace std;int T;int n,m;ll f[40010][22];int fa[40010][22],deep[40010];int head[40010],tot;struct edge{ int v,next,w;}e[80010];void Add(int u,int v,int w){ e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; head[u]=tot++;}void DFS(int u,int Fa,int de){ deep[u]=de; fa[u][0]=Fa; for(int i=head[u];i!=-1;i=e[i].next){ int v=e[i].v; if(v==Fa) continue; f[v][0]=(ll)e[i].w; DFS(v,u,de+1); }}void build(){ for(int i=1;i<=20;i++){ for(int j=1;j<=n;j++){ if(fa[j][i-1]!=-1){ fa[j][i]=fa[fa[j][i-1]][i-1]; f[j][i]=f[j][i-1]+f[fa[j][i-1]][i-1]; } } }}int LCA(int u,int v){ if(deep[u]<deep[v]) swap(u,v); int len=deep[u]-deep[v]; for(int i=0;i<=20;i++){ if(len&(1<<i)) u=fa[u][i]; } for(int i=20;i>=0;i--){ if(fa[u][i]!=fa[v][i]){ u=fa[u][i]; v=fa[v][i]; } } if(u==v) return u; return fa[u][0];}int main(){ scanf("%d",&T); while(T--){ mst(head,-1);tot=0;mst(deep,0);mst(fa,-1);mst(f,0); scanf("%d%d",&n,&m); for(int i=1;i<n;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); Add(u,v,w); Add(v,u,w); } DFS(1,0,0); build(); while(m--){ int u,v; scanf("%d%d",&u,&v); int lca=LCA(u,v); ///printf("lca=%d\n",lca); ll ans=0; int lenu=deep[u]-deep[lca]; for(int i=20;i>=0;i--){ if(lenu&(1<<i)) { ans+=f[u][i]; u=fa[u][i]; } } int lenv=deep[v]-deep[lca]; for(int i=20;i>=0;i--){ if(lenv&(1<<i)) { ans+=f[v][i]; v=fa[v][i]; } } printf("%I64d\n",ans); } } return 0;}
0 0
- HDU 2586 LCA
- HDU - 2586 LCA
- HDU 2586 LCA
- 【HDU 2586】LCA模板
- hdu 2586 LCA
- hdu 2586 lca入门
- lca模板 hdu 2586
- HDU 2586 LCA
- HDU 2586 lca 最近祖先
- hdu 2586 lca模板题
- hdu 2586 lca离线tarjan
- HDU 2586 【LCA-Tarjan-模板】
- hdu 2586在线LCA RMQ
- HDU 2586 LCA转RMQ
- LCA之tarjan HDU 2586
- 【HDU 2586】LCA模版题
- hdu 2586 LCA在线算法
- 周赛 HDU 2874 HDU 2586 LCA
- 111.You are installing Oracle Database 11g on a machine. When you run the installer, the Oracle Univ
- HDOJ-----5631并查集
- Java Basics Part 14/20 - Arrays
- Scala 函数式编程进阶 (1)
- 堆栈和栈
- hdu 2586 LCA
- python线程池实现bug的修改
- Set重写hashCode()和equals()的实例
- python random从集合中随机选择元素
- 随堂笔记7.30:枚举,构建者模式,内部类的详细介绍-------2
- C#控制台 判断字符串的索引值为N的字符是字母,是数字
- hduThe All-purpose Zero+LIS
- 6种手势的讲解
- 第二篇:SWT和JavaFX选哪个?