1787: [Ahoi2008]Meet 紧急集合

Time Limit: 20 Sec Memory Limit: 162 MB
Submit: 999999999 Solved: 999999999

Description

Input

Output

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

HINT

三个点分别组合,算出LCA,其中必有两个是一样的;
然后既然得知了LCA是哪两个点相同,可以唯一确定更深的另一个LCA,两个点的移动必定不如一个点的移动,由此得解

#include <cstdio>
#include <algorithm>
using namespace std;const int N = 500010;
const int M = N + N;
const int P = 16;int n, m;
int head[N], dest[M], last[M], dast[M], etot;
int dep[N], anc[N][P+1];void adde( int u, int v ) {etot++;dest[etot] = v;last[etot] = head[u];head[u] = etot;
}
void dfs( int u, int f ) {anc[u][0] = f;for( int p = 1; p <= P; p++ )anc[u][p] = anc[anc[u][p-1]][p-1];for( int t = head[u]; t; t = last[t] ) {int v = dest[t];if( v == f ) continue;dep[v] = dep[u] + 1;dfs( v, u );}
}
int lca( int u, int v ) {if( dep[u] < dep[v] ) swap(u,v);int t = dep[u] - dep[v];for( int p = 0; t; t>>=1,p++ )if( t & 1 ) u = anc[u][p];if( u == v ) return u;for( int p = P; p >= 0; p-- ) if( anc[u][p] != anc[v][p] )u = anc[u][p], v = anc[v][p];return anc[u][0];
}
int main() {scanf( "%d%d", &n, &m );int u, v, w;for( int i = 1; i < n; i++ ) {scanf( "%d%d", &u, &v );adde( u, v );adde( v, u );}dep[1] = 1;dfs( 1, 1 );while(m--){scanf( "%d%d%d", &u, &v, &w );int s=dep[u]+dep[v]+dep[w];int t=lca(u,v);int q=lca(v,w);int r=lca(u,w);if( t == q ){w = lca( r, v );printf( "%d %d\n", r, s-dep[w]*2-dep[r]);}else if( t == r ){w=lca( q, u );printf("%d %d\n",q,s-dep[w]*2-dep[q]);}else if( r == q ){w=lca( t, w );printf("%d %d\n",t,s-dep[w]*2-dep[t]);}}return 0;
}

[bzoj1787][Ahoi2008]Meet 紧急集合 倍增LCA相关推荐

  1. [Ahoi2008] Meet 紧急集合 (LCA+倍增+rmq-st)

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

  2. bzoj1787 [Ahoi2008]Meet 紧急集合

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

  3. [BZOJ1787][Ahoi2008]Meet 紧急集合

    原题地址 倍增LCA复习题- AC code: #include <cstdio> #include <vector> using namespace std; const i ...

  4. BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA

    欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ...

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

    [传送门:BZOJ1787] 简要题意: 给出有n个点的图,n-1条无向边,保证任意两点之间能互相到达,每条边的权值为1,给出m个询问,每个询问输入x,y,z,求出一个点使得三个点到这个点的距离和最短 ...

  6. [BZOJ1787][Ahoi2008]Meet 紧急集合[BZOJ1832][AHOI2008]聚会

    传送门 http://www.lydsy.com/JudgeOnline/problem.php?id=1787 http://www.lydsy.com/JudgeOnline/problem.ph ...

  7. BZOJ 1787 [Ahoi2008]Meet 紧急集合——LCA

    1787: [Ahoi2008]Meet 紧急集合 题目传送门 解题思路 本题是裸的LCA,特殊就在是三个点的LCA,然而并没有什么关系. 三个节点两两求LCA,将会有两个一样的公共祖先,剩下的一个就 ...

  8. [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB http://www.lydsy.com/JudgeOnline/ ...

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

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

最新文章

  1. 终于把论文Check完了
  2. Vuebnb:一个用vue.js和Laravel构建的全栈应用
  3. java s字符_java 字符集s
  4. acer p245 linux换win7,宏碁Acer笔记本预装Win8改Win7设置教程
  5. 【AI-1000问】机器学习和模式识别是什么关系?
  6. HTTP的请求头标签 If-Modified-Since与Last-Modified 相等就是200新的请求 不等就是304没有变化
  7. oracle分区和锁的难,oracle使用三(锁和表分区)
  8. nsga2算法_用遗传算法进行容量管理 让虚拟机放置策略更优
  9. 旷视周而进:人脸识别简史与距离对齐技术的应用
  10. Android助手 V23.34,Android助手
  11. oracle:用户购买平台案例分析与优化
  12. UI设计不能忽略的字体设计素材
  13. Rust : 递归、效率与替代、溢出
  14. android 开源fc模拟器_用 Go 撸了一个 NES/FC/红白机模拟器——GoNES
  15. Python使用pyodbc访问数据库操作方法详解
  16. 2013年国考行测错题集(省级)
  17. 网页设计配色应用——色彩的调和
  18. 腾讯北大合作的稀疏大模型训练加速方案HET入选国际顶会VLDB
  19. 解决Mac电脑连不上wifi的问题
  20. momi's home

热门文章

  1. 浅析MOS管开关速度影响因素-KIA MOS管
  2. 全网疯传!Java利用TCP协议实现客户端与服务器通信【附通信源码
  3. Linux SCP跨服务器传输文件
  4. Android 网络请求、网络状态及各种权限判断
  5. Python3:pygame模块的简单的使用(加载图片并让图片动起来)
  6. NextCloud集成OnlyOffice功能
  7. Python爬取微信好友的信息
  8. 使用建造者模式(Builder Pattern) 设计Excel导出场景,附源码
  9. 我是怎么解决微信封了我们域名的
  10. 沙盘ROS机器人调试使用记录