HDOJ2586 lca查询 tarjan模板程序

本文介绍了一种利用离线Tarjan算法解决村庄中房屋间路径查询问题的方法。通过构建村庄中房屋间的双向道路图,并使用Tarjan算法进行预处理,可以高效地回答任意两点之间的距离查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How far away ?

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


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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3486  2874  2888  3234  2818 

好像离线的算法效率比较高???
然后我就用了离线的tarjan算法了。。。

#include <iostream>
#include <ctime>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int>P;
const int maxn = 4e4+10;
const int maxm = 205;
int father[maxn],dis[maxn],ans[maxm];
int i,n,m;
bool vis[maxn];
vector<P> e[maxn],q[maxn];

void init(){
     int x,y,z;
     cin >> n >> m;
     for (i=0; i<=n; i++) {
          e[i].clear();
          q[i].clear();
          vis[i] = 0;
          father[i] = i;
          dis[i] = 0;
     }

     for (i=1; i<n; i++) {
         cin >> x >> y >> z;
         e[x].push_back(make_pair(y,z));
         e[y].push_back(make_pair(x,z));
     }

     for (i=0; i<m; i++) {
            cin >> x >> y;
            if (x==y) continue;
            q[x].push_back(make_pair(y,i));
            q[y].push_back(make_pair(x,i));
     }
}

int Find(int x) {
     if (father[x]==x) return x;
     return father[x] = Find(father[x]);
}

void Union(int x, int y){
      int fx = Find(x), fy = Find(y);
      if (fx!=fy) father[fx] = fy;
}

void tarjan(int x){
    int i,v,w,lca;
    vis[x] = 1;
    for (i=0; i<e[x].size(); i++) {
        v = e[x][i].first;
        w = e[x][i].second;
        if (!vis[v]) {
             dis[v] = dis[x] + w;
             tarjan(v);
             Union(v,x);
             father[Find(x)] = x;
        }
    }

    for (i=0; i<q[x].size(); i++) {
        v = q[x][i].first;
        w = q[x][i].second;
        if (vis[v]) {
            lca = father[Find(v)];
            ans[w] = dis[x] + dis[v] - 2*dis[lca];
        }
    }
}

int main(){
    std::ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while (T--){
        init();
        tarjan(1);
        for (i=0; i<m; i++) cout << ans[i] << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值