1787: [Ahoi2008]Meet 紧急集合

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 1841  Solved: 857
[Submit][Status][Discuss]

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

Source

【思路】

Lca。

求出三点之间的lca,会出现两个点相同的情况,集合点为另外一个点。

【代码】

 /**************************************************************
Problem: 1787
User: hahalidaxin2
Language: C++
Result: Accepted
Time:3716 ms
Memory:65216 kb
****************************************************************/ #include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
using namespace std; const int maxn = +;
const int maxd = ; struct Edge{ int u,v;
};
vector<int> G[maxn];
vector<Edge> es; int d[maxn];
int p[maxn][maxd]; void addedge(int u,int v) {
es.push_back((Edge){u,v});
int m=es.size(); G[u].push_back(m-);
}
void dfs(int u,int fa) {
for(int i=;i<=maxd;i++) { //构造倍增数组
if(d[u]<(<<i)) break;
p[u][i]=p[p[u][i-]][i-];
}
for(int i=;i<G[u].size();i++) {
Edge e=es[G[u][i]]; int v=e.v;
if(v!=fa)
d[v]=d[u]+ , p[v][]=u , dfs(v,u);
}
}
int lca(int u,int v) {
if(d[v]>d[u]) swap(u,v);
int dep=d[u]-d[v];
for(int i=;i<maxd;i++)
if((<<i)&dep) u=p[u][i];
if(u==v) return u;
for(int i=maxd-;i>=;i--)
if(p[u][i]!=p[v][i])
u=p[u][i] , v=p[v][i];
return p[u][];
}
int dist(int x,int y) { return d[x]+d[y]-(d[lca(x,y)]<<); }
int n,m; void read(int& x) {
char c=getchar();
while(!isdigit(c)) c=getchar();
x=;
while(isdigit(c))
x=x*+c-'' , c=getchar();
}
int main() {
read(n),read(m);
int u,v;
for(int i=;i<n-;i++) {
read(u),read(v);
addedge(u,v); addedge(v,u);
}
dfs(n>>,-);
int a,b,c,lab,lac,lbc,s;
while(m--) {
read(a),read(b),read(c);
lab=lca(a,b),lac=lca(a,c),lbc=lca(b,c);
if(lab==lac) s=lbc;
else if(lab==lbc) s=lac;
else s=lab;
printf("%d %d\n",s,dist(a,s)+dist(b,s)+dist(c,s));
}
return ;
}

bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)的更多相关文章

  1. bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会

    bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...

  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

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

  5. 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 ...

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

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

  7. bzoj1787 [Ahoi2008]Meet 紧急集合

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

  8. [Ahoi2008]Meet 紧急集合

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

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

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

随机推荐

  1. PHP 中xampp不能启动服务器的问题

    有时候别人电脑上面的XAMPP,你把安装文件拷贝下来后,会发现,自己的电脑上用不了 这个时候有很多种情况 1. 关闭你自己电脑上有可能暂用80端口的程序 2.D:\xampp\apache\conf\ ...

  2. Sql2008中使用DataTable作为存储过程的参数

    使用DataTable作为存储过程的参数   最近工作中写了几个存储过 程,需要向存储过程中传递字符串,因为SQL Server 2000中没有内置类似于 split 的函数,只好自己处理,将前台数据 ...

  3. JavaScript Comparison and Logical Operators

    Ref:http://www.w3schools.com/js/js_comparisons.asp var r = 1; var result = r || 2; console.log(resul ...

  4. php验证是否为手机端还是PC

    <?php $forasp = strtolower($_SERVER['HTTP_USER_AGENT']); if(strpos($forasp,'mobile')==true) { ech ...

  5. spring jdbctemplate调用procedure(返回游标)

    package cn.com.git.htsc.uac.core.repository.report; import cn.com.git.htsc.uac.core.api.dto.report.R ...

  6. Java循环性能随笔

    for iterator做迭代循环性能最好 然后是foreach 然后是提前声明好变量的for循环 最后是每次都要计算集合size的for       package test;   import j ...

  7. SGU 145.Strange People(无环K短路)

    时间:0.25s空间:4m 题意: 其实就是求无环第K短路. 输入: 给出n,m,k,分别代表,n个点,m条边,第k长路. 接下来m行,三个整数x,y,z,分别代表x,y之间有条费用为x的双向路.保证 ...

  8. vijos P1055奶牛浴场&& Winter Camp2002

    这道题是我在寒假的模拟赛里碰到的,现在想起来仍觉得余味无穷.题目大意大致如下:给你一个矩形并在其中划出一个最大的子矩形,当然,在这个矩形里有些地方是取不到的,也就是说我们划的这个子矩形不能包含这些点( ...

  9. pycharm常用快捷键与设置

    pycharm高频率使用的快捷键 Ctrl+Shift+F10 运行当前的页面 Ctrl + / 注释(取消注释)选择的行 Ctrl+Shift+F 高级查找 Shift + Enter 开始新行 T ...

  10. 《python学习手册》之一——程序运行

    Python解释器执行Python代码时候,大概经历如下几个阶段:(1) 加载代码文件 (2)翻译成AST (3)生成bytecode(.pyc文件,与编译的python版本有关).可以使用pytho ...