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. 1、序列化 2、转义 3、eval 4、正则表达式 5、时间处理

    1.序列化 JSON.stringify(obj)   序列化 JSON.parse(str)        反序列化 2.转义 decodeURI( )                   URl中 ...

  2. 使用Java2D改善API的绘制效果

    ---------------siwuxie095                             工程名:TestSwingPaintAPI 包名:com.siwuxie095.swingp ...

  3. xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at:

    今天更新了一下mac系统,然后发现  idea的svn插件不能用了,有的报 有的报  is not under version 经查找需要做如下处理,打开终端,安装xcode: xcode-selec ...

  4. mysql join查询的on 与 where 的不同点

    on总数以left join的左表为准,where会过滤掉,不符合where条件的数据

  5. GNU Gettext

    一.简介 当前,无论是商业还是免费软件都是英文的,并用英文做为文档.直到现在,为使其它非英语语言用户也能够进行交互所做的工作仍然不足,所以这对非英语语言的国家很不利.然而,随着GNU gettext工 ...

  6. 999F Cards and Joy

    传送门 题目大意 有n个人n*m张牌,每个人分m张牌.每个人有一个自己喜欢的数值,如果他的牌中有x张数值等于这个值则他的高兴度为L[x],求怎样分配牌可以使得所有人的总高兴度最大. 分析 我们发现每一 ...

  7. jquery 仿文本编辑器(智能提示输入文字)

    1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InputAu ...

  8. C#中 ACCESS数据库常用操作语句...容易出错的地方(DateTime类型)

    这次在C#编程过程中,第一次用到了ACCESS数据库,重点涉及到时间类型,整数类型.是否类型....;遇到了许多困难,就把这些整理了下来,与大家分享. 一.Insert语句的基本格式: INSERT ...

  9. ModelSim Simulation of RapidIO II IP Core Demonstration Testbench May Require ld_debug Command

    Solution ID: fb83262Last Modified: May 17, 2013Product Category: Intellectual PropertyProduct Area: ...

  10. 自动化打包资源混淆集成python实践----资源混淆

    前面自动化打包资源混淆集成python实践----打包一文讲述了四种打包方案,以及美团打包方案.apk注释添加渠道号方案的实现.这里讲集成资源混淆. 1.资源混淆带来的好处: 1)对资源文件起一定的保 ...