How far away?
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Problem Descriptionthere is n houses in the village and some bidirectional roads connecting them. Every day peole "What is it if I am want to go from House A to house B"? Usually it hard to answer. But luckily int this village the answer was always unique, since the roads was built in the the-the-the-that-there is a unique simp Le path ("simple" means you can ' t visit a place twice) between every the houses. Yout task is to answer all these curious people.
Inputfirst line was a single integer T (t<=10), indicating the number of test cases.
For each test case,in the first line there is the numbers N (2<=n<=40000) and M (1<=m<=200), the number of hous Es and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning this there is a road con Necting House I and House j,with length K (0<k<=40000). The houses is labeled from 1 to N.
Next m lines each have distinct integers i and j, you areato answer the distance between House I and House J.
Outputfor each test case,output m lines. Each line represents the answer of the query. Output a bland line after all test case.
Sample Input23 21 2 103 1 151 22 32 21 2 1001 22 1
Sample Output1025100100
SOURCEECJTU Spring Contest Idea: On the basis of LCA plus dis (distance from the root distance) array, in DFS processing, ANS=DIS[A]-2*DIS[LCA (A, b)]+dis[b];
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>#defineTrue Ture#defineFalse flaseusing namespacestd;#definell Long Long#defineINF 0XFFFFFFFintScan () {intres =0, ch; while( ! (ch = getchar ()) >='0'&& CH <='9' ) ) { if(ch = = EOF)return 1<< - ; } Res= CH-'0' ; while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;}#defineMAXN 40010#defineM 22struct is{ intV,next,w;} EDGE[MAXN*2];intDeep[maxn],jiedge;intDIS[MAXN];intHEAD[MAXN];intRUDU[MAXN];intFa[maxn][m];voidAddintUintVintW) {Jiedge++; EDGE[JIEDGE].V=v; EDGE[JIEDGE].W=W; Edge[jiedge].next=Head[u]; Head[u]=Jiedge;}voidDfsintu) { for(intI=head[u]; I I=Edge[i].next) { intv=edge[i].v; intw=EDGE[I].W; if(!Deep[v]) {Dis[v]=dis[u]+EDGE[I].W; DEEP[V]=deep[u]+1; fa[v][0]=T; DFS (v); } }}voidStintN) { for(intj=1; j<m; J + +) for(intI=1; i<=n; i++) Fa[i][j]=fa[fa[i][j-1]][j-1];}intLCA (intU,intv) { if(Deep[u] <Deep[v]) Swap (U, v); intD = Deep[u]-Deep[v]; inti; for(i =0; i < M; i + +) { if( (1<< i) & D)//note here, try to simulate, you will understand{u=Fa[u][i]; } } if(U = = v)returnu; for(i = M-1; I >=0; I--) { if(Fa[u][i]! =Fa[v][i]) {u=Fa[u][i]; V=Fa[v][i]; }} u= fa[u][0] ; returnu;}voidinit () {memset (head,0,sizeof(head)); memset (FA,0,sizeof(FA)); memset (Rudu,0,sizeof(Rudu)); memset (Deep,0,sizeof(deep)); Jiedge=0;}intMain () {intX,n; intT; scanf ("%d",&t); while(t--) {init (); scanf ("%d%d",&n,&x); for(intI=1; i<n; i++) { intu,v,w; scanf ("%d%d%d",&u,&v,&W); Add (U,V,W); RUDU[V]++; } for(intI=1; i<=n;i++) { if(!Rudu[i]) {Deep[i]=1; Dis[i]=0; DFS (i); Break; }} st (n); while(x--) { intb; scanf ("%d%d",&a,&b); printf ("%d\n", dis[a]-2*dis[lca (A, b)]+Dis[b]); } } return 0;}
View Code
Hdu 2586 how far away? Take right LCA