QTREE2 spoj 913. Query on a tree II 经典的倍增思想
经典的倍增思想
题目:
给出一棵树,求:
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 经典的倍增思想的更多相关文章
- 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 ...
- 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 ...
- 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至点 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 【BZOJ2589】 Spoj 10707 Count on a tree II
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
- 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 ...
- SPOJ 375. Query on a tree (树链剖分)
Query on a tree Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- 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 ...
- 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 ...
随机推荐
- 服务器资源共享--IIS站点/虚拟目录中访问共享目录(UNC)
本文重点描述如何使用IIS访问共享资源来架设站点或执行 ASP.Net 等脚本. 通常情况下,拥有多台服务器的朋友在使用IIS建立站点的时候,会遇到如何把多台服务器的资源合并到一起的问题.如何让A服务 ...
- 通过ajax获得json数据后格式的转换
在有些情况下获取到的json数据可能是string类型的,需要把其格式化为json对象才方便解析. a)原生js通过ajax获取到的json 此时返回的数据默认是string型的,所以需要用eval( ...
- ActionBar ShareActionProvider
添加share按钮 添加share按钮的主要步骤: 1. 在ActionBar中添加share按钮 2. 从item中获取ShareActionProvider ShareActionProvider ...
- js正则表达式之解析——URL的正则表达式
首先,此片文章并不是直接告诉你,url的正则表达式是什么,以及怎么使用这个正则表达式去解析一个URL地址,相信这种问题在网络上已经能找到很多.本文的宗旨在于教你如何理解URL的正则表达式,以达到理解正 ...
- STM32 使用 printf 发送数据配置方法 -- 串口 UART, JTAG SWO, JLINK RTT
STM32串口通信中使用printf发送数据配置方法(开发环境 Keil RVMDK) http://home.eeworld.com.cn/my/space-uid-338727-blogid-47 ...
- SpinLock 自旋锁, CAS操作(Compare & Set) ABA Problem
SpinLock 自旋锁 spinlock 用于CPU同步, 它的实现是基于CPU锁定数据总线的指令. 当某个CPU锁住数据总线后, 它读一个内存单元(spinlock_t)来判断这个spinlock ...
- hadoop分布式部署(2014-3-8)
hadoop简介: (维基百科)Apache Hadoop是一款支持數據密集型分佈式應用并以Apache 2.0許可協議發佈的開源軟體框架.它支持在商品硬件構建的大型集群上運行的應用程序.Hadoop ...
- UVA 573 (13.08.06)
The Snail A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...
- linux C 9*9
#include<stdio.h> #include<conio.h> #include <windows.h> void Gotoxy(int x, int y) ...
- Editor Scripting学习笔记之Menu Item
主要用到: MenuItem属性 MenuCommand参数 可能用到: EditorApplication类 Selection类 GameObjectUtility类 FileUtil类 Undo ...