poj 1986 Distance Queries// 树上两点距离 tarjan(lca) 模板 + 手动vecotor

31 篇文章 0 订阅
14 篇文章 0 订阅

poj 1986 Distance Queries// 树上两点距离 tarjan(lca) 模板 + 手动vecotor模板

Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 17532 Accepted: 6072
Case 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 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

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

https://segmentfault.com/a/1190000015145319?utm_source=index-hottest //tarjan演示图 

题意 求树上两点距离。

tarjan就是一个询问的两个点都访问到时,比如v后访问,找未回溯过的深度较大的u的祖先。

只要学数据结构,大家都可以变成一行怪!

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int max_n=1e5+5;
struct no{int to,cost,ne;};
no e[max_n*2];int head[max_n],edgenum;
void initvector(){edgenum=0;memset(head,-1,sizeof(head));}
void addedge(int from,int to,int c){//头插法
    no E={to,c,head[from]};
    e[edgenum]=E;head[from]=edgenum++;
}
struct qnov{int to,i,ne;};
qnov qe[max_n*2];int qhead[max_n],qedgenum;
void qinitvector(){qedgenum=0;memset(qhead,-1,sizeof(qhead));}
void qaddedge(int from,int to,int i){//头插法
    qnov E={to,i,qhead[from]};
    qe[qedgenum]=E;qhead[from]=qedgenum++;
}

int fa[max_n];
int found(int x){if(fa[x]==x) return x;return fa[x]=found(fa[x]);}

struct qno{int x,y;};qno query[max_n];
bool vis[max_n];int dis[max_n];int ans[max_n];

void tarjan(int u){
    vis[u]=true;
    for(int i=qhead[u];i!=-1;i=qe[i].ne){
        int qid=qe[i].i,to=qe[i].to;
        if(vis[to]) ans[qid]=dis[u]+dis[to]-2*dis[found(to)];//到两点的dis-2*到lca的dis
        //lca为found(to)
    }
    for(int i=head[u];i!=-1;i=e[i].ne){
        int to=e[i].to;
        if(!vis[to]){
            dis[to]=dis[u]+e[i].cost;  //要先更新 出口要使用
            tarjan(to);
            fa[to]=u; //要后更新,不能破坏当前的父节点
        }
    }
}
int main(){
    int n,m;scanf("%d%d",&n,&m);
    initvector();qinitvector();
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++){
        int u,v,w;char ch;
        scanf("%d%d%d %c",&u,&v,&w,&ch);
        addedge(u,v,w);addedge(v,u,w);
    }int st;
    int k;scanf("%d",&k);for(int i=1;i<=k;i++){
        scanf("%d%d",&query[i].x,&query[i].y);
        qaddedge(query[i].x,query[i].y,i);
        qaddedge(query[i].y,query[i].x,i);
        st=query[i].x;
    }
    tarjan(st);
    for(int i=1;i<=k;i++) printf("%d\n",ans[i]);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值