[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,如果为同一点,显然该点为集合点,如果有一个x不同于另外两个,则三者在一条链上,x为中间的一个,故x为集合点,如果三者互不相同,集合点可以是三者到共同lca路径上任何一点,或者干脆将三者共同lca作为集合点就行了。

代码:

program Meet;
type
point=^node;
node=record
x:longint; next:point;
end;
var
a:array[..]of point;
f:array[..,..]of longint;
deep,s:array[..]of longint;
n,i,m,x,y,t,z,v1,v2,v3,ans:longint;
procedure add(x,y:longint);
var p:point;
begin
new(p); p^.x:=y; p^.next:=a[x]; a[x]:=p;
end;
procedure dfs(x,k:longint);
var p:point;
begin
new(p); p:=a[x]; f[,x]:=k; deep[x]:=deep[k]+;
while p<>nil do
begin
if p^.x<>k then dfs(p^.x,x);p:=p^.next;
end;
end;
procedure work;
var i,j:longint;
begin
for i:= to trunc(ln(n)/ln())- do
for j:= to n do
f[i+,j]:=f[i,f[i,j]];
end;
function lca(x,y:longint):longint;
var i,t:longint;
begin
if deep[x]<deep[y] then begin t:=x; x:=y; y:=t; end;
for i:= to trunc(ln(n)/ln()) do
if ((deep[x]-deep[y])>>i) and = then x:=f[i,x];
if x=y then exit(x);
for i:=trunc(ln(n)/ln()) downto do
if f[i,x]<>f[i,y] then
begin x:=f[i,x]; y:=f[i,y]; end;
exit(f[,x]);
end;
begin
assign(input,'Meet.in');
reset(input);
assign(output,'Meet.out');
rewrite(output);
readln(n,m);
for i:= to n- do
begin
readln(x,y); add(x,y); add(y,x);
end;
deep[]:=;
dfs(,); work;
for i:= to m do
begin
readln(x,y,z);
v1:=lca(x,y); v2:=lca(x,z); v3:=lca(z,y);
if (v1=v2)and(v2=v3) then t:=v1
else if v1=v2 then t:=v3
else if v2=v3 then t:=v1
else t:=v2;
v1:=lca(x,t); v2:=lca(y,t); v3:=lca(z,t);
ans:=deep[x]+deep[y]+deep[z]+deep[t]*-*(deep[v1]+deep[v2]+deep[v3]);
writeln(t,' ',ans);
end;
close(input); close(output);
end.

BZOJ 1787: [Ahoi2008]Meet 紧急集合(lca+贪心)的更多相关文章

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

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

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

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

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

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

  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. javaweb基础(32)_jdbc学习入门

    一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...

  2. highcharts与ajax的应用

    整理一份完整的例子,以供参考: <1>页面chart.html: <span style="font-size:14px;"><!DOCTYPE HT ...

  3. Express框架 --router/app.use

    翻看去年自己记录的印象笔记,准备把笔记上的一些内容也同时更新到博客上,方便自己查看. 1.app.use和app.get的区别及解析 app.use(path,callback)中的callback既 ...

  4. ORTP-0.27.0移植

    注意: a. 对于0.27一下版本的ORTP的交叉编译则没有一下依赖库 b. 交叉编译工具链是: arm-linux-gnueabihf-gcc-4.9.1 (4.9版本一下的编译bctoolbox出 ...

  5. 1207: [HNOI2004]打鼹鼠

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4154  Solved: 1997[Submit][Status][Discuss] Descript ...

  6. django-redis缓存记录

    对于站点缓存,我们使用redis这款key-value数据库.Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径.Redis的数据类型都是基于基本数据结构的 ...

  7. 转:maven国内镜像(maven下载慢的解决方法)

    http://www.cnblogs.com/xiongxx/p/6057558.html https://jingyan.baidu.com/article/1876c8524ee0d0890a13 ...

  8. 读书笔记-JavaScript面向对象编程(二)

    第5章 原型 5.1 原型属性(所有函数拥有一个prototype属性,默认为空对象) 5.1.1 利用原型添加方法和属性 function Gadget(name,color){ this.name ...

  9. Python全栈学习:匿名函数使用规范

    匿名函数,当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x) ...

  10. Python学习笔记:math模块(数学),random模块(随机数)

    math模块 math模块用于数学意义上的一些计算,常用的方法有: math.pi:PI的值(3.141592653589793). math.floor(x):返回一个小于等于x的最大整数(浮点类型 ...