BZOJ 1787: [Ahoi2008]Meet 紧急集合/1832: [AHOI2008]聚会 LCA
来源:互联网 发布:visio 数据库图标 编辑:程序博客网 时间:2024/05/20 04:32
1832: [AHOI2008]聚会
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1429 Solved: 597
[Submit][Status][Discuss]
Description
Y岛风景美丽宜人,气候温和,物产丰富。Y岛上有N个城市,有N-1条城市间的道路连接着它们。每一条道路都连接某两个城市。幸运的是,小可可通过这些道路可以走遍Y岛的所有城市。神奇的是,乘车经过每条道路所需要的费用都是一样的。小可可,小卡卡和小YY经常想聚会,每次聚会,他们都会选择一个城市,使得3个人到达这个城市的总费用最小。 由于他们计划中还会有很多次聚会,每次都选择一个地点是很烦人的事情,所以他们决定把这件事情交给你来完成。他们会提供给你地图以及若干次聚会前他们所处的位置,希望你为他们的每一次聚会选择一个合适的地点。
Input
第一行两个正整数,N和M。分别表示城市个数和聚会次数。后面有N-1行,每行用两个正整数A和B表示编号为A和编号为B的城市之间有一条路。城市的编号是从1到N的。再后面有M行,每行用三个正整数表示一次聚会的情况:小可可所在的城市编号,小卡卡所在的城市编号以及小YY所在的城市编号。
Output
一共有M行,每行两个数Pos和Cost,用一个空格隔开。表示第i次聚会的地点选择在编号为Pos的城市,总共的费用是经过Cost条道路所花费的费用。
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
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
数据范围:
100%的数据中,N<=500000,M<=500000。
40%的数据中N<=2000,M<=2000。
2 5
4 1
6 0
数据范围:
100%的数据中,N<=500000,M<=500000。
40%的数据中N<=2000,M<=2000。
这题一看就是求LCA
三个两两各取一次,比较后得出最优解
再看看发现会有两个的LCA是相同的,不同的那个就是答案
括弧 用链刨写的LCA所以rank很高
而且是双倍经验 好开心
#include<cmath>#include<ctime>#include<cstdio>#include<cstdlib>#include<cstring>#include<complex>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<string>#include<queue>#include<set>#include<map>using namespace std;inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return f*x;}const int N=500100;int n,ecnt,last[N],d[N],size[N],top[N],fa[N];struct EDGE {int fr,to,nt;}e[N<<1];inline void add(int u,int v){e[++ecnt]=(EDGE){u,v,last[u]};last[u]=ecnt;}void dfs1(int u){size[u]=1;for(int i=last[u];i;i=e[i].nt){int v=e[i].to;if(v==fa[u])continue;d[v]=d[u]+1;fa[v]=u;dfs1(v);size[u]+=size[v];}}void dfs2(int u,int tp){top[u]=tp;int k=0;for(int i=last[u];i;i=e[i].nt){int v=e[i].to;if(v==fa[u])continue;if(size[k]<size[v])k=v;}if(!k)return;dfs2(k,tp);for(int i=last[u];i;i=e[i].nt){int v=e[i].to;if(v==fa[u]||v==k)continue;dfs2(v,v);}}int query_lca(int x,int y){while(top[x]!=top[y]){if(d[top[x]]<d[top[y]])y=fa[top[y]];else x=fa[top[x]];}return d[x]<d[y]?x:y;}inline int dis(int x,int y){int lca=query_lca(x,y);return d[x]+d[y]-d[lca]-d[lca];}int main(){n=read();int u,v,x,m=read();for(int i=1;i<n;i++){u=read();v=read();add(u,v);add(v,u);}dfs1(1);dfs2(1,1);while(m--){u=read();v=read();x=read();int p1=query_lca(x,u),p2=query_lca(u,v),p3=query_lca(x,v),ans=0,lca;if(p1==p2)lca=p3;else if(p2==p3)lca=p1;else lca=p2;ans=dis(x,lca)+dis(v,lca)+dis(u,lca);printf("%d %d\n",lca,ans);}}
0 0
- BZOJ 1787: [Ahoi2008]Meet 紧急集合/1832: [AHOI2008]聚会 LCA
- bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)
- BZOJ 1787: [Ahoi2008]Meet 紧急集合(LCA)
- bzoj 1787: [Ahoi2008]Meet 紧急集合(lca)
- [AHOI2008] BZOJ 1787 Meet紧急集合-LCA
- 【BZOJ】1787 [Ahoi2008]Meet 紧急集合 LCA
- bzoj 1787: [Ahoi2008]Meet 紧急集合 lca
- BZOJ 1787: [Ahoi2008]Meet 紧急集合【LCA】
- BZOJ 1787 & 1832 [Ahoi2008]Meet 紧急集合
- BZOJ 1787 [Ahoi2008]Meet 紧急集合——LCA
- bzoj 1787: [Ahoi2008]Meet 紧急集合 (LCA)
- BZOJ1787: [Ahoi2008]Meet 紧急集合&1832: [AHOI2008]聚会
- bzoj 1787: [Ahoi2008]Meet 紧急集合
- 【bzoj 1787】: [Ahoi2008]Meet 紧急集合
- BZOJ 1787 [Ahoi2008]Meet 紧急集合 题解
- bzoj 1787 && bzoj 1832: [Ahoi2008]Meet 紧急集合(倍增LCA)
- BZOJ [Ahoi2008]Meet 紧急集合
- 【BZOJ】【P1787】【AHOI2008】【Meet 紧急集合】【题解】【LCA】
- 设置eclipse中xml的默认编辑器、行数、xsd和dtd
- UVALIVE 3523 双连通分量+二分图染色
- liunx下编译安装LNMP环境
- SpringBoot+Junit Test
- QT事件处理(二) 之 事件过滤器
- BZOJ 1787: [Ahoi2008]Meet 紧急集合/1832: [AHOI2008]聚会 LCA
- SQL优化总结
- eclipse打开xml文件都显示为灰色
- 算法训练 删除数组零元素
- python学习笔记 过滤文件
- Glance镜像服务安装配置
- VS2010免注册调用大漠插件
- Hessian知识学习
- [译]使用AssetBundle Manader