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 ...
随机推荐
- Routed Events【pluralsight】
Routing Strategies: Direct Bubbling Tunneling WHy use them? Any UIElement can be a listener Common h ...
- 使用java实现持续移动的小球
原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/5559829.html 仅为自己学习作品,使用java的JFrame框架实现持续移动的小球. ...
- glibc strlen delphi pascal
From: Will DeWitt Jr. Subject: Fast strlen routine? NewsGroup: borland.public.delphi.language.basm D ...
- python flask model 序列化
class DictSerializable(object): def as_dict(self,*args): result = OrderedDict() ...
- hdu 5258 数长方形 离散化
数长方形 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5258 Des ...
- Codeforces Gym 100463B Music Mess Hash 逻辑题
Music Mess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments ...
- ANativeWindow是个什么东西
公司经常组织一些培训,培训的都是些奇技淫巧.什么设计模式啦,开发策略啦,git啦,repo啦,另外就是培训一些开发流程的东东,例如CMMI啦.可是,却忘记了,程序员终究要归结到三个问题上: 1.解决什 ...
- 区域医疗移动医疗影像解决方案1-基于HTML5的PACS
系统描述: 1.系统基于HTML5开发,突破了平台限制,可以在任意移动终端的浏览器上调阅原始海量医学影像图像. 2.客户端无需任何下载安装,直接通过浏览器即可使用,并处理基于DICOM标准的高保真医学 ...
- 淘宝 印风 UDF
http://blog.csdn.net/zhaiwx1987/article/details/6902623
- lucene和egg项目的异同点
1 和lucene一样 支持全域索引 2 对字符串域提供全文检索,对数字类型域提供范围查询 3 采取和lucene类似的倒排表压缩方式 4 和lucene的多级跳转表不同,egg采取的是B+树做索引, ...