hdu2586倍增lca
求距离
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct edge{
int to,Next,w;
}e[N];
int dis[N],father[][N],depth[N];
int cnt,head[N],value[N];
void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].Next=head[u];
head[u]=cnt++;
}
void dfs(int u,int f)
{
father[][u]=f;
for(int i=head[u];~i;i=e[i].Next)
{
int To=e[i].to;
if(To!=f)
{
dis[To]=dis[u]+e[i].w;
depth[To]=depth[u]+;
dfs(To,u);
}
}
}
void init(int n)
{
depth[]=;
dis[]=;
dfs(,-);
for(int i=;i<;i++)
for(int j=;j<=n;j++)
father[i][j]=father[i-][father[i-][j]];
}
int lca(int x,int y)
{
if(depth[x]>depth[y])swap(x,y);
for(int i=;i<;i++)
if((depth[y]-depth[x])>>i&)
y=father[i][y];
if(x==y)return x;
for(int i=;i>=;i--)
{
if(father[i][x]!=father[i][y])
{
x=father[i][x];
y=father[i][y];
}
}
return father[][x];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
cnt=;
memset(head,-,sizeof head);
for(int i=; i<n; i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
init(n);
while(m--)
{
int a,b;
cin>>a>>b;
cout<<dis[a]+dis[b]-*dis[lca(a,b)]<<endl;
}
}
return ;
}
/******************** ********************/
hdu2586倍增lca的更多相关文章
- HDU2586 How far away? —— 倍增LCA
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Ot ...
- 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增
倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...
- [板子]倍增LCA
倍增LCA板子,没有压行,可读性应该还可以.转载请随意. #include <cstdio> #include <cstring> #include <algorithm ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- Gym100685G Gadget Hackwrench(倍增LCA)
题目大概说一棵边有方向的树,q个询问,每次询问结点u是否能走到v. 倍增LCA搞即可: 除了par[k][u]表示u结点往上走2k步到达的结点, 再加上upp[k][u]表示u结点往上走2k步经过边的 ...
- Codeforces 418d Big Problems for Organizers [树形dp][倍增lca]
题意: 给你一棵有n个节点的树,树的边权都是1. 有m次询问,每次询问输出树上所有节点离其较近结点距离的最大值. 思路: 1.首先是按照常规树形dp的思路维护一个子树节点中距离该点的最大值son_di ...
- hdu 4674 Trip Advisor(缩点+倍增lca)
花了一天半的时间,才把这道题ac= = 确实是道好题,好久没敲这么长的code了,尤其是最后的判定,各种销魂啊~ 题目中给出的条件最值得关注的就是:每个点最多只能在一个环内->原图是由一个个边连 ...
- Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序
题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s 内存限制:512.0MB 总提交次数:196 AC次数:65 平均分: ...
- codevs 1036 商务旅行 (倍增LCA)
/* 在我还不知道LCA之前 暴力跑的SPFA 70分 三个点TLE */ #include<iostream> #include<cstdio> #include<cs ...
随机推荐
- 一篇搞定vue-router
由于Vue常见于前后端分离开发场景下,所以页面跳转工作全部交给了前端,所以基于集中管理的原则,就有了vue-router插件,它给定了url和组件之间的跳转规则 Demo准备 vue init web ...
- Learn How To Cross Over The Wall
1.一个proxy的实现 http://blog.codingnow.com/2011/05/xtunnel.html 2.SOCK5 RFC http://www.faqs.org/rfcs/rfc ...
- gophercloud openstack networking 源码分析
1.network 部分 // Package networks contains functionality for working with Neutron network resources. ...
- 协程+IO切换实现并发
from gevent import monkey # 以后代码中遇到IO都会自动执行greenlet的switch进行切换 monkey.patch_all() import requests im ...
- python进程间数据不共享(示例演示)
import multiprocessing data_list = [] def task(arg): data_list.append(arg) print(data_list) def run( ...
- 图形数据库 Neo4j 开发实战【转载】
简介: Neo4j 是一个高性能的 NoSQL 图形数据库.Neo4j 使用图(graph)相关的概念来描述数据模型,把数据保存为图中的节点以及节点之间的关系.很多应用中数据之间的关系,可以很直接地使 ...
- 关于shared pool的深入探讨(五)
Oracle使用两种数据结构来进行shared pool的并发控制:lock 和 pin.Lock比pin具有更高的级别. Lock在handle上获得,在pin一个对象之前,必须首先获得该handl ...
- SQL 函数以及SQL 编程
1.数学函数:操作一个数据,返回一个结果 --去上限: ceiling ☆select --去下限:floor ☆select floor(price) from car --ABS 绝对值 --PI ...
- CSS 初级攻略
内容来自html dog. css的格式为 ‘property: value’ 给html插入css样式的方式有三种:内联.内部css.外部css文件,如下所示: <p style=" ...
- 【Flask】Flask Restful api
### 安装: Flask-Restful需要在Flask 0.8以上的版本,在Python2.6或者Python3.3上运行.通过pip install flask-restful即可安装. ### ...