HDU 2586 How far away ?(dfs序,RMQ,LCA,Tarjan)

40 篇文章 0 订阅
6 篇文章 0 订阅

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 

刚接触最近公共祖先,对这个题还没有什么思路,,而且,,我还以为是求最短路类。。。hhh 加油吧~

完全是看这篇博客学习的,原来vector用起来这么方便

https://blog.csdn.net/nameofcsdn/article/details/52230548

dfs序->RMQ->LCA

/*
翻译:
有 n个房子,一些双向路连接他们,人们都喜欢问,如果我想到
A->B 需要多远,答案唯一
n个房子,m条询问
n-1行是代表 i->j 需要k
求询问两个home的最小距离。
这是 lca 吗 怎么那么像最短路。。。才识尚浅呀~~
LCA RMQ ST
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <set>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define maxx 44000
int n,m;
struct Edge
{
    int to,diss;
};
vector <Edge> v[maxx];
int dep[maxx];//每一点的深度
int visnum[maxx<<2];//遍历数组(dfs序)2*n-1  遍历的点
int visn[maxx];//遍历点的遍历数
int vnum;
int mins[maxx<<2][20];//RMQ利用dp在求区间最小值
int dist[maxx];//每点到祖先的距离diss
int fa[maxx];
void dfs(int u,int d,int dis)//dfs序
{//u点 深度 距离
    vector<Edge>::iterator p;
    dep[u]=d;
    dist[u]=dis;
    for(p=v[u].begin();p!=v[u].end();p++)//突然,,,感觉用 vector 真方便诶。。
    {
        if(fa[(*p).to]>-1) continue;
        fa[(*p).to]=u;
        visnum[vnum++]=u;//存入访问的第vnum个点是哪个点
        dfs((*p).to,d+1,dis+(*p).diss);
    }
    visn[u]=vnum;
    visnum[vnum++]=u;
}
void RMQ()//Range Minimum/Maximum Query区间查询最值 ST算法
{
    for(int i=1;i<=2*n-1;i++) mins[i][0]=visnum[i];//初始
    for(int j=1;(1<<j)<=2*n-1;j++)
    {
        for(int i=1;i+(1<<j)-1<=2*n-1;i++)
        {
            int mid=i+(1<<(j-1));//把区间分成长度相同的两段
            if(dep[mins[i][j-1]]<dep[mins[mid][j-1]])
                mins[i][j]=mins[i][j-1];
            else mins[i][j]=mins[mid][j-1];
        }
    }
}
int lca(int x,int y)//求x,y的最近公共祖先
{
    x=visn[x];
    y=visn[y];
    if(x>y) swap(x,y);
    int j=0;
    while((1<<j)<=y-x+1) j++;//在len=y-x+1分成长度为(1<<j)区间
    j--;
    int minn=mins[y+1-(1<<j)][j];
    if(dep[minn]>dep[mins[x][j]])
        minn=mins[x][j];
    return minn;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int a,b,c,x,y;
        cin>>n>>m;
        vnum=1;
        for(int i=1;i<=n;i++)
        {
            v[i].clear();
            fa[i]=-1;
        }
        for(int i=1;i<n;i++)
        {
            cin>>a>>b>>c;
            //借助vector建边
            Edge e1,e2;
            e1.diss=c,e1.to=b;
            v[a].insert(v[a].end(),e1);
            e2.diss=c,e2.to=a;
            v[b].insert(v[b].end(),e2);
        }
        fa[1]=1;
        dfs(1,1,0);
        RMQ();
        while(m--)
        {
            cin>>x>>y;
            cout<<dist[x]+dist[y]-2*dist[lca(x,y)]<<endl;
        }
    }return 0;
}

还有一种 方法是 我在HDU 网站讨论区看的,只使用了 dfs ,有n个房子,n-1条边,深搜的话是一定能搜到的。果然……越简单的方法越容易被遗忘,不过这个题数据有点水, 如果数据很大,直接用 dfs 就爆了 ,就不太适用了……

不管怎么样,贴个代码吧……因为dfs,bfs太弱了 (T▽T)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <set>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define maxx 44000
struct Edge
{
    int to,diss;
}edge;
vector <Edge> mapp[maxx];//相当于二维数组,第一维size为maxx,第二维无限
int vis[maxx];
int flag;//判断是否找到终点
int n,m;
int st,en;
void dfs(int t,int dis)
{
    if(mapp[t].empty())//
        return ;
    if(t==en)
    {
        flag=1;
        cout<<dis<<endl;
        return ;
    }
    for(int j=0;j<mapp[t].size();j++)
    {
        if(!vis[mapp[t][j].to])
        {
            vis[mapp[t][j].to]=1;
            dfs(mapp[t][j].to,dis+mapp[t][j].diss);
            vis[mapp[t][j].to]=0;
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int a,b,c,x,y;
        cin>>n>>m;
        for(int i=1;i<n;i++)
        {
            cin>>a>>b>>c;
            edge.to=b,edge.diss=c;
            mapp[a].push_back(edge);
            edge.to=a,edge.diss=c;
            mapp[b].push_back(edge);
        }
        for(int i=1;i<=m;i++)
        {
            memset(vis,0,sizeof(vis));
            flag=0;
            cin>>st>>en;
            vis[st]=1;
            dfs(st,0);
        }
        for(int i=0;i<n;i++)
            mapp[i].clear();
    }return 0;
}

Other:Tarjan

/*
Tarjan(u)//marge 和 find 为并查集 合并函数和查找函数
{
    for each(u,v)//访问u的所有子节点
    {
        Tarjan(v);//继续往下遍历
        marge(u,v);//合并v到u上
        标记v被放访问过;
    }
    for each(u,e)//访问所有和u有询问关系的e
    {
        如果e被访问过,
        u,e的最近公共祖先为find(e);
    }
}
Tarjan是离线算法,
离线算法是 统一输入后再统一输出,而不是边输入边实时输出
算法复杂度O(n+q) q为查询次数
*/
#pragma comment (linker,"/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#define ll long long
#define inf 0x3f3f3f3f
#define maxx 44000
using namespace std;
int cnt,n,m;
int fa[maxx],x[maxx],y[maxx],z[maxx];
int dis[maxx],vis[maxx],head[maxx];
struct Edge
{
    int to,next,diss;
}edge[maxx<<1];
void add_edge(int u,int v,int val)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    edge[cnt].diss=val;
    head[u]=cnt++;
}
int Find(int x)
{
    if(fa[x]==x) return x;
    else return fa[x]=Find(fa[x]);
}
void tarjan(int u)
{
    vis[u]=1;
    fa[u]=u;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        if(vis[edge[i].to]==0)
        {
            dis[edge[i].to]=dis[u]+edge[i].diss;
            tarjan(edge[i].to);
            fa[edge[i].to]=u;
        }
    }
    for(int i=1;i<=m;i++)
    {
        if(vis[y[i]]&&x[i]==u) z[i]=Find(y[i]);
        if(vis[x[i]]&&y[i]==u) z[i]=Find(x[i]);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int a,b,c;
        cin>>n>>m;
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<n;i++)
        {
            cin>>a>>b>>c;
            add_edge(a,b,c);
            add_edge(b,a,c);
        }
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        memset(z,0,sizeof(z));
        for(int i=1;i<=m;i++)
            cin>>x[i]>>y[i];
        dis[1]=0;
        memset(vis,0,sizeof(vis));
        tarjan(1);
        for(int i=1;i<=m;i++)
        {
            cout<<dis[x[i]]+dis[y[i]]-2*dis[z[i]]<<endl;
        }
    }return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值