1471

先学习了下tarjan算法的LCA 离线算法 它是先知道询问的结点对 在遍历的时候就已经算出来了

看篇图解 讲的很清楚

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 100010
struct node
{
int u,v,w,next;
}ed[N<<];
int head[N],t,n,q,dis[N];
int vis[N],ans[N],qhead[N<<];
int father[N],x[N],y[N];
void init()
{
t = ;
memset(head,-,sizeof(head));
memset(qhead,-,sizeof(qhead));
}
void add(int u,int v,int w)
{
ed[t].u = u;
ed[t].v = v;
ed[t].w = w;
ed[t].next = head[u];
head[u] = t++;
}
void add1(int u,int v,int d)
{
ed[t].w = d;
ed[t].u = u;
ed[t].v = v;
ed[t].next = qhead[u];
qhead[u] = t++;
}
int find(int x)
{
if(x!=father[x])
father[x] = find(father[x]);
return father[x];
}
void tarjan(int u)
{
int i;
for(i = qhead[u] ; i!=- ; i = ed[i].next)
{
int v = ed[i].v;
if(vis[v])
ans[ed[i].w] = find(v);
}
for(i = head[u] ; i!=- ; i = ed[i].next)
{
int v = ed[i].v;
if(!vis[v])
{
vis[v] = ;
dis[v] = dis[u]+ed[i].w;
tarjan(v);
father[v] = u;
}
}
}
int main()
{
int i,a,b,c;
init();
scanf("%d",&n);
for(i = ; i < n ; i++)
father[i] = i;
for(i = ; i < n ; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
scanf("%d",&q);
for(i = ; i <= q ; i++)
{
scanf("%d%d",&x[i],&y[i]);
add1(x[i],y[i],i);
add1(y[i],x[i],i);
}
vis[] = ;
tarjan();
for(i = ; i <= q ; i++)
{
printf("%d\n",dis[x[i]]+dis[y[i]]-*(dis[ans[i]]));
}
return ;
}

1471. Tree(LCA)的更多相关文章

  1. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. hdu Dylans loves tree [LCA] (树链剖分)

    Dylans loves tree view code#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...

  3. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  4. 235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)

      Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...

  5. SPOJ:Ada and Orange Tree (LCA+Bitset)

    Ada the Ladybug lives near an orange tree. Instead of reading books, she investigates the oranges. T ...

  6. [BZOJ2588]Count on a tree(LCA+主席树)

    题面 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个询问 ...

  7. poj 3237 Tree [LCA] (树链剖分)

    poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...

  8. HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树

    lca的做法还是非常明显的.简单粗暴, 只是不是正解.假设树是长链就会跪,直接变成O(n).. 最后跑的也挺快,出题人还是挺阳光的.. 动态树的解法也是听别人说能ac的.预计就是放在splay上剖分一 ...

  9. hdu 4912 Paths on the tree(lca+馋)

    意甲冠军:它使树m路径,当被问及选择尽可能多的路径,而这些路径不相交. 思考:贪心,比較忧伤.首先求一下每对路径的lca.依照lca的层数排序.在深一层的优先级高.那么就能够贪心了,每次选择层数最深的 ...

随机推荐

  1. 使用VS时点右键卡住—不响应的问题

    以前因为论坛,后来因为工作,发现已经好久没有来百度空间了.也好久没人留言或发表评论了,今天自己更新一下吧. 关于使用VS时点右键卡住或不响应的问题,我是在VS 2008中遇到的,不知道其它的版本有没有 ...

  2. 排序,求几个最值问题,输入n个整数,输出其中最小的k个元素。

    看完两个求最大值算法之后的一些感想. 如果想直接看算法的可以跳过.但是我觉得我这些想法还是比较有用的,至少对我将来的算法设计是这样的. 算法的功能越强大,必然意味着速度慢,因为根据丛林法则,那种慢又功 ...

  3. PHP json_encode中日语问题

    <?php header('Content-type:text/html;charset=utf-8'); $s = array('message'=>'4月以降.遺体の捜索活動が続けられ ...

  4. JavaScript笔记(一)

    JavaScript组成 EcmaScript:核心部分 作为解释器.几乎没有兼容性问题 DOM:Document Object Model,操作HTML页面的入口.有些操作不兼容. BOM:Brow ...

  5. 实例讲解如何在Delphi中动态创建dxBarManager内容

    一.dxBarManager中一些非常重要的概念: TCategorys:为了方便对dxBarManager中的项目进行归类而设计的一个属性,当然,只使用默认的名字为Default的Category也 ...

  6. vs2010配备boost编程环境

    vs2010配备boost编程环境 vs2010配置boost编程环境 第一步:下载boost,我下载的方法是从http://www.boost.org/上找最新的下载.名字叫boost_1_53_0 ...

  7. 计算器(delphi)

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  8. 解决rhel相关系统下yum找不到安装包的解决方法

    最近重新安装了Linux,用的版本是CentOS 5.1.但老是出现很多包找不到的情况. [root@toughhou /]# yum install rlwrap Loaded plugins: f ...

  9. 十一、mysql输入安全

    .尽量使用“绑定参数”功能,php中可用pdo进行一系列操作 .php可使用mysql_real_escape_string()函数进行输入过滤:

  10. GridView控件RowDataBound事件中获取列字段值的几种途径

    前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...