HDU 2586 How far away ? LCA离线tarjan思想

来源:互联网 发布:盛势网络剧哪里可以看 编辑:程序博客网 时间:2024/06/06 01:09

How far away ?

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


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
23 21 2 103 1 151 22 32 21 2 1001 22 1
 

Sample Output
1025100100

解题思路:

1,输入的图和查询的点都告诉你了,离线算法就可以做了

2,tarjan经常用于LCA的离线算法,他本来是一个求联通分量的算法


#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 40005 ;struct Edge {    int form,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] ;bool vis[maxn] ;int len[maxn] ;int find(int x){    return x==f[x]?x:f[x]=find(f[x]);}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]){///这里给双向边都存了结果,得益于神奇的^1运算           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() {    int t ;    scanf("%d",&t) ;    while(t--) {        memset(G,0,sizeof(G)) ;        memset(G2,0,sizeof(G2)) ;        memset(len,0,sizeof(len)) ;        memset(vis,false,sizeof(vis)) ;        edges.clear() ;        edges2.clear() ;        int n,m;        scanf("%d%d",&n,&m) ;        for(int i=0; i<n-1; i++) {            int u,v,w ;            scanf("%d%d%d",&u,&v,&w) ;            add_edge(u,v,w) ;            add_edge(v,u,w) ;        }        for(int i=0; i<m; i++) {            int u,v ;            scanf("%d%d",&u,&v) ;            add_edge2(u,v) ;            add_edge2(v,u) ;        }        LCA(1) ;        for(int i=0; i<m; i++) {            printf("%d\n",edges2[2*i].dist) ;        }    }    return 0;}




阅读全文
0 0
原创粉丝点击