hdu-2586 How far away ?(lca+bfs+dfs+线段树)

题目链接:

How far away ?

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

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

题意:

给一个无向图,问两点i和j之间的距离是多少;

思路:

由于询问规模大,所以就无向图转化成树,然后寻找lca,然后再算距离;

把lca转化成RMQ问题,我是用的线段树找的RMQ;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=4e4+4;
typedef long long ll;
const double PI=acos(-1.0);
int n,m,t,dis[N],dep[N],vis[N],w[N];
vector<int>ve[N];
queue<int>qu;
int u[N],v[N],d[N],fa[N],a[4*N],tot,first[N];
struct Tree
{
    int l,r,ans;
};
Tree tree[8*N];
void bfs()//bfs把图转化成树;
{
    qu.push(1);
    vis[1]=1;
    dep[1]=0;
    while(!qu.empty())
    {
        int top=qu.front();
        qu.pop();
        int len=ve[top].size();
        for(int i=0;i<len;i++)
        {
            int x=ve[top][i];
            if(!vis[x])
            {
                vis[x]=1;
                qu.push(x);
                dep[x]=dep[top]+1;
                fa[x]=top;
            }
        }
    }
}
int dfs(int num)//dfs找到lca的数组,并计算i到1的dis
{
    vis[num]=0;
    first[num]=tot;
    a[tot++]=num;
    int len=ve[num].size();
    for(int i=0;i<len;i++)
    {
        int x=ve[num][i];
        if(vis[x])
        {
            dis[x]=dis[num]+d[x];
            dfs(x);
            a[tot++]=num;
        }
    }
}
void Pushup(int node)
{
    if(dep[a[tree[2*node].ans]]>=dep[a[tree[2*node+1].ans]])tree[node].ans=tree[2*node+1].ans;
    else tree[node].ans=tree[2*node].ans;
}//tree[node].ans为数组里的lca的位置;
void build(int node,int L,int R)
{
    tree[node].l=L;
    tree[node].r=R;
    if(L>=R)
    {
        tree[node].ans=L;
        return ;
    }
    int mid=(L+R)>>1;
    build(2*node,L,mid);
    build(2*node+1,mid+1,R);
    Pushup(node);
}
int query(int node,int L,int R)
{
    if(L<=tree[node].l&&R>=tree[node].r)return tree[node].ans;
    int mid=(tree[node].l+tree[node].r)>>1;
    if(R<=mid)return query(2*node,L,R);
    else if(L>mid)return query(2*node+1,L,R);
    else
    {
        int pos1=query(2*node,L,mid),pos2=query(2*node+1,mid+1,R);
        if(dep[a[pos1]]>=dep[a[pos2]])return pos2;
        else return pos1;
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            ve[i].clear();
        }
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&u[i],&v[i],&w[i]);
            ve[u[i]].push_back(v[i]);
            ve[v[i]].push_back(u[i]);
        }
        memset(vis,0,sizeof(vis));
        bfs();
        for(int i=1;i<n;i++)
        {
            if(fa[u[i]]==v[i])
            {
                d[u[i]]=w[i];
            }
            else
            {
                d[v[i]]=w[i];
            }
        }
        tot=1;
        dfs(1);
        build(1,1,tot-1);
        int ql,qr;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&ql,&qr);
            int pos;
            if(first[ql]<=first[qr])
            pos=query(1,first[ql],first[qr]);
            else pos=query(1,first[qr],first[ql]);
            printf("%d\n",dis[ql]-dis[a[pos]]-dis[a[pos]]+dis[qr]);
        }
    }
    return 0;
}
时间: 2024-12-08 18:12:18

hdu-2586 How far away ?(lca+bfs+dfs+线段树)的相关文章

Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序

题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s   内存限制:512.0MB 总提交次数:196   AC次数:65   平均分:58.62 将本题分享到: 查看未格式化的试题   提交   试题讨论 试题来源 2013中国国家集训队第二次作业 问题描述 给定一棵N个节点的树,每个点有一个权值,有M个询问(a,b,c)若a 为1,回答b到c路径上的最小权值,若a为2,回答b到c路径上的最大权值,若a为3,回答b到c路径上的所有权值的

HDU 5877 dfs+ 线段树(或+树状树组)

1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au,查询它有多少个祖先av满足av<=k/au. (1)dfs+线段树 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm>

HDU 5023 A Corrupt Mayor&#39;s Performance Art 线段树区间更新+状态压缩

Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5023 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <string> 7 #include <cmath> 8 using namesp

hdu 4893 (多校1007)Wow! Such Sequence!(线段树&amp;二分&amp;思维)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 352    Accepted Submission(s): 104 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

题解 HDU 3698 Let the light guide us Dp + 线段树优化

http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 759    Accepted Submission(s): 253 Problem Description Plain of despair was

hdu 4970 killing monster 代代相传刷qq 不用线段树啦~

[题意]塔防游戏,一条n长的路上,有m个炮台,可以覆盖[li,ri]范围,威力ci,即每一秒,炮塔可以对范围 内的怪物可以造成ci点伤害.只有有q只怪物,每只怪物有hi点血,出现位置为xi:当怪物血量减少到0或以下时消失,怪物一直朝n位置前进.问有几只怪物可以离开这条路. [题解]用线段树可以做,不过还好我们有代代相传的刷qq 算法 ,让解法变得简单的多~    ^_^ 1 #include <iostream> 2 #include <cstdio> 3 #include <

hdu 5023 A Corrupt Mayor&#39;s Performance Art(线段树区间更新)

#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; int tree[5001000],add[5001000]; int color[50]; int n,m; void pushup(int pos) { tree[pos]=tree[pos<<1]|tree[pos<<1|1]; //更新

hdu 5023 A Corrupt Mayor&#39;s Performance Art (线段树)

把求和操作改为或操作,就可以了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define lson l,m,rt<<1 7 #define rson m+1,r,rt<<1|1 8 using namespace std; 9 const int ma

HDU 2795 Billboard(宣传栏贴公告,线段树应用)

HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意: 要在h*w宣传栏上贴公告,每条公告的高度都是为1的,而且每条公告都要尽量贴最上面最靠左边的,给你一系列的公告的长度,问它们能不能贴上. 分析: 不是很好想,不过想到了就很好写了. 只要把宣传栏倒过来就好办了,这时候就是变成有h条位置可以填公告,填放公告时就可以尽量找最左边的合适的位置来放了. 可以用线段树实现,查找的复杂度是O(logn),需要注意的坑点是h的范围非常