zoj 3195 Design the city lca倍增
给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离。
其实就是两两之间的最短距离加起来除2.
倍增的lca模板
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
int head[maxn*], num, dis[maxn], f[maxn], p[maxn][], deep[maxn], n;
struct node
{
int to, nextt, w;
}e[maxn*];
void add(int u, int v, int w) {
e[num].to = v, e[num].w = w, e[num].nextt = head[u], head[u] = num++;
}
void init() {
num = ;
mem1(head);
mem1(p);
}
void dfs(int u, int fa) {
f[u] = fa;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dis[v] = dis[u]+e[i].w;
deep[v] = deep[u]+;
dfs(v, u);
}
}
void lca_init()
{
int i,j;
for(i = ; i<=n; i++)
p[i][] = f[i];
for(j = ; (<<j)<=n; j++) {
for(i = ; i<=n; i++) {
if(p[i][j-] != -)
p[i][j] = p[p[i][j-]][j-];
}
}
}
int lca(int a,int b)
{
int i,j;
if(deep[a]<deep[b])
swap(a, b);
for(i = ; (<<i)<=deep[a]; i++);
i--;
for(j = i; j>=; j--)
if(deep[a]-(<<j)>=deep[b])
a = p[a][j];
if(a == b)
return a;
for(j = i; j>=; j--)
{
if(p[a][j] != - && p[a][j] != p[b][j])
{
a = p[a][j];
b = p[b][j];
}
}
return f[a];
}
int main()
{
int x, y, m, w, z, ok = ;
while(cin>>n) {
if(ok)
puts("");
ok = ;
init();
for(int i = ; i<n-; i++) {
scanf("%d%d%d", &x, &y, &w);
add(x, y, w);
add(y, x, w);
}
dfs(, -);
lca_init();
cin>>m;
while(m--) {
scanf("%d%d%d", &x, &y, &z);
int ans = ;
ans += dis[x]+dis[y]-*dis[lca(x,y)];
ans += dis[x]+dis[z]-*dis[lca(x,z)];
ans += dis[y]+dis[z]-*dis[lca(y,z)];
printf("%d\n", ans/);
}
}
return ;
}
zoj 3195 Design the city 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 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
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 Design the city
题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...
- 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(LCA)
解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...
- 算法笔记--lca倍增算法
算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...
随机推荐
- 修改ubuntu的默认python版本
ubuntu14.04LTS上装有两个版本的python:python2.7.6与python3.4,默认使用python2.7.6. 可以使用以下命令来修改默认python版本: sudo cp / ...
- saiku之固定维度(必选维度)
工作中遇到的问题,记录下来方便以后查找. 在saiku中如何设定固定维度? 找到WorkspaceDropZone.js文件,在synchronize_query: function(){}方法中的“ ...
- 一个神奇的bug
在使用touch命令创建了一个swift文件后,如果用xcode打开该文件,然后输入 #!/usr/bin/env xcrun swift 接着你就会发现,xcode崩溃了.
- Qt程序设计——txt文本中获取字符串的问题
QT对txt文本进行读写一般是采用QFile和QTextStream结合使用,在此不细说,主要说一下读取txt文本的注意事项.因为txt文本中有中文也有英文还有数字,要准确获得需要的字串可就要多一个心 ...
- Ext.Net 使用总结之GridPanel的删除事件
1.关于Ext.net中GridPanel的删除事件 首先是GridPanel,如下: <ext:GridPanel ID="GridPanel1" runat=" ...
- onhashchange事件--司徒正美
onhashchange事件是针对AJAX无缝刷新导致后退键失效而产生的事件,因此属于一个够新的事件,浏览器兼容性如下: Feature Chrome Firefox IE Opera Safari ...
- WebService:The remote server returned an error: (400) Bad Request
开发工具:VS2010.开发组件:WebService.运行环境:Windows 今天一个同事在进行计费接口联调试时,发现了一个非常奇怪的问题:接口在家里环境测试,一切正常,但是部署到现网环境之后,连 ...
- oracle sql developer 使用技巧
1.配置自动提示快捷键: 选中completion insight 然后选中新建快捷方式,然后按alt+/,再选中分配即可:
- USACO Section 4.2 The Perfect Stall(二分图匹配)
二分图的最大匹配.我是用最大流求解.加个源点s和汇点t:s和每只cow.每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to prod ...
- symbolを見つけられませんの解決策
类互相调用造成编译时找不到符号symbol 可以同时编译多个文件: