【简单Trajan_LCA】How far away ?

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8846    Accepted Submission(s): 3083


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
 
题意:给你一棵树,然后有q次询问,对于每次询问求出两点之间的距离。
解法:通过LCA来实现,求出相对路径即可、
代码:2015.8.12
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 #define MAX 400020
 5 using namespace std;
 6 
 7 int fa[MAX];
 8 int Dis[MAX];
 9 int ans[MAX][3];
10 struct edge{int to,next,v;}edg[MAX],omg[MAX];
11 int first[MAX];
12 int first_s[MAX];
13 int sign;
14 int Vis[MAX];/*点标记*/
15 void Cread(int N)
16 {
17     for(int i=0;i<=N;i++)
18     {
19         first[i]=first_s[i]=Dis[i]=0;
20         Vis[i]=1;fa[i]=i;
21     }sign=1;
22 }
23 int Find(int x)
24 {
25     if(fa[x]!=x)fa[x]=Find(fa[x]);
26     return fa[x];
27 }
28 
29 void add_e(int x,int y,int z)
30 {
31     edg[sign].to=y;
32     edg[sign].v=z;
33     edg[sign].next=first[x];
34     first[x]=sign++;
35 }
36 
37 void add_o(int x,int y,int z)
38 {
39     omg[sign].to=y;
40     omg[sign].v=z;
41     omg[sign].next=first_s[x];
42     first_s[x]=sign++;
43 }
44 void Tarjan(int n)
45 {
46     Vis[n]=0;
47     fa[n]=n;
48     for(int i=first_s[n];i;i=omg[i].next)
49     {
50         int TMD=omg[i].to;
51         if(!Vis[TMD])
52         {
53             ans[omg[i].v][2]=Find(TMD);
54         }
55     }
56     for(int i=first[n];i;i=edg[i].next)
57     {
58         int TMD=edg[i].to;
59         if(Vis[TMD])
60         {
61             Dis[TMD]=Dis[n]+edg[i].v;
62             Tarjan(TMD);
63             fa[TMD]=n;
64         }
65     }
66 }
67 int main()
68 {
69     int T,n,m,i,j,a,b,c;
70     int aa,bb;
71     scanf("%d",&T);
72     while(T--)
73     {
74         scanf("%d%d",&n,&m);
75         Cread(n);
76         for(i=1;i<n;i++)
77         {
78             scanf("%d%d%d",&a,&b,&c);
79             add_e(a,b,c);add_e(b,a,c);
80         }
81         for(i=1,sign=1;i<=m;i++)
82         {
83             scanf("%d %d",&aa,&bb);
84             add_o(aa,bb,i);add_o(bb,aa,i);
85             ans[i][0]=aa;ans[i][1]=bb;
86         }
87         Tarjan(1);
88         for (i = 1; i <=m; i++){
89             printf("%d\n", Dis[ans[i][0]] + Dis[ans[i][1]] - 2 * Dis[ans[i][2]]);
90         }
91     }
92     return 0;
93 }
View Code

 

posted @ 2015-08-12 00:02  Wurq  阅读(227)  评论(0编辑  收藏  举报