[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. POJ 2718 Smallest Difference(dfs,剪枝)

    枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...

  2. java算法面试题:排序都有哪几种方法?请列举。用JAVA实现一个快速排序。选择冒泡快速集合至少4种方法排序

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...

  3. Dungeon Master POJ - 2251 (搜索)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48605   Accepted: 18339 ...

  4. pycahrm git配置笔记

    1. 在file - setting - plugins 中查看是否有github插件, 此处是用于处理插件位置

  5. 五、Linux 远程登录

    Linux 远程登录 Linux一般作为服务器使用,而服务器一般放在机房,你不可能在机房操作你的Linux服务器. 这时我们就需要远程登录到Linux服务器来管理维护系统. Linux系统中是通过ss ...

  6. java设计模式1--单例模式

    1:单例模式简介 单例模式是一种常用的软件设计模式,它确保某个类只有一个实例,而且自行实例化并向整个系统提供唯一的实例.总而言之就是在系统中只会存在一个对象,其中的数据是共享的 特点: 单例类只能有一 ...

  7. Yii 2.x html 代码压缩

    <?php namespace Pangu\web; use yii\base\Component; /** * html格式响应内容格式化 * @author zhouzhian * */ c ...

  8. DeepFaceLab进阶(4):通过Colab免费使用Tesla K80 跑模型!

    当学会了换脸软件DeepFaceLab基本使用,各种参数配置,各有优化技能之后.唯一约束你的可能是电脑配置. CPU能跑,但是慢到怀疑人生,低配模型都得跑一周 低配显卡,显存不够,H128 根本就跑不 ...

  9. 迭代器Iterator与语法糖for-each

    一.为什么需要迭代器 设计模式迭代器 迭代器作用于集合,是用来遍历集合元素的对象.迭代器迭代器不是Java独有的,大部分高级语言都提供了迭代器来遍历集合.实际上,迭代器是一种设计模式: 迭代器模式提供 ...

  10. 洛谷 P3740 [HAOI2014]贴海报

    题目描述 Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙. 张贴规则如下: electo ...