1787: [Ahoi2008]Meet 紧急集合

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,其中有两个相同,答案则为另一个,参考HZWer.

///

#include<bits/stdc++.h>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=;
#define mod 1000000007
#define inf 1000000007
int n,m,head[N],t,vis[N],deep[N],fa[N][];
struct ss {
int to,next;
}e[N*];
void add(int u,int v) {
e[t].next=head[u];e[t].to=v;head[u]=t++;
}
void init() {
t=;mem(head);mem(vis);mem(fa);mem(deep);
}
void dfs(int x) {
vis[x]=;
for (int i=; i<= ;i++) {
if(deep[x]<(<<i)) break;
fa[x][i] = fa[fa[x][i-]][i-];
}
for (int i=head[x];i;i=e[i].next) {
if(vis[e[i].to]) continue;
deep[e[i].to]=deep[x]+;
fa[e[i].to][]=x;
dfs(e[i].to);
}
}
int RMQ_LCA(int x,int y) {
if(deep[x]<deep[y]) swap(x,y);
int d=deep[x]-deep[y];
for (int i=; i<= ;i++)
if((<<i)&d) x=fa[x][i];
for (int i=; i>= ;i--) {
if(fa[x][i]!=fa[y][i]) {
x=fa[x][i];y=fa[y][i];
}
}
if(x==y) return x;
else return fa[x][];
}
int Dis_LCA(int x,int y) {
int LCA= RMQ_LCA(x,y);
return (deep[x]+deep[y]-*deep[LCA]);
}
int cal(int x,int y,int z) {
int A;
int L1=RMQ_LCA(x,y);
int L2=RMQ_LCA(y,z);
int L3=RMQ_LCA(x,z);
if(L1==L2) {
A=L3;
}
else if(L2==L3) {
A=L1;
}
else A=L2;
int anss=Dis_LCA(x,A)+Dis_LCA(y,A)+Dis_LCA(z,A);
printf("%d %d\n",A,anss);
//cout<<A<<" "<<anss<<endl;
}
int main() {
scanf("%d%d",&n,&m);
init();
for(int i=;i<n;i++) {
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs();
for(int i=;i<=m;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
cal(a,b,c);
} return ;
}

代码

BZOJ 1787: [Ahoi2008]Meet 紧急集合 LCA的更多相关文章

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

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

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

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

  3. bzoj 1787: [Ahoi2008]Meet 紧急集合

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

  4. BZOJ 1787: [Ahoi2008]Meet 紧急集合(lca+贪心)

    [Ahoi2008]Meet 紧急集合 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 ...

  5. bzoj 1787: [Ahoi2008]Meet 紧急集合【树链剖分lca】

    对于三个点求最小路径长度和,答案肯定在某两个点的lca上,因为如果把集合点定在公共lca上,一定有两个点汇合后再一起上到lca,这样显然不如让剩下的那个点下来 这个lca可能是深度最深的--但是我懒得 ...

  6. BZOJ——1787: [Ahoi2008]Meet 紧急集合

    http://www.lydsy.com/JudgeOnline/problem.php?id=1787 题目描述 输入 输出 样例输入 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 ...

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

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

  8. 【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 ...

  9. bzoj 1787 && bzoj 1832: [Ahoi2008]Meet 紧急集合(倍增LCA)算法竞赛进阶指南

    题目描述 原题连接 Y岛风景美丽宜人,气候温和,物产丰富. Y岛上有N个城市(编号\(1,2,-,N\)),有\(N-1\)条城市间的道路连接着它们. 每一条道路都连接某两个城市. 幸运的是,小可可通 ...

随机推荐

  1. [ SCOI 2008 ] 着色方案

    \(\\\) \(Description\) 给出\(K\)种颜料各自的个数\(C_i\),每一个颜料只够涂一个格子,求将颜料用完,涂一排格子,每个格子只能涂一次的条件下,相邻两个格子的颜色互不相同的 ...

  2. JavaScript 封装插件学习笔记(一)

    此篇只是笔记,在借鉴.参考.模仿的过程,可能不完整,请多指教! 定义插件名称要注意命名冲突,防止全局污染. 1.第一种Javascript对象命名:(Javascript语言是“先解析,后运行”,解析 ...

  3. 【GAN学习笔记】对抗式生成网络入门

    今天观看学习了一下台大李宏毅所讲授的 <Introduction of Generative Adversarial Network (GAN)>,对GAN有了一个初步的了解. GAN的基 ...

  4. git——merge和rebase的区别

    参考http://www.jianshu.com/p/129e721adc6e 我在公司里看到其他同事都使用git pull --rebase拉取远程代码,而我总是用git pull,也有同事和我说过 ...

  5. Monkey测试总结

    Monkey测试总结 1.什么是Monkey Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等), 实 ...

  6. http通信流程

    Host https://www.charlesproxy.com Path / Notes SSL Proxying not enabled for this host. Enable in the ...

  7. excel求1加到100的和

    强大的数组公式

  8. PHP 魔术方法浅谈

    php中把以两个下划线(__)开头的方法称之为魔术方法.魔术方法包括: __construct()    类的构造方法  构建方法时被调用 __destruct()      类的析构方法  明确销毁 ...

  9. tomcat8安装及配置

    首先是解压版的安装.很简单,直接解压到要安装的位置就OK了. 2.启动 bin目录下,执行startup.bat文件 3.浏览器中打开地址http://localhost:8080/

  10. php观察折模式

    <?php class Paper{ private $_observers = array(); public function register($sub){ $this->_obse ...