HDU 2586 How far away?(lca模板)
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14028 Accepted Submission(s): 5295
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.
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
lca模板
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #define N 40010 5 using namespace std; 6 struct node{ 7 int to,len; 8 }; 9 int n,parent[N],len[N],ans[N]; 10 vector<node> edge[N],qedge[N]; 11 bool visit[N]; 12 void init(){ //初始化 13 for(int i=1;i<=n;i++){ 14 edge[i].clear(); 15 qedge[i].clear(); 16 parent[i]=i; 17 visit[i]=false; 18 len[i]=0; 19 } 20 } 21 int find(int x){ 22 if(x==parent[x]){ 23 return x; 24 } 25 return parent[x]=find(parent[x]); 26 } 27 void add(int a,int b){ 28 a=find(a); 29 b=find(b); 30 if(a!=b){ 31 parent[b]=a; 32 } 33 } 34 void lca(int cnt,int sum){ 35 visit[cnt]=true; 36 len[cnt]=sum; 37 int size=edge[cnt].size(); 38 for(int i=0;i<size;i++){ 39 node aa; 40 aa=edge[cnt][i]; 41 if(!visit[aa.to]){ 42 lca(aa.to,aa.len+sum); 43 add(cnt,aa.to); 44 } 45 } 46 size=qedge[cnt].size(); 47 for(int i=0;i<size;i++){ 48 node aa; 49 aa=qedge[cnt][i]; 50 if(visit[aa.to]){ 51 ans[aa.len]=len[cnt]+len[aa.to]-len[find(aa.to)]*2; 52 //cout<<aa.len<<" "<<ans[aa.len]<<endl; 53 } 54 } 55 } 56 int main(){ 57 cin.sync_with_stdio(false); 58 int T,m,a,b,c; 59 cin>>T; 60 while(T--){ 61 cin>>n>>m; 62 init(); 63 for(int i=1;i<n;i++){ 64 cin>>a>>b>>c; 65 node aa; 66 aa.to=b; 67 aa.len=c; 68 edge[a].push_back(aa); 69 aa.to=a; 70 edge[b].push_back(aa); 71 } 72 for(int i=1;i<=m;i++){ 73 cin>>a>>b; 74 node aa; 75 aa.to=b; 76 aa.len=i; 77 qedge[a].push_back(aa); 78 aa.to=a; 79 qedge[b].push_back(aa); 80 } 81 lca(1,0); 82 for(int i=1;i<=m;i++){ 83 cout<<ans[i]<<endl; 84 } 85 } 86 return 0; 87 }