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. Solr 空间搜索配置、按经纬度计算距离排序

    Solr 空间搜索配置 1. 在solr目录下的找到conf文件夹下的schema.xml. <fields> <!-- 在fields元素中添加如下代码 --> <fi ...

  2. linux进程的软中断通信

    linux进程的软中断通信 要求 实现软中断通信的程序 使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上发出的中断信号(即按delete键),当父进程接收到这两个 ...

  3. AndroidImageSlider(图片轮播控件)

    1,引入 dependencies { compile "com.android.support:support-v4:+" compile 'com.squareup.picas ...

  4. JS/jQuery--iframe框架内外元素的操作(转)

    JS/jQuery--iframe框架内外元素的操作 原创 2017年12月07日 14:23:09 标签: js / iframe 28 两个问题: 如何在父页面操作iframe框架内的元素? 如何 ...

  5. PHP微信分享

    注意 : 分享需要认证微信订阅号或者服务号. php 代码(thinkphp): $appid='xxx';  $appsecret='xxxx';    $timestamp = time();   ...

  6. 数学基础-3D空间的位置表示

    转自:http://www.cnblogs.com/gaoxiang12/p/5113334.html 刚体运动 本篇讨论一个很基础的问题:如何描述机器人的位姿.这也是SLAM研究的一个很基本的问题. ...

  7. PartyLocation的Post请求问题---debug

    这里,遇到了一个debug: @Override public void setPrimaryPartyLocation(PartyLocation partyLocation) { if (!get ...

  8. 网站下载器WebZip、Httrack及AWWWB.COM网站克隆器

     动机 闲扯节点,可略读. 下载并试用这些软件并非是为了一己之私,模仿他人网站以图利.鉴于国内网络环境之艰苦,我等屌丝级半罐水程序员,纵有百度如诸葛大神万般协力相助,也似后主般无能不能解决工作和娱乐中 ...

  9. Redhat 6 git服务器配置 (git-daemon)

    git-daemon是按照git的自己的git协议进行访问git服务   1.git-daemon软件安装 软件仓库见 redhat 6 git 服务器 配置 (http)   2.配置git dae ...

  10. .NET 图片转base64

    //图片 转为 base64编码的文本 private string ImgToBase64String(string Imagefilename) { try { Bitmap bmp = new ...