[HDU]P2586 How far away?[LCA]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HDU]P2586 How far away?[LCA]相关的知识,希望对你有一定的参考价值。
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18675 Accepted Submission(s): 7274
Problem Description
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
Source
Recommend
lcy | We have carefully selected several similar problems for you: 3486 2874 2888 3234 2818
这道题就是很裸的LCA,主要是练一下倍增,今天考试一道有关LCA的,我用树剖打竟然T了?(感觉效率有保证,不知道是不是数据问题)
可恶啊,打了很久诶,于是就来学习一下倍增。
代码:
1 //2017.11.7 2 //lca 3 #include<iostream> 4 #include<cstdio> 5 #include<cstring> 6 using namespace std; 7 inline int read(); 8 namespace lys{ 9 const int N = 4e4 + 7 ; 10 struct edge{ 11 int to; 12 int next; 13 int w; 14 }e[N*3]; 15 int anc[N][17],dis[N][17],dep[N],pre[N]; 16 int n,m,cnt; 17 void swap(int &a,int &b){int t=a;a=b;b=t;} 18 void add(int x,int y,int w){ 19 e[++cnt].to=y;e[cnt].next=pre[x];pre[x]=cnt;e[cnt].w=w; 20 e[++cnt].to=x;e[cnt].next=pre[y];pre[y]=cnt;e[cnt].w=w; 21 } 22 void dfs(int node,int deep){ 23 dep[node]=deep; 24 int i,v; 25 for(i=1;i<=16;i++) anc[node][i]=anc[anc[node][i-1]][i-1],dis[node][i]=dis[node][i-1]+dis[anc[node][i-1]][i-1]; 26 for(i=pre[node];i;i=e[i].next){ 27 v=e[i].to; 28 if(v==anc[node][0]) continue ; 29 anc[v][0]=node; 30 dis[v][0]=e[i].w; 31 dfs(v,deep+1); 32 } 33 } 34 int lca(int x,int y){ 35 int res=0,i; 36 if(dep[x]<dep[y]) swap(x,y); 37 for(i=16;i>=0;i--) 38 if(dep[y]<=anc[x][i]) res+=dis[x][i],x=anc[x][i]; 39 if(x==y) return res; 40 for(i=16;i>=0;i--) 41 if(anc[x][i]!=anc[y][i]) res+=dis[x][i]+dis[y][i],x=anc[x][i],y=anc[y][i]; 42 return res+dis[x][0]+dis[y][0]; 43 } 44 int main(){ 45 memset(pre,0,sizeof pre); 46 int i,u,v,w; 47 n=read(); m=read(); 48 cnt=0; 49 for(i=1;i<n;i++){ 50 u=read(); v=read(); w=read(); 51 add(u,v,w); 52 } 53 dfs(1,1); 54 while(m--){ 55 u=read(); v=read(); 56 printf("%d\n",lca(u,v)); 57 } 58 return 0; 59 } 60 } 61 int main(){ 62 int T=read(); 63 while(T--) lys::main(); 64 return 0; 65 } 66 inline int read(){ 67 int kk=0,ff=1; 68 char c=getchar(); 69 while(c<‘0‘||c>‘9‘){ 70 if(c==‘-‘) ff=-1; 71 c=getchar(); 72 } 73 while(c>=‘0‘&&c<=‘9‘) kk=kk*10+c-‘0‘,c=getchar(); 74 return kk*ff; 75 }
以上是关于[HDU]P2586 How far away?[LCA]的主要内容,如果未能解决你的问题,请参考以下文章