1787: [Ahoi2008]Meet 紧急集合

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 1482  Solved: 652
[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

Day1

题解:很明显,两个人的时候,最优方案即为两者的LCA;而三个点时,必然是某两个点的LCA,然后求出两两的LCA,然后判断即可= =,直接水过

 /**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g:longint;
next:point;
end;
var
i,j,k,l,m,n,a1,a2,a3,a4,a5,a6:longint;
a:array[..] of point;
b:array[..] of longint;
c:array[..,..] of longint;
d:array[..,..] of longint;
function min(x,y:longint):longint;
begin
if x<y then min:=x else min:=y;
end;
function max(x,y:longint):longint;
begin
if x>y then max:=x else max:=y;
end;
function max3(x,y,z:longint):longint;
begin
exit(max(max(x,y),z));
end;
procedure swap(var x,y:longint);
var z:longint;
begin
z:=x;x:=y;y:=z;
end;
procedure add(x,y:longint);
var p:point;
begin
new(p);p^.g:=y;p^.next:=a[x];a[x]:=p;
end;
procedure dfs(y,x:longint);
var p:point;
begin
p:=a[x];
while p<>nil do
begin
if p^.g<>y then
begin
b[p^.g]:=b[x]+;
c[,p^.g]:=x;
dfs(x,p^.g);
end;
p:=p^.next;
end;
end;
function getfat(x,y:longint):longint;
var i:longint;
begin
i:=;
while y> do
begin
if odd(y) then x:=c[i,x];
inc(i);y:=y div ;
end;
exit(x);
end;
function getcom(x,y:longint):longint;
var i:longint;
begin
if b[x]<b[y] then swap(x,y);
x:=getfat(x,b[x]-b[y]);
if x=y then exit(x);
for i:=trunc(ln(b[x])/ln()+) downto do
if c[i,x]<>c[i,y] then
begin
x:=c[i,x];
y:=c[i,y];
end;
exit(c[,x]);
end;
function dis(x,y:longint):longint;
var z:longint;
begin
z:=getcom(x,y);
exit(b[x]-b[z]+b[y]-b[z]);
end;
procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;j:=r;x:=d[(l+r) div ,];y:=d[(l+r) div ,];
repeat
while (d[i,]<x) do inc(i);
while (d[j,]>x) do dec(j);
if i<=j then
begin
swap(d[i,],d[j,]);
swap(d[i,],d[j,]);
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l>j then sort(l,j);
end;
begin
readln(n,m);
for i:= to n do a[i]:=nil;
for i:= to n- do
begin
readln(j,k);
add(j,k);add(k,j);
end;
l:=random(n)+;b[l]:=;
dfs(,l);
for i:= to trunc(ln(n)/ln()+) do
for j:= to n do
c[i,j]:=c[i-,c[i-,j]];
for i:= to m do
begin
readln(j,k,l);
a1:=getcom(j,k);
a2:=getcom(k,l);
a3:=getcom(j,l);
a4:=dis(a1,l);
d[,]:=(b[j]-b[a1])+(b[k]-b[a1])+a4;
d[,]:=a1;
a5:=dis(a2,j);
d[,]:=a5+(b[k]-b[a2])+(b[l]-b[a2]);
d[,]:=a2;
a6:=dis(a3,k);
d[,]:=(b[j]-b[a3])+a6+(b[l]-b[a3]);
d[,]:=a3;
sort(,);writeln(d[,],' ',d[,]);
end;
end.

1787: [Ahoi2008]Meet 紧急集合的更多相关文章

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

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

  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. 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. bzoj 1787: [Ahoi2008]Meet 紧急集合【树链剖分lca】

    对于三个点求最小路径长度和,答案肯定在某两个点的lca上,因为如果把集合点定在公共lca上,一定有两个点汇合后再一起上到lca,这样显然不如让剩下的那个点下来 这个lca可能是深度最深的--但是我懒得 ...

  8. bzoj1787 [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 2272  Solved: 1029 [Submi ...

  9. [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBhttp://www.lydsy.com/JudgeOnline/p ...

随机推荐

  1. JAVA轻量级文件监控

    原文地址:http://blog.csdn.net/three_man/article/details/31012903?utm_source=tuicool 介绍 本文主要介绍一种轻量级的文件监控方 ...

  2. 深入理解Java Proxy

    深入理解Java Proxy: http://blog.csdn.net/rokii/article/details/4046098 整理之后的代码: package com.stono.reftes ...

  3. MyBatis 多个查询条件的传递

    <!-- 方法1,构建查询对象: QueryCondition qc = new QueryCondition(); qc.setGender(1); qc.setBirthday(new Da ...

  4. NSNotificationCenter机制

    作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息 [[NSNotificationCenter defaultCenter]  ...

  5. Paxos 实现日志复制同步(Multi-Paxos)

    Paxos 实现日志复制同步 这篇文章以一种易于理解的方式来解释 Multi-Paxos 的机制. Multi-Paxos 的是为了创建日志复制 一种实现方式是用一组基础 Paxos 实例,每条记录都 ...

  6. HDU5918(KMP)

    Sequence I Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. MyBatis中多对多关系的映射和查询

    先说一下需求: 在页面上显示数据库中的所有图书,显示图书的同时,显示出该图书所属的类别(这里一本书可能同时属于多个类别) 创建表: 笔者这里使用 中间表 连接 图书表 和 图书类别表,图书表中 没有使 ...

  8. jquery在Asp.net下实现ajax

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTest ...

  9. shell编程其实真的很简单(四)

    上篇我们学习了shell中条件选择语句的用法.接下来本篇就来学习循环语句.在shell中,循环是通过for, while, until命令来实现的.下面就分别来看看吧. for for循环有两种形式: ...

  10. 百度富文本编辑器ueditor在jsp中的使用(ssm框架中的应用)

    折腾了一下午终于把百度富文本编辑器ueditor搞定了!   项目地址:https://github.com/724888/lightnote_new     首先我参考了一个ueditor的demo ...