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. gulp 不是内部或者外部命令 或者 webpack 不是内部或者外部命令

    gulp安装也正常,但是就是查看gulp -v的时候报错,原因:缺少系统变量PATH或者PATH变量错误 提示:这个系统变量PATH,直接追加就好(多个变量值用分号;隔开),不要删除已经有的系统变量P ...

  2. js-shortid:优雅简洁地实现短ID

    短ID在实际运用中很广泛, 其中比较典型的运用就是短地址. 市面上肯定有不少开源的生成短ID库, 基于node.js的估计也不少. 鉴于本人已然是node.js的脑残粉(本职java开发), 很多业余 ...

  3. android.intent.action.MAIN 与 android.intent.category.LAUNCHER 的验证理解

    第一种情况:有MAIN,无LAUNCHER,程序列表中无图标 原因:android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里  第二种情况:无MAIN,有LAU ...

  4. RequireJS入门与进阶

    RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...

  5. ECSHOP添加购物车加图片飞入效果

    为ECSHOP的添加购物车,加入图片飞入效果. 首先: 在goods.dwt中查找添加购物车按钮: 为添加购物车按钮加上id: 例如: <a id="iproduct_{$goods. ...

  6. http://phantomjs.org/page-automation.html

    http://phantomjs.org/page-automation.html install brew curl -LsSf http://github.com/mxcl/homebrew/ta ...

  7. Python for 循环 失效

    昨天发现一个负责处理观察者模式的基类工作失败,默认的N个观察者负责处理 发送的一些东西, 其中提供一个内置接口移除观察者: def removeObserver(self, observer): if ...

  8. Kinetic使用注意点--ellipse

    new Ellipse(config) 参数: config:包含所有配置项的对象. { radius: "半径,可以用数字a.数组[a,b]或对象{x:a,y:b}来表示" } ...

  9. gridView AspNetPager 翻页时 弹出窗体关闭报错

    gridView AspNetPager 翻页后,你右击刷新或F5会发现弹出一个刷新页面. 这是因为默认翻页都是用dopostback方式回发的.因为这时的页面已经不是原来的页面.所以会弹出提示. 这 ...

  10. EXTJS 4.2 资料 控件之Window窗体相关属性的用法

    最大化,最小化,是否显示关闭按钮 var win_CommonPicLibMultiple = Ext.create("Ext.window.Window", { title: & ...