HDU 2586 How far away ?LCA Tarjan

47 篇文章 0 订阅
4 篇文章 0 订阅
How far away ?
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13712    Accepted Submission(s): 5142


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
ECJTU 2009 Spring Contest


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

每2个点之间距离唯一 且只有n-1条边 显然是一棵树了 但是边是无向边
在树中 从u->v的路径 必定经过LCA(u,v)
所以tarjan跑一遍 求出所有查询的LCA
n-1条边 m次查询 复杂度为O(n+m)
然后 刚开始脑抽…..对每次查询{u,v} 以LCA(u,v)作为起点跑一遍Dijkstra
LCA(u,v)到u 和到 v的距离和就是查询的答案
m次查询 每次Dijkstra O(nlog(n))
总复杂度为O(m*nlog(n)+n+m)=O(mnlog(n)) 过了

后来才发现…既然是一棵树 从根开始dfs 就可以求出每个点到根的距离
显然 {u->v}的距离=dis[u]+dis[v]-2*dis[LCA(u,v)]

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N=4e4+5;
const int M=200+5;

struct Query{
    int u,v,lca;
};

vector<pii>G[N];//fist:to second:w边权
vector<pii>queryTo[N];//first:to second:这个查询在query[M]中的下标
Query query[M];
int par[N];//并查集
bool visited[N];//Tarjan标记是否被访问过
int dis[N];
inline int find(int x){
    return x==par[x]?x:par[x]=find(par[x]);
}

void Tarjan(int u){
    visited[u]=true;
    for(int i=0;i<G[u].size();++i){
        int to=G[u][i].first;
        if(visited[to]==false){
            Tarjan(to);
            par[to]=u;
        }
    }
    for(int i=0;i<queryTo[u].size();++i){
        int v=queryTo[u][i].first;
        int index=queryTo[u][i].second;
        if(visited[v])
            query[index].lca=find(v);
    }
}

void dfs(int u,int father){
    for(int i=0;i<G[u].size();++i){
        int to=G[u][i].first;
        if(to==father)
            continue;
        int w=G[u][i].second;
        dis[to]=dis[u]+w;
        dfs(to,u);
    }
}

inline int distance(const Query&q){
    return dis[q.u]+dis[q.v]-2*dis[q.lca];
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int T,n,m,u,v,w;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i){
            G[i].clear();
            queryTo[i].clear();
        }
        for(int i=0;i<n-1;++i){
            scanf("%d%d%d",&u,&v,&w);
            G[u].push_back({v,w});
            G[v].push_back({u,w});
        }
        for(int i=0;i<m;++i){
            scanf("%d%d",&u,&v);
            query[i]={u,v,-1};
            queryTo[u].push_back({v,i});
            queryTo[v].push_back({u,i});
        }
        fill(visited,visited+n+1,false);
        for(int i=1;i<=n;++i)
            par[i]=i;
        Tarjan(1);
        dis[1]=0;
        dfs(1,-1);
        for(int i=0;i<m;++i){
            printf("%d\n",distance(query[i]));
        }
    }
    return 0;
}
//Dijkstra....
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N=4e4+5;
const int M=200+5;

struct Query{
    int u,v,lca;
};

vector<pii>G[N];//fist:to second:w边权
vector<pii>queryTo[N];//first:to second:这个查询在query[M]中的下标
Query query[M];
int par[N];//并查集
bool visited[N];//Tarjan标记是否被访问过
int dis[N];//Dijkstra 距离
inline int find(int x){
    return x==par[x]?x:par[x]=find(par[x]);
}

void Tarjan(int u){
    visited[u]=true;
    for(int i=0;i<G[u].size();++i){
        int to=G[u][i].first;
        if(visited[to]==false){
            Tarjan(to);
            par[to]=u;
        }
    }
    for(int i=0;i<queryTo[u].size();++i){
        int v=queryTo[u][i].first;
        int index=queryTo[u][i].second;
        if(visited[v])
            query[index].lca=find(v);
    }
}

struct S{//点x 封装进最小优先队列 重载>运算符
    int x;
};
bool operator>(const S&a,const S&b){
    return dis[a.x]>dis[b.x];
}

int dijkstra(int s,int n){
    fill(dis,dis+n+1,INF);
    dis[s]=0;
    priority_queue<S,vector<S>,greater<S> >queue;
    queue.push({s});
    S tmp;
    while(!queue.empty()){
        int u=queue.top().x;
        queue.pop();
        for(int i=0;i<G[u].size();++i){
            int v=G[u][i].first;
            int w=G[u][i].second;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                tmp.x=v;
                queue.push(tmp);
            }
        }
    }
}

inline int distance(int u,int v,int lca,int n){
    dijkstra(lca,n);
    return dis[u]+dis[v];
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int T,n,m,u,v,w;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i){
            G[i].clear();
            queryTo[i].clear();
        }
        for(int i=0;i<n-1;++i){
            scanf("%d%d%d",&u,&v,&w);
            G[u].push_back({v,w});
            G[v].push_back({u,w});
        }
        for(int i=0;i<m;++i){
            scanf("%d%d",&u,&v);
            query[i]={u,v,-1};
            queryTo[u].push_back({v,i});
            queryTo[v].push_back({u,i});
        }
        fill(visited,visited+n+1,false);
        for(int i=1;i<=n;++i)
            par[i]=i;
        Tarjan(1);
        for(int i=0;i<m;++i){
            printf("%d\n",distance(query[i].u,query[i].v,query[i].lca,n));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值