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 ...
随机推荐
- C:函数
函数 函数:都是实现一定的功能.具有特定功能的代码段.凡是由系统提供的函数就是库函数,自己写的函数就是自定义函数. 如何定义一个函数 : 函数类型修饰符 函数名 (函数参数) { 函数语句 ...
- Expression<Func<T,TResult>>和Func<T,TResult> 与AOP与WCF
1>>Expression<Func<T,TResult>>和Func<T,TResult>http://www.cnblogs.com/xcsn/p/ ...
- SQLServer: 无法修改表
工具—选项—Designers—表设计器和数据库设计器—阻止保存要求重新创建表的更改前的勾去掉.
- Linux下 如何正确配置 Nginx + PHP
假设我们用PHP实现了一个前端控制器,或者直白点说就是统一入口:把PHP请求都发送到同一个文件上,然后在此文件里通过解析「REQUEST_URI」实现路由. 一般这样配置 此时很多教程会教大家这样配置 ...
- C#高性能大容量SOCKET并发(一):IOCP完成端口例子介绍(转)
原文地址 http://blog.csdn.net/SQLDebug_Fan/article/details/17556353 例子主要包括SocketAsyncEventArgs通讯封装.服务端实现 ...
- php中curl不支持https的解决办法
在php程序中使用curl去访问https站点时,报错:Protocol https not supported or disabled in libcurl 该错误信息表示php当时编译时使用的cu ...
- How to log in to Amazon EC2 using PEM format from SecureCRT
SecureCRT requires both a private and a public key. Use the supplied key.pem file from EC2 here as y ...
- Windows Self Signed Driver
In particular, Microsoft® instituted a device driver certification process for its Windows® desktop ...
- Owin管道与asp.net管道模型
------2016.3.6 更新 文中提到没有Microsoft.Owin.Host.SystemWeb 这个dll 便不会加载Startup.Configuration,因为这个dll 其中有个O ...
- 【JavaScript】深入理解JavaScript之强大的原型和原型链
由于JavaScript是唯一一个被广泛使用的基于原型继承的语言,所以理解两种继承模式的差异是需要一定时间的,今天我们就来了解一下原型和原型链. AD: hasOwnProperty函数: hasOw ...