[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 ...
随机推荐
- 最新精品 强势来袭 XP,32/64位Win7,32/64位Win10系统【电脑城版】
随着Windows 10Build 10074 Insider Preview版发布,有理由相信,Win10离最终RTM阶段已经不远了.看来稍早前传闻的合作伙伴透露微软将在7月底正式发布Win10的消 ...
- C#高级编程 第十五章 反射
(二)自定义特性 使自定义特性非常强大的因素时使用反射,代码可以读取这些元数据,使用它们在运行期间作出决策. 1.编写自定义特性 定义一个FieldName特性: [AttributeUsage(At ...
- H2 database 应用
以前对内存表的引用一直采用sqllite,由于sqllite对字段的局限性无法满足需要.后来对h2 有了一定青睐做了下应用.下面对h2进行介绍. 1. H2数据库引擎 H2数据库由Java编写的,它可 ...
- RSA加密、解密、公钥私钥生成
有时项目中需要用到一些加密和解密工具,这里之前整理了一个demo,记录一下,方便查询 package com.test; import java.security.KeyFactory; import ...
- json字符串转集合或者数组
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util. ...
- 【题解】 P1092虫食算
[题解]P1092 虫食算 老题了,很经典. 用到了一些搜索套路. 可行性剪枝,劣者靠后,随机化,\(etc......\) 搜索设参也很有技巧,设一个\(adjustment\)参数可以很方便地在两 ...
- vue 的基本语法
一 . Vue 的介绍 1 . 前端的三大框架 (可以去 GitHub 查看三个框架的 star 星) vue : 作者尤雨溪, 渐进式的JavaScript 框架 react : Faceb ...
- web 全栈 学习 2 一个好的页面是如何炼成的
第一章:Web页面内容的构成2.Web内容的分工一个Web页面可能的构成(视觉上看):①文字.链接.标题②交互入口(表单元素)③图片(哪些类型)④动画 Flash动画 HTML5 CSS3 动画⑤音视 ...
- Java多线程系列 基础篇01 线程的状态
1.进程和线程 进程: 计算机中程序关于某数据集合的一次运行活动,是计算机系统进行资源分配和调度的基本单位,是操作系统结构的基础. 线程: 线程是进程的实例,是CPU进行资源分配和调度的最小单位,线程 ...
- SDUT 2133 数据结构实验之栈三:后缀式求值
数据结构实验之栈三:后缀式求值 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 对于一个基于二元运算符的后缀表示式(基本操作数都是 ...