LCA模板题

随便找的倍增模板...

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+11;
int to[maxn<<2],nxt[maxn<<2],cost[maxn<<2];
int head[maxn],tot;
int fa[maxn][20],dep[maxn];
void init(){
memset(head,-1,sizeof head);
memset(fa,0,sizeof fa);
tot=0;
}
void add(int u,int v,int w){
to[tot]=v;cost[tot]=w;
nxt[tot]=head[u];
head[u]=tot++;
}
void bfs(int root){
queue<int> que;
memset(dis,0,sizeof dis);
memset(fa,0,sizeof fa);
memset(dep,0,sizeof dep);
fa[root][0]=root;dep[root]=0;dis[root]=0;
que.push(root);
while(!que.empty()){
int u = que.front(); que.pop();
for(int i = 1; i < 20; i++) fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i = head[u]; ~i; i = nxt[i]){
int v=to[i],w=cost[i];
if(v==fa[u][0])continue;
dep[v]=dep[u]+1;dis[v]=dis[u]+w;fa[v][0]=u;
que.push(v);
}
}
}
int lca(int u,int v){
if(dep[u]<dep[v]) swap(u,v);
for(int i = 0; i < 20; i++) if((dep[u]-dep[v])&(1<<i)) u=fa[u][i];
if(u==v)return u;
for(int i = 19; i >= 0; i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][0];
}
int main(){
int n,m,u,v,w,x;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i = 1; i <= n-1; i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
bfs(1);
for(int i = 1; i <= m; i++){
scanf("%d%d",&u,&v);
x=lca(u,v);
printf("%d\n",dis[u]+dis[v]-2*dis[x]);
// cout<<"lca "<<x<<endl;
}
}
return 0;
}

UESTC 1437的更多相关文章

  1. ACM:UESTC - 649 括号配对问题 - stack

      UESTC - 649  括号配对问题 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu ...

  2. UESTC 1015 Lweb and pepper --前,后缀最值

    题意: n种食物,每种含花椒的概率为Pi,现在已经选择了[L,R]这个区间(下标)的食物,要再选一个,使总的食物只有一种含花椒的概率最大,问选哪个最好,相同的选下标小的. 解法: 就不写解法了.此处有 ...

  3. UESTC 1852 Traveling Cellsperson

    找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...

  4. UESTC 1851 Kings on a Chessboard

    状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...

  5. UESTC 30 最短路,floyd,水

    最短路 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Statu ...

  6. ural 1437. Gasoline Station

    1437. Gasoline Station Time limit: 1.0 secondMemory limit: 64 MB Once a gasoline meter broke at a fi ...

  7. uestc oj 1218 Pick The Sticks (01背包变形)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...

  8. uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...

  9. 2015 CCPC D- Pick The Sticks(UESTC 1218) (01背包变形)

    http://acm.uestc.edu.cn/#/problem/show/1218 既然二维dp表示不了,就加一维表示是否在边界放置,放置一个,两个.有一个trick就是如果只放一根,那么多长都可 ...

随机推荐

  1. java之线程飞机大战制作

    import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...

  2. hibernate学习笔记(5)在数据库中存取图片

    如何从数据库读取存入的图片,即Blob(二进制)数据. 先从数据库读取对象. 再从获取的对象中得到blob对象. 通过blob对象的getBinaryStream()方法获取input输出流. 之后通 ...

  3. How to watch property in attrs of directive

    Q: I have a controller that has a counter that changes from time to time. That counter is tied to an ...

  4. render tree与css解析

    浏览器在构造DOM树的同时也在构造着另一棵树-Render Tree,与DOM树相对应暂且叫它Render树吧,我们知道DOM树为javascript提供了一些列的访问接口(DOM API),但这棵树 ...

  5. 使用Java建立聊天客户端

    ---------------siwuxie095                             关于 聊天服务器,详见本人博客的分类:来一杯Java, 里面的 使用ServerSocket ...

  6. 3DPDF是个什么东西?

    就是可以把3D模型放入到PDF中,然后客户可以直接用adobe reader查看这个PDF.经过搜索发现,大多数PDF编辑软件都没有直接把3D模型插入到PDF中的功能. 很多是3D软件自身提供的,比如 ...

  7. little case1

    这句话有问题: frame.setContentPane().add(button); The method setContentPane(Container) in the type JFrame ...

  8. R: 基本的数学运算

    ################################################### 问题:基本数学运算   18.4.30 R语言用于初等数学的计算,都怎么表示??加减乘除.余数. ...

  9. Luogu U15118 萨塔尼亚的期末考试(fail)

    感觉...昨天是真的傻... 题意 T个询问,每个询问给一个n,求 $ \frac{\sum_{n}^{i = 1}Fib_{i} * i}{n * (n + 1) / 2} $ Fib是斐波那契数列 ...

  10. Bootstrap 的 Collapse

    一.简介 Collapse 插件为 HTML 标签提供折叠.展开行为,依赖 transition.js(bootstrap.js 文件中已包含). 二.实现机制 实现 Collapse 效果需要: 一 ...