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 ...
随机推荐
- OC键值观察KVO
什么是KVO? 什么是KVO?KVO是Key-Value Observing的简称,翻译成中文就是键值观察.这是iOS支持的一种机制,用来做什么呢?我们在开发应用时经常需要进行通信,比如一个model ...
- ASP.NET中DesignMode属性
参考:http://blog.sina.com.cn/s/blog_4c9da9b50100r4u7.html http://book.51cto.com/art/200902/108836.htm ...
- 使用 Protocol Buffers 代替 JSON 的五个原因
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- MySQL数据库能够用随意ip连接訪问的方法
通过CMD命令行改动数据库表的一个字段的值.实现连接,訪问. 第一步.找到MYSQL软件安装所在的bin文件夹. (1)cd\当前文件夹 (2)指定MYSQL安装的bin文件夹 (3)输入 -h lo ...
- Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序
B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Swift - 本地消息的推送通知(附样例)
使用UILocalNotification可以很方便的实现消息的推送功能.我们可以设置这个消息的推送时间,推送内容等. 当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息. 1, ...
- centos 7 没有ifconfig 命令
centos 7 没有ifconfig 命令: 安装命令: yum install net-tools
- js模板引擎介绍搜集
js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...
- Python--动态类型
函数的参数传递,本质上传递的是引用.比如说: def f(x): x = 100 print x a = 1 f(a) print a 输出结果为: 100 1 参数x是一个新的引用,指向a所指的对象 ...
- 一款基于jQuery仿淘宝红色分类导航
今天给大家分享一款基于jQuery仿淘宝红色分类导航.这款分类导航适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览 ...