bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)
1787: [Ahoi2008]Meet 紧急集合
Time Limit: 20 Sec Memory Limit: 162 MB
Submit: 1841 Solved: 857
[Submit][Status][Discuss]
Description

Input

Output

Sample Input
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6
Sample Output
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]聚会)的更多相关文章
- bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会
bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )
这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...
- 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 ...
- 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 ...
- 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 ...
- 1787: [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1482 Solved: 652[Submit][ ...
- bzoj1787 [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ...
- [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBhttp://www.lydsy.com/JudgeOnline/p ...
- 【bzoj1787】[Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 2466 Solved: 1117[Submit] ...
随机推荐
- 彻底删除mysql的方法(有隐藏文件)
1.建议使用360进行卸载,可以彻底卸载软件 2.360会提醒删除注册表 3.这个隐藏文件要删除掉 在 C:\Documents and Settings\ 路径下搜索 MySQL 文件夹(默认隐藏的 ...
- 项目报错,tomcat中引起
1.项目报错,但发现工程并没有错.此刻错误应该定位如下,即工程里面引用的jar可能有错,可能是路劲变了....
- 删除我的电脑中360随身WiFi云U盘的图标
可通过删除注册表的方法 运行-regedit 找到这个项 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\My ...
- 借用Toad 生成表空间的使用量图示
图示产生方法 图示(tablespace uage)如下
- 欢迎使用 Markdown 编辑器写博客
本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和图片上传 LaTex数学公式 UML ...
- 在Mac OS上搭建本地服务器
我们在做网络编程的时候一般是需要有网络环境的,这样可以边写边测试达到很高的效率.但有些时候我们由于很多原因我们的电脑无法连接到网络,这时就会感觉很不自在,所以今天在这里教大家怎么用自己电脑作服务器. ...
- (whh仅供自己参考)进行ip网络请求的步骤
这个过程大致是这个样子: 1 添加通知 2 发送网络请求 里边有一个发送通知的操作 3 执行发送通知的具体操作 代码如下: 1 在VC添加通知 [[NSNotificationCenter defau ...
- NSArray 跟 NSMutableArray 使用 区别
NSArray 可变数组 一.NSArray是静态数组,创建后数组内容及长度不能再修改. 实例: //用arrayWithObjects初始化一个不可变的数组对象. //初始化的值之间使用逗号分开,以 ...
- Jquery 点击空白处消失
$(document).bind("click", function (e){ if ( $((e.target || e.srcElement)).closest("# ...
- LeetCode OJ -Happy Number
题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...