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之类的封装

    类和对象成员变量,成员函数特殊的成员变量和成员函数 函数重载 构造函数 静态变量 静态函数面向对象:封装 继承 多态封装 Encapsulation为什么需要封装?外界无法直接操作对象的具体的属性(安 ...

  2. day17 12.复习

    最后能抽取成word文档或者是图片之类的. 1.jdbc介绍  jdbc是一套标准,可以让我们Java程序员通过Java代码直接操作数据库,这就够了.jdbc涉及到的包两个:java.sql,java ...

  3. [codevs1159]最大全0子矩阵(悬线法)

    解题关键:悬线法模板题.注意此模板用到了滚动数组. #include<cstdio> #include<cstring> #include<algorithm> # ...

  4. php get_include_path();是干嘛的、??还有set_include_path();/?????

    首先 我们来看这个全局变量:__FILE__ 它表示文件的完整路径(当然包括文件名在内) 也就是说它根据你文件所在的目录不同,有着不同的值:当然,当它用在包行文件中的时候,它的值是包含的路径: 然后: ...

  5. jquery.pagination.js使用

    直接上代码: <script type="text/javascript"> var pageIndex = 1; //页面索引初始值 var pageSize = 1 ...

  6. C++ 成员函数前和函数后加const修饰符区别

    博客转载自: https://www.iteblog.com/archives/214.html 分析以下一段程序,阐述成员函数后缀const 和 成员函数前const 的作用 #include< ...

  7. c语言学习笔记 scanf和printf格式的问题

    int a =0; int b =0; scanf("%d %d",&,&b); 上面这种和下面这种哪种对? int a =0; int b =0; scanf(& ...

  8. RTX这种东西究竟有什么价值?

    我在第一家公司工作的时候,同事沟通用的就是RTX,第一感觉就是这么简单的软件也能卖钱? 这种东西有啥价值啊?不就是个没广告蓝色UI的qq吗? 还是那句话,当你已经习惯了一个东西之后,你不会感觉到他的价 ...

  9. 《Head First Servlets & JSP》-3-1st servlet MVC demo

    项目结构 用户首页 form.html <html> <body> <h1 align='center'>Beer Selection Page</h1> ...

  10. php数据连接

    <?php header("Content-type: text/html;charset=utf-8");//设置编码格式为UTF-8 error_reporting(E_ ...