POJ 1986 Distance Queries LCA离线算法tarjan

来源:互联网 发布:怎样走网络群众路线 编辑:程序博客网 时间:2024/06/05 15:22

Distance Queries
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 13792 Accepted: 4870Case 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. 

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 61 6 13 E6 3 9 E3 5 7 S4 1 3 N2 4 20 W4 7 2 S31 61 42 6

Sample Output

13336

Hint

Farms 2 and 6 are 20+3+13=36 apart. 


解题思路:

1,输入的就是一个树,n个点m个边,后面的输入的字母没发现什么含义,可能就是迷惑人的,忽视掉。

2,LCA的tarjan算法,这个算法网上到处都有,//其实只看代码也看得懂,挺简单的思路


#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 100005 ;int n,m;struct Edge{    int from,to,dist ;};vector<Edge> edges ;vector<int> G[maxn] ;void add_edge(int from,int to,int dist){    edges.push_back((Edge){from,to,dist}) ;    int mm = edges.size() ;    G[from].push_back(mm-1) ;}vector<Edge> edges2 ;vector<int> G2[maxn] ;void add_edge2(int from,int to,int dist=0){    edges2.push_back((Edge){from,to,dist}) ;    int mm = edges2.size() ;    G2[from].push_back(mm-1) ;}int f[maxn] ;int find(int x){    return x==f[x]?x:f[x]=find(f[x]) ;}bool vis[maxn] ;int len[maxn] ;void LCA(int u){    f[u] = u ;    vis[u] = true ;    for(int i=0;i<G[u].size();i++){        if(!vis[edges[G[u][i]].to]){            len[edges[G[u][i]].to] = len[u]+edges[G[u][i]].dist ;            LCA(edges[G[u][i]].to) ;            f[edges[G[u][i]].to] = u ;        }    }    for(int i=0;i<G2[u].size();i++){        if(vis[edges2[G2[u][i]].to]){            edges2[G2[u][i]^1].dist = edges2[G2[u][i]].dist = len[edges2[G2[u][i]].to]+len[u]-2*len[find(edges2[G2[u][i]].to)] ;        }    }}int main(){    while(~scanf("%d%d",&n,&m)){        memset(G,0,sizeof(G)) ;        memset(G2,0,sizeof(G2)) ;        memset(f,0,sizeof(f)) ;        memset(vis,false,sizeof(vis)) ;        memset(len,0,sizeof(len)) ;        edges.clear() ;        edges2.clear() ;        for(int i=0;i<n-1;i++){            int u,v,w;            char c[2];            scanf("%d%d%d%s",&u,&v,&w,c);            add_edge(u,v,w);            add_edge(v,u,w);        }        int k;        scanf("%d",&k) ;        for(int i=0;i<k;i++){            int u,v;            scanf("%d%d",&u,&v);            add_edge2(u,v);            add_edge2(v,u);        }        LCA(1) ;        for(int i=0;i<k;i++){            printf("%d\n",edges2[2*i].dist) ;        }    }    return 0;}




原创粉丝点击