题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1832


省选出出了CF的感觉.....

显然一发贪心,如果两个点显然就是他们的$LCA$(不在一条链上的情况),第三个点不就直接走到这个$LCA$么,考虑$3$种分别组合的情况即可。


 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 500100
#define llg int
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,f[maxn][],deep[maxn];
vector<llg>a[maxn]; void dfs(llg x,llg fa)
{
llg w=a[x].size(),v;
deep[x]=deep[fa]+,f[x][]=fa;
for (llg i=;i<w;i++)
{
v=a[x][i];
if (v==fa) continue;
dfs(v,x);
}
} void make_f()
{
for (llg j=;j<=;j++)
for (llg i=;i<=n;i++)
f[i][j]=f[f[i][j-]][j-];
} llg find(llg x,llg y)
{
if (deep[x]<deep[y]) swap(x,y);
for (llg i=;i>=;i--)
if (deep[f[x][i]]>=deep[y])
x=f[x][i];
if (x==y) return x;
for (llg i=;i>=;i--)
if (f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][];
} void init()
{
llg x,y;
cin>>n>>m;
for (llg i=;i<n;i++)
{
scanf("%d%d",&x,&y);
a[x].push_back(y),a[y].push_back(x);
}
dfs(,);
make_f();
} llg dis(llg x,llg y){return deep[x]+deep[y]-deep[find(x,y)]*;} int main()
{
yyj("bzoj1832");
init();
llg x,y,z,val,po,ans;
while (m--)
{
ans=0x7fffffff;
scanf("%d%d%d",&x,&y,&z); val=dis(x,y)+dis(find(x,y),z);
if (val<ans) {ans=val,po=find(x,y);} val=dis(z,y)+dis(find(z,y),x);
if (val<ans) {ans=val,po=find(z,y);} val=dis(x,z)+dis(find(x,z),y);
if (val<ans) {ans=val,po=find(x,z);} printf("%d %d\n",po,ans);
}
return ;
}

【BZOJ】1832: [AHOI2008]聚会的更多相关文章

  1. BZOJ 1832: [AHOI2008]聚会( LCA )

    LCA模板题...不难发现一定是在某2个人的LCA处集合是最优的, 然后就3个LCA取个最小值就OK了. 距离就用深度去减一减就可以了. 时间复杂度O(N+MlogN) (树链剖分) -------- ...

  2. bzoj 1832: [AHOI2008]聚会

    良心题2333 三个点两两求一遍就行,最小肯定是在某2个点的lca处,(肯定让第三个人去找2个人,不能让2个人一起去找第三个人233) #include<bits/stdc++.h> #d ...

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

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

  4. bzoj 1787 && bzoj 1832: [Ahoi2008]Meet 紧急集合(倍增LCA)算法竞赛进阶指南

    题目描述 原题连接 Y岛风景美丽宜人,气候温和,物产丰富. Y岛上有N个城市(编号\(1,2,-,N\)),有\(N-1\)条城市间的道路连接着它们. 每一条道路都连接某两个城市. 幸运的是,小可可通 ...

  5. bzoj1832: [AHOI2008]聚会

    写过的题... #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...

  6. bzoj 1832 lca

    1832: [AHOI2008]聚会 Time Limit: 10 Sec  Memory Limit: 64 MB Description Y岛风景美丽宜人,气候温和,物产丰富.Y岛上有N个城市,有 ...

  7. BZOJ 1790: [Ahoi2008]Rectangle 矩形藏宝地

    BZOJ 1790: [Ahoi2008]Rectangle 矩形藏宝地 题目传送门 [题目大意] 游戏的主办方把这块开阔地当作第一象限,将所有可能埋藏宝藏的地方划成一个个矩形的土地,并把这些矩形土地 ...

  8. bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会

    bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...

  9. BZOJ 1832、1787 洛谷 4281 [AHOI2008]紧急集合

    [题解] 题目要求找到一个集合点,使3个给定的点到这个集合点的距离和最小,输出集合点的编号以及距离. 设三个点为A,B,C:那么我们可以得到Dis=dep[A]+dep[B]+dep[C]-dep[L ...

随机推荐

  1. How to solve the problem that BMW Icom A2 A3 host can’t be connected?

    Aftre the BMW ICOM host is connected to the car via a 16PIN connector, and the other side is connect ...

  2. 洛谷 P1015 回文数

    #include<iostream> #include<cstdio> #include<cmath> #include<string> #includ ...

  3. ogg 12.3 中 format release的变化

    Non-CDB databases with compatibility set to 12.1, FORMAT RELEASE 12.2 or above is supported. Non-CDB ...

  4. Spring Boot 中使用 @ConfigurationProperties 注解

    @ConfigurationProperties 主要作用:绑定 application.properties 中的属性 例如: @Configuration public class DataSou ...

  5. Qt 文本文件的打开、新建、保存以及另存为

    我们平时在使用windows的notepad以及其他各种软件过程中,都会有保存文件和另存为两种功能,这两者不能混为一谈. 一.保存时有两种情况,一种是对于新创建的文件,一种是对于已有的文件,前者需要打 ...

  6. Eloquent JavaScript #01# values

    When action grows unprofitable, gather information; when information grows unprofitable, sleep.      ...

  7. AngularJS中angular.min.js:80 Error: [ng:areq] http://errors.angularjs.org/1.2.9/ng/areq

    报出来的时候,出现这种错误,是因为在引入控制器的时候没有引入成功,我遇到这个错误是在因为没有将父控制器引入到子控制器中.

  8. Selenium+Headless Firefox

    背景 今天本地调试基于Selenium+PhantomJS的动态爬虫程序顺利结束后,着手部署到服务器上,刚买的热乎的京东云,噼里啪啦一顿安装环境,最后跑的时候报了这么个错误: UserWarning: ...

  9. python简说(十三)递归

    #递归就是函数自己调用自己count = 0# def abc():# pass# abc()最多循环999次

  10. 0x17二叉堆之超市

    题目链接:https://www.acwing.com/problem/content/147/ 容易想到一个贪心策略:在最优解中,对于每个时间(天数) t,应该在保证不卖出过期商品的前提下,尽量卖出 ...