题目传送门

LCA,对于每一个(x,y,z),两两求LCA得最优解或求出LCA不同于其他两组的那个为最优解。

code:

/**************************************************************
    Problem: 1787
    User: yekehe
    Language: C++
    Result: Accepted
    Time:5212 ms
    Memory:51604 kb
****************************************************************/
 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
 
int read()
{
    char c;while(c=getchar(),c<''||c>'');
    int x=c-'';while(c=getchar(),c>=''&&c<='')x=(x<<)+(x<<)+c-'';
    return x;
}
 
const int MAXN=;
int N,M;
int head[MAXN],to[MAXN<<],nxt[MAXN<<],cnt;
void add(int x,int y)
{
    to[cnt]=y;
    nxt[cnt]=head[x];
    head[x]=cnt;
    cnt++;
}
 
int D[MAXN];
int f[][MAXN];
void dfs(int now,int deep,int fa)
{
    D[now]=deep;
    f[][now]=fa;
        for(int i=head[now];i!=-;i=nxt[i])
            if(!D[to[i]])
                dfs(to[i],deep+,now);
    return ;
}
int Me;
int get(int x,int y)
{
    if(D[x]<D[y])swap(x,y);
    for(int i=;i>=;i--)
        if(D[f[i][x]]>=D[y])x=f[i][x];
    if(x==y)return x;
    for(int i=;i>=;i--)
        if(f[i][x]!=f[i][y])
            x=f[i][x],y=f[i][y];
    return f[][x];
}
 
int solve(int x,int y)
{
    return D[x]+D[y]-(D[get(x,y)]<<);
}
int main(){
    memset(head,-,sizeof head);
    memset(nxt,-,sizeof nxt);
    N=read(),M=read();
    register int i,j;
        for(i=;i<N;i++){
            int x=read(),y=read();
            add(x,y),add(y,x);
        }
    dfs(,,);
        for(i=;i<=;i++)
            for(j=;j<=N;j++)
                f[i][j]=f[i-][f[i-][j]];
        for(i=;i<=M;i++){
            int x=read(),y=read(),z=read();
            int a=get(x,y),b=get(y,z),c=get(x,z);
            int dist=;
            if(a==b)dist=c;else
            if(b==c)dist=a;else
            dist=b;
            printf("%d %d\n",dist,solve(x,dist)+solve(y,dist)+solve(z,dist));
        }
    return ;
}

BZOJ1787_紧急集合_KEY的更多相关文章

  1. bzoj1787 [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 2272  Solved: 1029 [Submi ...

  2. 【BZOJ-1787&1832】Meet紧急集合&聚会 倍增LCA

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 2259  Solved: 1023[Submit] ...

  3. bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1841  Solved: 857[Submit][ ...

  4. BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )

    这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...

  5. 1787: [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1482  Solved: 652[Submit][ ...

  6. [AHOI2008]紧急集合 / 聚会

    题目描述 欢乐岛上有个非常好玩的游戏,叫做“紧急集合”.在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等待点,通过道路从一个点到另一个点要 ...

  7. [AHOI2008] 紧急集合

    Description 欢乐岛上有个非常好玩的游戏,叫做"紧急集合".在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等 ...

  8. P4281 [AHOI2008]紧急集合 / 聚会

    P4281 [AHOI2008]紧急集合 / 聚会 lca 题意:求3个点的lca,以及3个点与lca的距离之和. 性质:设点q1,q2,q3 两点之间的lca t1=lca(q1,q2) t2=lc ...

  9. 【BZOJ1787】[Ahoi2008]Meet 紧急集合 LCA

    [BZOJ1787][Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 ...

随机推荐

  1. Linux 挂载

    千万不要挂载到 根目录下 也不要用 umount -fl  会死的 fdisk -l 看 能挂载的是哪个盘 格式化 mkfs.ext4 /dev/vde 创建一个文件 mkdir /testmnt 卸 ...

  2. [WinCE] Win CE 屏幕截图

    VS2005自带了这个工具,这个工具的名称为:“远程放大”,其路径为:"C:\Program Files\CE Remote Tools\5.01\bin\cczoom.exe" ...

  3. Asp.net单点登录解决方案

    原文出处:http://www.cnblogs.com/wu-jian 主站:Passport集中验证服务器,DEMO中为:http://www.passport.com/ 分站:http://www ...

  4. umount nfs文件系统 显示 umount.nfs: device is busy

    网上的方法一般都是 fuser -m  /nfs 查出进程号,然后杀死进程号,或者fuser -km /nfs直接杀死,我试了下都不行 解决方法: 对于nfs文件系统来说,umount -l /nfs ...

  5. Linux架构之简述企业网站简述

    简述企业网站 用户  --> 负载均衡服务器(Nginx)  ->根据扩展名访问不同的服务区 ->访问数据库 ->返回用户          静态服务器&&动态 ...

  6. September 09th 2017 Week 36th Saturday

    Don't wait to be lonely, to recognize the value of a friend. 不要等到孤独了,才明白朋友的价值. Don't wait to be left ...

  7. ZT pthread_cleanup_push()/pthread_cleanup_pop()的详解

    pthread_cleanup_push()/pthread_cleanup_pop()的详解 分类: Linux 2010-09-28 16:02 1271人阅读 评论(1) 收藏 举报 async ...

  8. C++ Circle

    作业链接 https://github.com/How-Come/object-oriented/tree/master/Circle

  9. Matlab面向对象编程基础

    DeepLab是一款基于Matlab面向对象编程的深度学习工具箱,所以了解Matlab面向对象编程的特点是必要的.笔者在做Matlab面向对象编程的时候发现无论是互联网上还是书店里卖的各式Matlab ...

  10. zookeeper环境搭建.md

    zookeeper 简介 ZooKeeper是一个为分布式应用所设计的分布的.开源的协调服务,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,简化分布式应用协调及其管理的难度,提供高性能的分布 ...