POJ1986 Distance Queries (LCA)
传送门:
http://poj.org/problem?id=1986
Distance Queries
Time Limit: 2000MS | Memory Limit: 30000K | |
Case Time Limit: 1000MS |
Description
Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!
Input
* Lines 1..1+M: Same format as "Navigation Nightmare"
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.
Sample Input
7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 3 1 6 1 4 2 6
Sample Output
13 3 36
Hint
Farms 2 and 6 are 20+3+13=36 apart.
Source
1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 100010; 10 #define For(i,n) for(int i=1;i<=n;i++) 11 #define Rep(i,l,r) for(int i=l;i<=r;i++) 12 13 struct Edge{ 14 int t,next,w; 15 }E[N*2]; 16 vector< pair<int,int> > Q[N]; 17 int s,t,w,Es,ans[N],head[N],fa[N],d[N]; 18 bool vis[N]; 19 20 int find(int i){ 21 if(fa[i]==i) return i; 22 else return find(fa[i]); 23 } 24 25 void makelist(int s,int t,int w){ 26 E[Es].t = t; E[Es].w = w;E[Es].next = head[s]; 27 head[s] = Es++; 28 } 29 30 void LCA(int i,int w){ 31 d[i] = w;fa[i] = i; 32 vis[i] = true; 33 for(int p=head[i];p!=-1;p=E[p].next) 34 if(!vis[E[p].t]){ 35 LCA(E[p].t,w+E[p].w); 36 fa[E[p].t] = i; 37 } 38 int len = Q[i].size(); 39 for(int j=0;j<len;j++){ 40 if(vis[Q[i][j].first]) 41 ans[Q[i][j].second] = w + d[Q[i][j].first] - 2 * d[find(Q[i][j].first)]; 42 } 43 } 44 int n,m,qn; 45 int main(){ 46 memset(head,-1,sizeof(head)); 47 scanf("%d%d",&n,&m); 48 For(i,m){ 49 scanf("%d%d%d %*c",&s,&t,&w); 50 makelist(s,t,w);makelist(t,s,w); 51 } 52 scanf("%d",&qn); 53 For(i,qn){ 54 scanf("%d%d",&s,&t); 55 Q[s].push_back(make_pair(t,i));Q[t].push_back(make_pair(s,i)); 56 } 57 LCA(1,0); 58 For(i,qn) printf("%d\n",ans[i]); 59 return 0; 60 }
1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 100010; 10 const int M = 16; 11 #define For(i,n) for(int i=1;i<=n;i++) 12 #define Rep(i,l,r) for(int i=l;i<=r;i++) 13 #define Down(i,r,l) for(int i=r;i>=l;i--) 14 struct Edge{ 15 int t,next,w; 16 }E[N*2]; 17 int root,s,t,w,Es,ans[N],head[N],deep[N],d[N],fa[N][M*2]; 18 bool vis[N]; 19 20 void dfs(int i,int last){ 21 for(int p=head[i];p!=-1;p=E[p].next) 22 if(!vis[E[p].t]){ 23 vis[E[p].t] = true; 24 deep[E[p].t] = deep[i] + 1; 25 d[E[p].t] = d[i] + E[p].w; 26 fa[E[p].t][0] = i; 27 For(j,M) 28 if((1<<j)<deep[E[p].t]) fa[E[p].t][j] = fa[fa[E[p].t][j-1]][j-1]; 29 dfs(E[p].t,i); 30 } 31 } 32 33 void makelist(int s,int t,int w){ 34 E[Es].t = t; E[Es].w = w;E[Es].next = head[s]; 35 head[s] = Es++; 36 } 37 38 int LCA(int u,int v){ 39 if(deep[u] < deep[v]) swap(u,v); 40 int d = deep[u] - deep[v]; 41 Rep(i,0,M) 42 if((1<<i)&d) u = fa[u][i]; 43 if (u==v) return u; 44 Down(i,M,0) 45 if(fa[u][i]!=fa[v][i]) u = fa[u][i] , v = fa[v][i]; 46 if(u!=v) u = fa[u][0]; 47 return u; 48 } 49 50 int n,m,qn; 51 int main(){ 52 memset(head,-1,sizeof(head)); 53 scanf("%d%d",&n,&m); 54 For(i,m){ 55 scanf("%d%d%d %*c",&s,&t,&w); 56 makelist(s,t,w);makelist(t,s,w); 57 } 58 deep[1] = 1; 59 dfs(1,0); 60 scanf("%d",&qn); 61 For(i,qn){ 62 scanf("%d%d",&s,&t); 63 printf("%d\n",d[s] + d[t] - 2*d[LCA(s,t)]); 64 } 65 return 0; 66 }