【洛谷2912】[USACO08OCT]牧场散步Pasture Walking

25 篇文章 0 订阅
4 篇文章 0 订阅

牧场散步Pasture Walking

题目描述

The N cows (2 <= N <= 1,000) conveniently numbered 1..Nare grazing among the N pastures also conveniently numbered 1..N. Mostconveniently of all, cow i is grazing in pasture i.

Some pairs of pastures are connected by one of N-1 bidirectionalwalkways that the cows can traverse. Walkway i connects pastures A_i and B_i (1<= A_i <= N; 1 <= B_i <= N) and has a length of L_i (1 <= L_i<= 10,000).

The walkways are set up in such a way that between any twodistinct pastures, there is exactly one path of walkways that travels betweenthem. Thus, the walkways form a tree.

The cows are very social and wish to visit each other often.Ever in a hurry, they want you to help them schedule their visits by computingthe lengths of the paths between 1 <= L_i <= 10,000 pairs of pastures(each pair given as a query p1,p2 (1 <= p1 <= N; 1 <= p2 <= N).

POINTS: 200

N(2<=N<=1000)头奶牛,编号为1W,它们正在同样编号为1N的牧场上行走.为了方便,我们假设编号为i的牛恰好在第i号牧场上.

有一些牧场间每两个牧场用一条双向道路相连,道路总共有N - 1条,奶牛可以在这些道路上行走.i条道路把第Ai个牧场和第Bi个牧场连了起来(1 <= A_i <= N; 1 <= B_i <= N),而它的长度 1 <= L_i <= 10,000.在任意两个牧场间,有且仅有一条由若干道路组成的路径相连.也就是说,所有的道路构成了一棵树.

奶牛们十分希望经常互相见面.它们十分着急,所以希望你帮助它们计划它们的行程,你只需要计算出Q(1 < Q < 1000)对点之间的路径长度每对点以一个询问p1,p2 (1 <= p1 <= N; 1 <= p2 <= N). 的形式给出.

输入输出格式

输入格式:
 

Line 1: Two space-separatedintegers: N and Q

Lines 2..N: Line i+1contains three space-separated integers: A_i, B_i, and L_i

Lines N+1..N+Q: Each line contains two space-separated integers representing two distinct pastures between which the cows wish to travel: p1 and p2

输出格式:
 

Lines 1..Q: Line i contains the length of the path between the two pastures in query i.

输入输出样例

输入样例#1 

4 2 
2 1 2 
4 3 2 
1 4 3 
1 2 
3 2 

输出样例#1 

2 
7 

 

dist保存的是到根节点的距离,两个点到根节点的距离再减去两次最近公共祖先到根节点的距离就是两点的距离。(稍微yy一下就能明白了=。=)

 

 

#include<iostream>
#include<cstdio>
#define N 50050
using namespace std;

int n,q,tot;
int head[N],next[N],to[N],c[N],depth[N],dist[N],fa[N][32],du[N];

void add_edge(int x,int y,int z)
{
    to[++tot]=y;
    c[tot]=z;
    next[tot]=head[x];
    head[x]=tot;
}
void dfs(int x,int yeye)
{
    for (int i=head[x];i;i=next[i])
    {
        int y=to[i];
        if (y != yeye)
        {
            depth[y]=depth[x]+1;
            dist[y]=dist[x]+c[i];
            fa[y][0]=x;
            for (int j=1;j<32;j++) fa[y][j]=fa[fa[y][j-1]][j-1];
            dfs(y,x);
        }
    }
}
int lca(int x,int y)
{
    if (depth[x]<depth[y]) swap(x,y);
    int d=depth[x]-depth[y];
    for (int i=0;i<31;i++)
        if (d&(1<<i)) x=fa[x][i];
    for (int i=31;i>=0;i--)
        if (fa[x][i] != fa[y][i]) x=fa[x][i],y=fa[y][i];
    return (x == y?x:fa[x][0]);
}


int main()
{
    tot=0;
    cin>>n>>q;
    for (int i=1;i<n;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        add_edge(x,y,z);
        add_edge(y,x,z);
        du[x]++;du[y]++;
    }
    for (int i=1;i<=n;i++)
        if (du[i] == 1)
        {
            depth[i]=1;
            dist[i]=0;
            dfs(i,0);
            break;
        }
    for (int i=0;i<q;i++)
    {
        int x,y;
        cin>>x>>y;
        cout<<(dist[x]+dist[y])-(2*dist[lca(x,y)])<<endl;
    }
    return 0;
}

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值