hdu5266pog loves szh III(RMQ+LCA)
2015-10-19 18:48
381 查看
pog loves szh III
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1026 Accepted Submission(s): 232
Problem Description
Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of the tree.Then Szh choose some nodes from the tree. He wants Pog helps to find the least common ancestor (LCA) of these node.The question
is too difficult for Pog.So he decided to simplify the problems.The nodes picked are consecutive numbers from
li
to ri
([li,ri]).
Hint : You should be careful about stack overflow !
Input
Several groups of data (no more than 3 groups,n≥10000
or Q≥10000).
The following line contains ans integers,n(2≤n≤300000).
AT The following n−1
line, two integers are bi
and ci
at every line, it shows an edge connecting bi
and ci.
The following line contains ans integers,Q(Q≤300000).
AT The following Q
line contains two integers li and ri(1≤li≤ri≤n).
Output
For each case,output Q
integers means the LCA of [li,ri].
Sample Input
5 1 2 1 3 3 4 4 5 5 1 2 2 3 3 4 3 5 1 5
Sample Output
1 1 3 3 1 HintBe careful about stack overflow. 方法一: 树链剖分维护lca,这样速度比较快,通过RMQ维护连续的LCA值#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define INF 0x3f3f3f3f #define inf -0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a, b) memset(a, b, sizeof(a)) typedef long long ll; const int maxn=301010; int tot; vector<int>G[maxn]; int siz[maxn],son[maxn],fa[maxn],deep[maxn],top[maxn],id[maxn],dp[maxn][20];; void init(int n){ tot=0; for(int i=1;i<=n;i++) G[i].clear(); } void dfs1(int u,int dep){ deep[u]=dep; siz[u]=1,son[u]=0; for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v==fa[u]) continue; fa[v]=u; dfs1(v,dep+1); if(siz[son[u]]<siz[v]){ son[u]=v; } siz[u]+=siz[v]; } } void dfs2(int u,int tp){ top[u]=tp; id[u]=++tot; if(son[u]!=0){ dfs2(son[u],tp); } for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(son[u]==v||v==fa[u]) continue; dfs2(v,v); } } int Query(int u,int v){ int f1=top[u],f2=top[v]; while(f1!=f2){ if(deep[f1]<deep[f2]){ swap(f1,f2); swap(u,v); } u=fa[f1]; f1=top[u]; } if(deep[u]>deep[v]) return v; return u; } int RMQ(int L,int R){ int k=log2(R-L+1); return Query(dp[L][k],dp[R-(1<<k)+1][k]); } int main(){ int n,Q; while(scanf("%d",&n)!=EOF){ init(n); int u,v; for(int i=1;i<n;i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } fa[1]=0,siz[0]=0; dfs1(1,0); dfs2(1,0); for(int i=1;i<=n;i++) dp[i][0]=i; for(int j=1;(1<<j)<=n;j++) for(int i=1;i+(1<<j)-1<=n;i++) dp[i][j]=Query(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); scanf("%d",&Q); while(Q--){ scanf("%d%d",&u,&v); printf("%d\n",RMQ(u,v)); } } return 0; }
方法二:利用倍增法维护LCA,这样的方法比较慢
[code]#include<bits/stdc++.h>using namespace std;
const int maxn=300000+1000;
const int DEG=20;
int head[maxn],tot,fa[maxn][DEG];
int dp[maxn][DEG];
int deg[maxn];
struct Edge{
int to,next;
}e[2*maxn];
void addedge(int u,int v){
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void init(){
memset(head,-1,sizeof(head));
tot=0;
}
void bfs(int root){
fa[root][0]=root;
deg[root]=0;
queue<int>Q;
Q.push(root);
while(!Q.empty()){
int tmp=Q.front();
Q.pop();
for(int i=1;i<DEG;i++)
fa[tmp][i]=fa[fa[tmp][i-1]][i-1];//i的第2^j祖先就是i的第2^(j-1)祖先的第2^(j-1)祖先 for(int i=head[tmp];i!=-1;i=e[i].next){
int v=e[i].to;
if(fa[tmp][0]==v) //这个点是tmp的父亲 continue;
fa[v][0]=tmp;
deg[v]=deg[tmp]+1;
Q.push(v);
}
}
}
int LCA(int u,int v){
if(deg[u]>deg[v])
swap(u,v);
int hu=deg[u],hv=deg[v];
int tu=u,tv=v;
for(int det=hv-hu,i=0;det;det>>=1,i++)
if(det&1)
tv=fa[tv][i];
if(tu==tv)
return tu;
for(int i=DEG-1;i>=0;i--){
if(fa[tu][i]==fa[tv][i])
continue;
tu=fa[tu][i];
tv=fa[tv][i];
}
return fa[tu][0];
}
int RMQ(int L,int R){
int k=log2(R-L+1);
return LCA(dp[L][k],dp[R-(1<<k)+1][k]);
}
int main(){
int n,u,v,Q;
while(scanf("%d",&n)!=EOF){
init();
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
bfs(1);
for(int i=1;i<=n;i++)
dp[i][0]=i;
for(int j=1;(1<<j)<=n;j++)
[/code]
Source
BestCoder Round #43
相关文章推荐
- iOS 9适配技巧(更新版)
- 代码混淆与反射的冲突
- python排序算法和实现
- 再见,印象中的世界
- vmplayer虚拟机中如何找到网络配置的窗口
- ftp 命令
- 51nod 1001 数组中和等于k的数对(单调性优化)
- 第八周项目三(4)~~公共字符
- 【IOS 开发】企业账号将.ipa包放置网页供用户下载的实现方式 —— 伊禾媛
- 没事用html5 canvas画一个仪表盘自用,自适应的哦
- 没事用html5 canvas画一个仪表盘自用,自适应的哦
- 第八周 项目3 顺序串算法
- NY
- 底层 添加资源到android源码中-修改framework层
- 最大最小公平共享算法 Max-min Fair Share Algorithm
- 10.17作业
- TypeError: $(...).dialog is not a function debug过程
- Linux汇编与C互相调用
- 检查内核反馈uevent消息,并提取出USB插入事件
- SQL join的三种用法(inner join、left join、right join)