zoj 3195(LCA加强版)
https://www.cnblogs.com/violet-acmer/p/9686774.html
题意:
给一个无根树,有q个询问,每个询问3个点(a,b,c),问将这3个点连起来,距离最短是多少。
题解:
我的思路:
(1)分别求出Lca(a,b),Lca(a,c),Lca(b,c);
(2)找到三个Lca( )中深度最深的那个节点(此处假设Lca(a,b)深度最深),设变量 res = dist[a]+dist[b]-2*dist[Lca(a,b)];
(3)求出Lca(a,b)与c的最近公共祖先,res += dist[c]+dist[Lca(a,b)]-2*dist[Lca(a,b,c)];
参考大佬题解思路:
分别求LCA(a,b),LCA(a,c),LCA(b,c),和对应的距离,然后3个距离相加再除以2就是这个询问的结果。
AC代码:
晚上看了LCA的RMQ算法的一个题,改了一晚上,貌似理解了,太累了,明天再把这个代码的细节写一下.............
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define pb push_back
#define ll long long
#define mem(a,b) (memset(a,b,sizeof a))
const int maxn=5e4+; struct Node
{
int to;
int weight;
Node(int to,int weight):to(to),weight(weight){}
};
vector<Node >edge[maxn];
int n,q;
int fa[][maxn];
int dist[maxn];
int depth[maxn];
void addEdge(int u,int v,int w)
{
edge[u].pb(Node(v,w));
edge[v].pb(Node(u,w));
}
void Init()
{
for(int i=;i < n;++i)
edge[i].clear();
}
void Dfs(int u,int f,int d,int l)
{
fa[][u]=f;
dist[u]=l;
depth[u]=d;
for(int i=;i < edge[u].size();++i)
{
int to=edge[u][i].to;
int w=edge[u][i].weight;
if(to != f)
Dfs(to,u,d+,l+w);
}
}
void Pretreat()
{
Dfs(,-,,);
for(int k=;k+ < ;++k)
for(int u=;u < n;++u)
if(fa[k][u] == -)
fa[k+][u]=-;
else
fa[k+][u]=fa[k][fa[k][u]];
}
int Lca(int u,int v)
{
if(depth[u] > depth[v])
swap(u,v);
int k;
for(k=;(<<k) <= depth[v];++k);
k--;
for(int i=k;i >= ;--i)
if((depth[v]-(<<i)) >= depth[u])
v=fa[i][v];
if(u == v)
return v;
for(int i=k;i >= ;--i)
if(fa[i][v] != - && fa[i][v] != fa[i][u])
{
v=fa[i][v];
u=fa[i][u];
}
return fa[][v];
}
int main()
{
bool flag=false;
while(~scanf("%d",&n))
{
Init();
if(flag)
printf("\n");
flag=true;
for(int i=;i < n;++i)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
}
Pretreat();
scanf("%d",&q);
for(int i=;i <= q;++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int res;
int lcaAB=Lca(a,b);
int lcaAC=Lca(a,c);
int lcaBC=Lca(b,c);
if(depth[lcaAB] > depth[min(lcaAC,lcaBC)])
{
res=dist[a]-dist[lcaAB]+dist[b]-dist[lcaAB];
res += dist[lcaAB]-dist[Lca(lcaAB,c)]+dist[c]-dist[Lca(lcaAB,c)];
}
else if(depth[lcaAC] > depth[min(lcaAB,lcaBC)])
{
res=dist[a]-dist[lcaAC]+dist[c]-dist[lcaAC];
res += dist[lcaAC]-dist[Lca(lcaAC,b)]+dist[b]-dist[Lca(lcaAC,b)];
}
else
{
res=dist[b]-dist[lcaBC]+dist[c]-dist[lcaBC];
res += dist[lcaBC]-dist[Lca(lcaBC,a)]+dist[a]-dist[Lca(lcaBC,a)];
}
printf("%d\n",res);
}
}
return ;
}
基于二分的LCA
zoj 3195(LCA加强版)的更多相关文章
- zoj 3195 Design the city LCA Tarjan
题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...
- 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 模板题)
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
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ 3195 Design the city 题解
这个题目大意是: 有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值. 多组数据 每组数据 1 < N < 50000 1 < Q ...
- zoj 3195
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3320 离线算法RE了.. #include<stdio.h> #i ...
- zoj 3649 lca与倍增dp
参考:http://www.xuebuyuan.com/609502.html 先说题意: 给出一幅图,求最大生成树,并在这棵树上进行查询操作:给出两个结点编号x和y,求从x到y的路径上,由每个结点的 ...
- ZOJ - 3195 Design the city
题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...
随机推荐
- C_数据结构_循环实现求阶乘
# include <stdio.h> int main(void) { int val; printf("请输入一个数字:"); printf("val = ...
- 个人对vuex的表象理解(笔记)
一个东西,首先要知道为什么用它,为什么要vuex,官方解释为了解决繁杂事件订阅和广播,那么事件的$dispatch,$on,怎么就复杂了?许多人是不是感觉后者还挺简单的,对的 如果简单小型项目,那么不 ...
- 代码规范(RL-TOC)用更合理的方式写 JavaScript
代码可以改变世界 不规范代码可以毁掉世界 只有先学会写规范的代码,才可以走的更远 编程语言之间有很多编程规范都是通用: 命名 不要用语言不明的缩写,不用担心名字过长,名字一定要让别人知道确切的意思; ...
- Week 2 代码规范
Question 1: 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. My opinion: 我认为恰恰相反,这个可以提高人们的开发效率. 在团队合作当中,如果 ...
- Linux内核第八节 20135332武西垚
第一种分类: I/O-bound:频繁进行I/O,并且需要花费很多时间等待I/O完成 CPU-bound:计算密集,需要大量的CPU时间进行运算 第二种分类: 批处理进程:不必与用户交互,常在后台进行 ...
- HDOJ2010_水仙花数
一道水题.一直出现Output Limit Exceeded的原因是在while循环中没有终止条件的时候会自动判断并报错,写的时候忘记加!=EOF结束标识了. HDOJ2010_水仙花数 #inclu ...
- type=hidden
非常值得注意的一个,通常称为隐藏域:如果一个非常重要的信息需要被提交到下一页,但又不能或者无法明示的时候. 一句话,你在页面中是看不到hidden在哪里.最有用的是hidden的值.
- WebPage设计专业术语
header footer master content placeholder breadcrumb 面包屑(breadcrumb)源于一个童话,在网站中就是一行层级属性链接组成的线性链接标示(我的 ...
- K Nearest Neighbor 算法
文章出处:http://coolshell.cn/articles/8052.html K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KN ...
- SAP PP顾问面试题及资料
SAP PP顾问面试试题及资料 1.主数据 组织架构 •SAP中主要的组织架构有哪些?哪些用于PP的组织架构?•成本控制范围.公司代码.估价范围.工厂.库存地点之间的关系 物料主数据 •不同的物料主数 ...