QTREE2

经典的倍增思想

题目:

给出一棵树,求:

1.两点之间距离。

2.从节点x到节点y最短路径上第k个节点的编号。

分析:

  第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每个询问(x,y),想求得lca(x,y),直接用dis[x]+dis[y]-2*dis[ lca(x,y) ]即可。

  

  第二问的话,可以用倍增的方式求。我们通过求得节点x,y,lca(x,y)的深度,判断第k个节点落在哪个链上,该链是指是从x到根或者从y到根。最后倍增可以轻松求出一个链上第k个父亲是谁了。

  我实现的时候,lca以及求某个节点的第k个祖先都是用倍增的思想实现。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 1e5+5;
const int LOG = 20; struct node{
int y,val,next;
}edge[MAXN]; int dis[MAXN];
int po[MAXN],tol;
int dep[MAXN];
int p[MAXN][22]; inline void add(int x,int y,int val){
edge[++tol].y = y;
edge[tol].val = val;
edge[tol].next = po[x];
po[x] = tol;
} void dfs(int x,int fa,int depth,int cost){
dep[x] = depth;
p[x][0] = fa;
dis[x] = cost; rep1(i,LOG)
p[x][i] = p[ p[x][i-1] ][i-1]; // 倍增,可以画个树理解一下,存的是节点x第2^i个祖先
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(y==fa)continue;
dfs(y,x,depth+1,cost+edge[i].val);
}
} int lca(int x,int y){ // 倍增求lca
if(dep[x]>dep[y])swap(x,y);
if(dep[x]<dep[y]){
int del = dep[y]-dep[x];
rep(i,LOG)
if( del>>i & 1 )
y = p[y][i];
}
if(x!=y){
for(int i=LOG-1;i>=0;i--)
if( p[x][i]!=p[y][i] ){
x = p[x][i];
y = p[y][i];
}
x = p[x][0];
y = p[y][0];
}
return x;
} int cc(int x,int k){ // 求节点x的第k个祖先编号
for(int i=0;i<LOG;i++)
if( k>>i & 1 )
x = p[x][i];
return x;
} int cc(int k,int x,int y){ // 求x到y路径上第k节点编号
int ca = lca(x,y);
if(dep[x]-dep[ca]+1>=k){
return cc(x,k-1);
}else{
k -= dep[x]-dep[ca];
k = dep[y]-dep[ca]-k+1;
return cc(y,k);
}
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int n,ncase,x,y,k;
RD(ncase);
char op[10]; while(ncase--){
Clear(po);
tol = 0; RD(n);
REP(i,2,n){
RD3(x,y,k);
add(x,y,k);
add(y,x,k);
} dfs(1,1,1,0); while(scanf("%s",op),op[1]!='O'){
if(op[1]=='I'){
RD2(x,y);
printf("%d\n",dis[x]+dis[y]-2*dis[lca(x,y)]);
}else{
RD3(x,y,k);
printf("%d\n",cc(k,x,y));
}
}
} return 0;
}

  

QTREE2 spoj 913. Query on a tree II 经典的倍增思想的更多相关文章

  1. spoj 913 Query on a tree II (倍增lca)

    Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...

  2. SPOJ 913 Query on a tree II

    spoj题面 Time limit 433 ms //spoj的时限都那么奇怪 Memory limit 1572864 kB //1.5个G,疯了 Code length Limit 15000 B ...

  3. LCA SP913 QTREE2 - Query on a tree II

    SP913 QTREE2 - Query on a tree II 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点 ...

  4. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  5. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

  6. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  7. SPOJ 375. Query on a tree (树链剖分)

    Query on a tree Time Limit: 5000ms Memory Limit: 262144KB   This problem will be judged on SPOJ. Ori ...

  8. SPOJ QTREE Query on a tree 树链剖分+线段树

    题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...

  9. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

随机推荐

  1. JedisPool连接池实现难点

    [http://jiangwenfeng762.iteye.com/blog/1280700]   [可改进的问题] 问题是jedispool有没有办法监控状态,比如说当前连接有多少,当前idle连接 ...

  2. log4net使用简明教程

    转自: http://www.cnblogs.com/songhaipeng/p/3343606.html http://www.cnblogs.com/TianFang/archive/2013/0 ...

  3. 解决Unable to connect to a repository at URL 禁止访问 (forbidden)

    连接SVN报如下错误. Unable to connect to a repository at URL 禁止访问 (forbidden) 1.         右键点击本地副本,TortoiseSV ...

  4. C#制作艺术字

    相信 Word  中的 艺术字 功能大家都不陌生, 前面这个 "Word" 单词就是它所为. 今天, 我们就利用C#来制作几款自己的艺术字, 可能会对我们了解字体图像的制作原理有一 ...

  5. Thread message loop for a thread with a hidden window? Make AllocateHwnd safe

    Thread message loop for a thread with a hidden window? I have a Delphi 6 application that has a thre ...

  6. STM32 使用 printf 发送数据配置方法 -- 串口 UART, JTAG SWO, JLINK RTT

    STM32串口通信中使用printf发送数据配置方法(开发环境 Keil RVMDK) http://home.eeworld.com.cn/my/space-uid-338727-blogid-47 ...

  7. word wrap 解惑

    源起 我们经常需要“修复”一个老生常谈的“bug”,那就是文本的自动换行问题.在专业术语上,这种期望得到的渲染现象被称作“word wrap”,即文本处理器有能力把超出页边的整个词自动传到下一行. 在 ...

  8. 前端优化分析 之 javascript引用位置优化

    在很多优化法则中都提到,尽量将javascript放到页面底部,这是为什么呢 我通过firebug进行了下简单的分析 看下图  本页面首尾都存在javascript代码 我们分析得出 1.整个页面文档 ...

  9. Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学

    E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...

  10. 【JavaScript】javascript常用的东西

    DOM编程.AJAX编程.异步编程(nodejs会涉及的相对多一点,事件.ajax) 函数.函数表达式.回调函数是基础. JavaScript的函数是一个核心. 回调函数有点类似于Android中的回 ...