POJ 1986 Distance Queries (在线LCA转RMQ)

题目地址:POJ 1986

纯模板题。输入的最后一个字母是多余的,完全不用管。还有注意询问的时候有相同点的情况。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=50000+10;
int F[MAXN<<1], fir[MAXN], rmq[MAXN<<1], dis[MAXN];
int head[MAXN], cnt, tot;
struct node
{
        int u, v, w, next;
}edge[1000000];
void add(int u, int v, int w)
{
        edge[cnt].v=v;
        edge[cnt].w=w;
        edge[cnt].next=head[u];
        head[u]=cnt++;
}
void dfs(int u, int fa, int dep)
{
        F[++tot]=u;
        fir[u]=tot;
        rmq[tot]=dep;
        for(int i=head[u];i!=-1;i=edge[i].next){
                int v=edge[i].v;
                if(v==fa) continue ;
                dis[v]=dis[u]+edge[i].w;
                dfs(v,u,dep+1);
                F[++tot]=u;
                rmq[tot]=dep;
        }
}
struct ST
{
        int dp[MAXN<<1][30], i, j;
        void init()
        {
                for(i=1;i<=tot;i++){
                        dp[i][0]=i;
                }
                for(j=1;(1<<j)<=tot;j++){
                        for(i=1;i<=tot-(1<<j-1)+1;i++){
                                dp[i][j]=rmq[dp[i][j-1]]<rmq[dp[i+(1<<j-1)][j-1]]?dp[i][j-1]:dp[i+(1<<j-1)][j-1];
                        }
                }
        }
        int Query(int l, int r)
        {
                if(r<l) swap(l,r);
                int k=0;
                while((1<<k+1)<=r-l+1) k++;
                return rmq[dp[l][k]]<rmq[dp[r+1-(1<<k)][k]]?dp[l][k]:dp[r+1-(1<<k)][k];
        }
}st;
void init()
{
        memset(head,-1,sizeof(head));
        memset(dis,0,sizeof(dis));
        cnt=tot=0;
}
int main()
{
        int n, m, i, u, v, k, tmp, w;
        char c;
        scanf("%d%d",&n,&m);
        init();
        while(m--){
                scanf("%d%d%d %c",&u,&v,&w,&c);
                add(u,v,w);
                add(v,u,w);
        }
        dfs(1,-1,0);
        st.init();
        scanf("%d",&k);
        while(k--){
                scanf("%d%d",&u,&v);
                if(u==v){
                        printf("0\n");
                        continue ;
                }
                tmp=F[st.Query(fir[u],fir[v])];
                printf("%d\n",dis[u]+dis[v]-2*dis[tmp]);
        }
        return 0;
}
时间: 2024-10-12 01:14:50

POJ 1986 Distance Queries (在线LCA转RMQ)的相关文章

POJ 1986 Distance Queries(LCA)

[题目链接] http://poj.org/problem?id=1986 [题目大意] 给出一棵树,问任意两点间距离. [题解] u,v之间距离为dis[u]+dis[v]-2*dis[LCA(u,v)] [代码] #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=300010; int d[N],num[N],dis[N],ed=0,x

POJ 1986 Distance Queries LCA两点距离树

标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + dis(root,v) - 2*dis(roor,LCA(u,v)) 求近期公共祖先和dis数组 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int max

POJ 1986 Distance Queries LCA树上两点的距离

题目来源:POJ 1986 Distance Queries 题意:给你一颗树 q次询问 每次询问你两点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + dis(root,v) - 2*dis(roor,LCA(u,v)) 求最近公共祖先和dis数组 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn =

POJ 1986 Distance Queries 【输入YY &amp;&amp; LCA(Tarjan离线)】

任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 16648   Accepted: 5817 Case Time Limit: 1000MS Description Farmer John's cows refused to run in his marathon since he chose a path much t

poj 1986 Distance Queries LCA

题目链接:http://poj.org/problem?id=1986 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

poj 1986 Distance Queries 带权lca 模版题

Distance Queries 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

POJ 题目1986 Distance Queries(LCA 离线)

Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 10142   Accepted: 3575 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 lifesty

POJ 1986 Distance Queries

http://poj.org/problem?id=1986 题意:一棵树里找到两个点的距离.(不用考虑不联通的情况) 题解:LCA模板题. 1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <string> 5 #include <cstdio> 6 #include <cmath> 7 #include <queue>

POJ - 1986 Distance Queries(离线Tarjan算法)

1.一颗树中,给出a,b,求最近的距离.(我没考虑不联通的情况,即不是一颗树的情况) 2.用最近公共祖先来求, 记下根结点到任意一点的距离dis[],这样ans = dis[u] + dis[v] - 2 * dis[lca(u, v)] 3. /* 离线算法,LCATarjan 复杂度O(n+Q); */ #include<iostream> #include<stdio.h> #include<string.h> using namespace std; const