洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)

时间:2023-03-09 16:21:32
洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)

题目描述

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

Some pairs of pastures are connected by one of N-1 bidirectional walkways 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 two distinct pastures, there is exactly one path of walkways that travels between them. 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 computing the 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)头奶牛,编号为1到W,它们正在同样编号为1到N的牧场上行走.为了方 便,我们假设编号为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-separated integers: N and Q

  • Lines 2..N: Line i+1 contains 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

说明

Query 1: The walkway between pastures 1 and 2 has length 2.

Query 2: Travel through the walkway between pastures 3 and 4, then the one between 4 and 1, and finally the one between 1 and 2, for a total length of 7.

代码:

#include<vector>
#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10001
using namespace std;
vector<pair<int,int> >vec[N];
int n,m,x,y,z,fa[N],deep[N],dis[N],size[N],top[N];
int lca(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(deep[x]<deep[y])
         swap(x,y);
        x=fa[top[x]];
    }
    if(deep[x]>deep[y])  swap(x,y);
    return x;
}
int dfs(int x)
{
    size[x]=1;
    deep[x]=deep[fa[x]]+1;
    for(int i=0;i<vec[x].size();i++)
    {
        if(fa[x]!=vec[x][i].first)
        {
            fa[vec[x][i].first]=x;
            dis[vec[x][i].first]=dis[x]+vec[x][i].second;
            dfs(vec[x][i].first);
            size[x]+=size[vec[x][i].first];
        }
    }
}
int dfs1(int x)
{
    int t=0;
    if(!top[x]) top[x]=x;
    for(int i=0;i<vec[x].size();i++)
     if(vec[x][i].first!=fa[x]&&size[x]<size[vec[x][i].first])
    	t=vec[x][i].first;
	if(t)   top[t]=top[x],dfs1(t);
	for(int i=0;i<vec[x].size();i++)
	  if(vec[x][i].first!=fa[x]&&vec[x][i].first!=t)
	   dfs1(vec[x][i].first);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        vec[x].push_back(make_pair(y,z));
        vec[y].push_back(make_pair(x,z));
    }
    dfs(1);  dfs1(1);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        printf("%d\n",dis[x]+dis[y]-2*dis[lca(x,y)]);
    }
    return 0;
}