[zoj3195]Design the city(LCA)
解题关键:求树上三点间的最短距离。
解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
typedef long long ll;
using namespace std;
const int maxn=;
const int maxm=;
int _pow[maxm],m,n;
int head[maxn],tot;
int ver[maxn*],depth[maxn*],first[maxn],rmq[maxn*][],id;//5个数组,注意哪个需要乘2
int dis[maxn];
inline int read(){
char k=;char ls;ls=getchar();for(;ls<''||ls>'';k=ls,ls=getchar());
int x=;for(;ls>=''&&ls<='';ls=getchar())x=(x<<)+(x<<)+ls-'';
if(k=='-')x=-x;return x;
} struct edge{
int to,w,nxt;
}e[maxn*];//链式前向星建树 void init(){
memset(head,-,sizeof head);
tot=;
id=;
} void add_edge(int u,int v,int w){
e[tot].to=v;
e[tot].w=w;
e[tot].nxt=head[u];
head[u]=tot++;
} void dfs(int u,int fa,int dep){
ver[++id]=u;//第i个访问到的结点编号
depth[id]=dep;//第i个访问到的结点深度
first[u]=id;
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
int w=e[i].w;
if(v==fa) continue;
dis[v]=dis[u]+w;//dis是先序遍历求
dfs(v,u,dep+);
ver[++id]=u;//后序遍历,再次访问父节点
depth[id]=dep;
}
} void rmq_init(int n){
int k=int(log(n)/log());
for(int i=;i<=n;++i) rmq[i][]=i;
for(int j=;j<=k;++j){
for(int i=;i+_pow[j]-<=n;++i){//因为存的是索引
int a=rmq[i][j-],b=rmq[i+_pow[j-]][j-];
rmq[i][j]=depth[a]<depth[b]?a:b;
}
}
} int rmq_query(int l,int r){
int k=int(log(r-l+1.0)/log(2.0));
int a=rmq[l][k],b=rmq[r-_pow[k]+][k];
return depth[a]<depth[b]?a:b;
}//返回的依然是索引 int LCA(int u,int v){
int x=first[u],y=first[v];
if(x>y)swap(x,y);
int res=rmq_query(x,y);
return ver[res];
} int main(){
for(int i=;i<maxm;++i) _pow[i]=<<i; //预处理2^n
int t,a,b,c,d,x=;
while(scanf("%d",&n)!=EOF){
if(x++) printf("\n");
init();
for(int i=;i<n-;++i){
a=read()+,b=read()+,c=read();
add_edge(a,b,c);
add_edge(b,a,c);
}
dfs(,-,);
rmq_init(*n-);
d=read();
for(int i=;i<d;++i){
a=read()+;b=read()+;c=read()+;
int t1=LCA(a,b);t1=dis[a]+dis[b]-*dis[t1];
int t2=LCA(b,c);t2=dis[b]+dis[c]-*dis[t2];
int t3=LCA(a,c);t3=dis[a]+dis[c]-*dis[t3];
printf("%lld\n",(t1+t2+t3)/);
}
}
return ;
}
[zoj3195]Design the city(LCA)的更多相关文章
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ3195 Design the city [2017年6月计划 树上问题04]
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ3195 Design the city(LCA)
题目大概说给一棵树,每次询问三个点,问要把三个点连在一起的最少边权和是多少. 分几种情况..三个点LCA都相同,三个点有两对的LCA是某一点,三个点有两对的LCA各不相同...%……¥…… 画画图可以 ...
- ZOJ 3195 Design the city (LCA 模板题)
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terribl ...
- ZOJ 3195 Design the city LCA转RMQ
题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...
- zoj 3195 Design the city lca倍增
题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #in ...
- zoj 3195 Design the city LCA Tarjan
题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- zoj——3195 Design the city
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
随机推荐
- ASP.NET MVC 相关的社群与讨论区
ASP.NET MVC 官方论坛 http://forums.asp.net/1146.aspx 台湾微软MSDN论坛 --- ASP.NET 与 AJAX(ASP.NET AND AJAX)讨论区 ...
- c# 根据读取的配置信息删除某个目录及下所有文件
#region 定时删除目录文件 /// <summary> /// 定时删除目录文件 /// northeasttycoon /// </summary> /// <p ...
- PHP购物车模块的实现(php/ajax/session)
购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- Python 深入剖析SocketServer模块(二)(V2.7.11)
五.Mix-In混合类 昨天介绍了BaseServer和BaseRequestHandler两个基类,它们只用与派生,所以贴了它们派生的子类代码. 今天介绍两个混合类,ForkingMix-In 和 ...
- 【BZOJ3630】[JLOI2014]镜面通道 几何+最小割
[BZOJ3630][JLOI2014]镜面通道 Description 在一个二维平面上,有一个镜面通道,由镜面AC,BD组成,AC,BD长度相等,且都平行于x轴,B位于(0,0).通道中有n个外表 ...
- python书写日志的重要性?
转自:https://blog.csdn.net/weixin_43063753/article/details/82899395 程序为什么要写日志?#为了能够在程序在运行过程中记录错误,方便维护, ...
- 如何获取 Greenplum 中用户最后登录时间和登录频率
这几天搞系统迁移,老板突然想知道给客户开的那么多用户当中,哪些还在用,哪些已经不用了.我们的数据库是 Greenplum,而且还是一直没有升级的老版本,Google 了一下没有发现特别好的查看用户登录 ...
- enumerate next eval reload 内置函数的用法
enumerate next eval reload 内置函数的用法 #enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用 ...
- Linux入门基础(三)——系统命令
- Pentaho BIServer Community Edtion 6.1 使用教程 第四篇 安装和使用Saiku 插件 进行 OLAP
OLAP(On-Line Analytical Processing,联机分析处理)是一个使分析师.管理者和执行者从原始数据中用来快速.一致.交互访问的一种软件技术,从而真实的反映企业的数据情况.OL ...