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. 别人走的路--uap

    首先,我先谈谈我个人的经历,我今年34岁了,做了10多年的ERP实施顾问,大学刚毕业的时候是做ERP软件开发的,后来转岗做了实施顾问.根据我的个人经验,我给你几点建议.1.既然是很大的公司,那么ERP ...

  2. HTML5 FileReader读取Blob对象API详解

    使用FileReader对象,web应用程序可以异步的读取存储在用户计算机上的文件(或者原始数据缓冲)内容,可以使用File对象或者Blob对象来指定所要读取的文件或数据.其中File对象可以是来自用 ...

  3. SGU 175.Encoding

    Solution: 简单题. 答案初始化为1. 从给定的n,q往下推出新的n和q,如果q是在右半边,答案加上 n-n/2. 一直到推到n==1. code: #include <iostream ...

  4. gSoap实现ONVIF中xsd__anyType到具体结构类型的转换

    上一篇文章已经粗略计划要讨论gsoap关于序列化/解析编程. 本文则阐述一下关于gsoap生成代码的一些重要特征方法及使用.如题,下我们从ONVIF生成的C码中,挑选简单的一个类型来试验一下与xsd_ ...

  5. chop 与 chomp 的对比

    chop       截去最后一个字符,无论是什么字符 chomp   截去末尾的分隔符(\n),行分隔符由$/决定 $a="ab\n\n\n"; #截去多个空行. $/=&quo ...

  6. Android各种访问权限Permission详解

    原文:http://jingyan.baidu.com/article/afd8f4de4688af34e386e976.html 在Android的设计中,资源的访问或者网络连接,要得到这些服务都需 ...

  7. windows7任务栏上的图标修复

    Technorati 标记: 疑难杂症   今天,我在使用Windows 7的时候,因为操作一些系统文件,发现桌面下角的个别正在运行的图标不见了,但是,我们如果再打开一个新程序,又会提醒你已经在运行了 ...

  8. block 和delegate的用法

    //block 和delegate的用法 设置代理 #import <UIKit/UIKit.h> typedef void (^ASIHeadersBlock)(NSString *my ...

  9. JQuery重要知识点

    jQuery基本选择器----包括ID选择器,标签选择器,类选择器,通配选择器和组选择器5种 a. ID选择器: $("#id") b. 标签选择器:$("element ...

  10. nginx+uwsgi+WSGI applications

    uwsgi一个专业的部署运用的工具,不仅能够部署Python运用,还能够部署其他运用比如Perl,Ruby等 uWSGI 安装: pip install uwsgi WSGI application( ...