bzoj 1787: [Ahoi2008]Meet 紧急集合 lca
来源:互联网 发布:一般淘宝哪些假货多 编辑:程序博客网 时间:2024/05/17 03:28
题意
给定一棵树,每次给出三个点,求一个点使得这三个点到该点的距离和最小。
n,q<=500000
分析
答案就是两两lca中深度最大的那个。
代码
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;const int N=500005;int n,m,size[N],last[N],cnt,dep[N],fa[N],top[N];struct edge{int to,next;}e[N*2];int read(){ int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}void addedge(int u,int v){ e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt; e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;}void dfs1(int x){ size[x]=1;dep[x]=dep[fa[x]]+1; for (int i=last[x];i;i=e[i].next) { if (e[i].to==fa[x]) continue; fa[e[i].to]=x; dfs1(e[i].to); size[x]+=size[e[i].to]; }}void dfs2(int x,int chain){ top[x]=chain;int k=0; for (int i=last[x];i;i=e[i].next) if (e[i].to!=fa[x]&&size[e[i].to]>size[k]) k=e[i].to; if (!k) return; dfs2(k,chain); for (int i=last[x];i;i=e[i].next) if (e[i].to!=fa[x]&&e[i].to!=k) dfs2(e[i].to,e[i].to);}int get_lca(int x,int y){ while (top[x]!=top[y]) { if (dep[top[x]]<dep[top[y]]) swap(x,y); x=fa[top[x]]; } return dep[x]<dep[y]?x:y;}int main(){ n=read();m=read(); for (int i=1;i<n;i++) { int x=read(),y=read(); addedge(x,y); } dfs1(1); dfs2(1,1); while (m--) { int x=read(),y=read(),z=read(); int l1=get_lca(x,y),l2=get_lca(x,z),l3=get_lca(y,z); if (dep[l2]>dep[l1]) swap(l1,l2); if (dep[l3]>dep[l1]) swap(l1,l3); printf("%d %d\n",l1,dep[x]+dep[y]+dep[z]-dep[l1]-dep[l2]*2); } return 0;}
阅读全文
0 0
- 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: [Ahoi2008]Meet 紧急集合/1832: [AHOI2008]聚会 LCA
- BZOJ 1787 [Ahoi2008]Meet 紧急集合——LCA
- bzoj 1787: [Ahoi2008]Meet 紧急集合 (LCA)
- bzoj 1787: [Ahoi2008]Meet 紧急集合
- 【bzoj 1787】: [Ahoi2008]Meet 紧急集合
- BZOJ 1787 [Ahoi2008]Meet 紧急集合 题解
- BZOJ 1787 & 1832 [Ahoi2008]Meet 紧急集合
- 【BZOJ】【P1787】【AHOI2008】【Meet 紧急集合】【题解】【LCA】
- BZOJ [Ahoi2008]Meet 紧急集合
- bzoj 1787 && bzoj 1832: [Ahoi2008]Meet 紧急集合(倍增LCA)
- bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)
- BZOJ 1787 AHOI2008 紧急集合 倍增LCA
- 剑指offer--链表中倒数第k个结点
- iOS 本地通知:UILocalNotification
- Android 组件学习笔记
- BZOJ 3566 概率充电器 (树形 概率DP)
- Unity Shader Example 28 (Reuse Depth Buffer)
- bzoj 1787: [Ahoi2008]Meet 紧急集合 lca
- 洛谷 P2038 无线网络发射器选址
- web笔记(10/13)-----课程内容
- Mybatis介绍
- AJAX 实现省市县地名三级联动
- <错排>codevs 1697 ⑨要写信
- 用户信息的添加与删除
- 算法分析与设计丨第六周丨LeetCode(10)——Best Time to Buy and Sell Stock(Easy)
- ArcSDE与SQL sevrver的连接、ArcSDE导入数据、ArcGIS Server发布地图服务