【BZOJ1832】 聚会

                                             1832: [AHOI2008]聚会

                                                                 Time Limit: 10 Sec  Memory Limit: 64 MB
                                                                           Submit: 1826  Solved: 780

Description

Y岛风景美丽宜人,气候温和,物产丰富。Y岛上有N个城市,有N-1条城市间的道路连接着它们。每一条道路都连接某两个城市。幸运的是,小可可通过这些道路可以走遍Y岛的所有城市。神奇的是,乘车经过每条道路所需要的费用都是一样的。小可可,小卡卡和小YY经常想聚会,每次聚会,他们都会选择一个城市,使得3个人到达这个城市的总费用最小。 由于他们计划中还会有很多次聚会,每次都选择一个地点是很烦人的事情,所以他们决定把这件事情交给你来完成。他们会提供给你地图以及若干次聚会前他们所处的位置,希望你为他们的每一次聚会选择一个合适的地点。

Input

第一行两个正整数,N和M。分别表示城市个数和聚会次数。后面有N-1行,每行用两个正整数A和B表示编号为A和编号为B的城市之间有一条路。城市的编号是从1到N的。再后面有M行,每行用三个正整数表示一次聚会的情况:小可可所在的城市编号,小卡卡所在的城市编号以及小YY所在的城市编号。

Output

一共有M行,每行两个数Pos和Cost,用一个空格隔开。表示第i次聚会的地点选择在编号为Pos的城市,总共的费用是经过Cost条道路所花费的费用。

Sample Input

6 4
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6
 

Sample Output

5 2
2 5
4 1
6 0

数据范围:
100%的数据中,N<=500000,M<=500000。
40%的数据中N<=2000,M<=2000。
 

解析:

       树上涉及到距离最短的问题多少跟LCA都有点关系,所以直接找三个点两两的LCA更新答案就行了。

代码:

#include <bits/stdc++.h>
using namespace std;
 
const int Max=500010;
int n,m,size,s,ans;
int depth[Max],first[Max],f[Max][20],vis[Max];
struct shu{int to,next;};
shu edge[Max<<1];
 
inline int get_int()
{
    int x=0,f=1;
    char c;
    for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
    if(c=='-') {f=-1;c=getchar();}
    for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
    return x*f;
}
 
inline void build(int x,int y)
{
    edge[++s].next=first[x];
    first[x]=s;
    edge[s].to=y;
}
 
inline void pre()
{
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
      int point = q.front();
      vis[point] = 1;
      q.pop();
      for(int u=first[point];u;u=edge[u].next)
      {
        int to=edge[u].to;
        if(vis[to]) continue;
        depth[to] = depth[point] + 1;
        f[to][0] = point;
        q.push(to);
      }
    }
}
 
inline int LCA(int x,int y)
{
    if(depth[x] < depth[y]) swap(x,y);
    int len = depth[x] - depth[y];
    for(int i=16;i>=0;i--)
      if(len >= 1<<i) len-=1<<i,x=f[x][i];
    if(x==y) return x;
    for(int i=16;i>=0;i--)
      if(f[x][i] != f[y][i]) x = f[x][i],y = f[y][i];
    return f[x][0];
}
 
inline int mn(int x,int y){return x > y ? y : x;}
 
int main()
{
    n=get_int(),m=get_int();
    for(int i=1;i<=n-1;i++)
    {
      int x=get_int(),y=get_int();
      build(x,y),build(y,x);
    }
 
    pre();
    for(int i=1;i<=16;i++)
      for(int j=1;j<=n;j++)
        if(f[j][i-1]) f[j][i] = f[f[j][i-1]][i-1];
 
    for(int i=1;i<=m;i++)
    {
      int x=get_int(),y=get_int(),z=get_int();
      if(x== y && y==z) cout<<x<<" 0\n";
      int fa1 = LCA(x,y),fa2 = LCA(x,z),fa3 = LCA(y,z);
      int len1 = depth[x] + depth[y] - 2*depth[fa1] + depth[fa1] + depth[z] - 2*depth[LCA(fa1,z)];
      int len2 = depth[x] + depth[z] - 2*depth[fa2] + depth[fa2] + depth[y] - 2*depth[LCA(fa2,y)];
      int len3 = depth[z] + depth[y] - 2*depth[fa3] + depth[fa3] + depth[x] - 2*depth[LCA(fa3,x)];
      int minn = mn(len1,mn(len2,len3));
      if(minn == len1) cout<<fa1<<" "<<len1<<"\n";
      else if(minn == len2) cout<<fa2<<" "<<len2<<"\n";
      else cout<<fa3<<" "<<len3<<"\n";
    }
 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38083668/article/details/81301942