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] ...
随机推荐
- window.showModalDialog()复制内容
ShowModalDialog 打开的 页面上加入个 <span id="mySpan" name="mySpan" contentEditable=&q ...
- SQL觸發器聯級刪除
Create TRIGGER [dbo].[trigInstructionsDelete] ON dbo.Instructions instead OF DELETE AS BEGIN DECLARE ...
- Oracle主键自动生成_表and存储过程
-- Create table create table T_EB_SYS_DN_SEQUENCE_CONFIG ( sequence_id VARCHAR2(36) default sys_guid ...
- 由App的启动说起(转)
The two most important days in your life are the day you are born and the day you find out why. -- M ...
- VisualStudio2013内置SQLServer入门(二)--增删改查
前一篇 http://www.cnblogs.com/qixi233/p/4766451.html 这篇是sqlserver的操作 界面比较走心哈哈哈,将就着看,主要就是下面增删改查四个btn 对于s ...
- SpringMVC4+thymeleaf3的一个简单实例(篇一:基本环境)
首语:用SpringMVC和thymeleaf实现一个简单的应用,包括基本环境搭建,SpringMVC4和thymeleaf3的整合,页面参数的获取,页面参数验证,以及用MySQL保存数据.我会把步骤 ...
- system.getProperties()
Properties props=System.getProperties(); //系统属性 System.out.println("Java的运行环境版本:"+prop ...
- 未能解析目标框架“.NETFramework,Version=v4.0”的 mscorlib 错误的解决办法
查看项目属性,发现该项目的目标框架是.NET Framework 4 Client Profile ,而被引用的程序集的目标框架是.NET Framework 4,将该项目的目标框架修改成.NET F ...
- 控制寄存器 CR*
控制寄存器(CR0-CR3)用于控制和确定处理器的操作模式以及当前执行任务的特性,如图4-3所示.CR0中含有控制处理器操作模式和状态的系统控制标志:CR1保留不用:CR2含有导致页错误的线性地址:C ...
- CSS3学习--dispaly:inline和float:left两者区别
1.display:inline: 任何不是块级元素的可见元素都是内联元素.其表现的特性是“行布局”形式!(行布局:其表现形式始终以行进行显示) 2.float:left:指定元素脱离普通的文档流 ...