洛谷 P3379 【模板】最近公共祖先(LCA)

来源:互联网 发布:抽奖系统数据库设计 编辑:程序博客网 时间:2024/06/05 07:27

题目描述

如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先。

输入输出格式

输入格式:
第一行包含三个正整数N、M、S,分别表示树的结点个数、询问的个数和树根结点的序号。

接下来N-1行每行包含两个正整数x、y,表示x结点和y结点之间有一条直接连接的边(数据保证可以构成树)。

接下来M行每行包含两个正整数a、b,表示询问a结点和b结点的最近公共祖先。

输出格式:
输出包含M行,每行包含一个正整数,依次为每一个询问的结果。

输入输出样例

输入样例#1: 复制
5 5 4
3 1
2 4
5 1
1 4
2 4
3 2
3 5
1 2
4 5
输出样例#1: 复制
4
4
1
4
4
说明

时空限制:1000ms,128M

数据规模:

对于30%的数据:N<=10,M<=10

对于70%的数据:N<=10000,M<=10000

对于100%的数据:N<=500000,M<=500000

样例说明:

该树结构如下:

这里写图片描述

第一次询问:2、4的最近公共祖先,故为4。

第二次询问:3、2的最近公共祖先,故为4。

第三次询问:3、5的最近公共祖先,故为1。

第四次询问:1、2的最近公共祖先,故为4。

第五次询问:4、5的最近公共祖先,故为4。

故输出依次为4、4、1、4、4。

周六考试,很焦灼,不知道刷什么题好。。。 打打模板。。

//树剖用时仅1044Ms#include<iostream>#include<cstring>#include<cstdio>#include<cctype>#include<algorithm>#define MAXN 520010using namespace std;struct Edge{ int to,next; }e[MAXN << 1];int tot,head[MAXN],fa[MAXN],dep[MAXN],siz[MAXN],son[MAXN],top[MAXN];inline void read(int &x) {    x = 0; register char c = getchar();    while(!isdigit(c)) c = getchar();    while(isdigit(c)) x = x * 10 + c - '0',c = getchar();}inline void Add_Edge(int u,int v) {    e[++tot].to = v,e[tot].next = head[u],head[u] = tot;    e[++tot].to = u,e[tot].next = head[v],head[v] = tot;}void DFS(int u,int ft,int deepth) {    fa[u] = ft,dep[u] = deepth,siz[u] = 1,son[u] = 0;    for(int i=head[u]; i; i=e[i].next) {        int v = e[i].to;        if(v == ft) continue;        DFS(v,u,deepth + 1);        siz[u] += siz[v];        if(!son[u] || siz[son[u]] < siz[v]) son[u] = v;    }}void Dfs(int u,int Top) {    top[u] = Top;    if(son[u]) Dfs(son[u],Top);    for(int i=head[u]; i; i=e[i].next) {        int v = e[i].to;        if(v != son[u] && v != fa[u]) Dfs(v,v);    }}int LCA(int u,int v) {    while(top[u] != top[v])        if(dep[top[u]] > dep[top[v]]) u = fa[top[u]];        else v = fa[top[v]];    return dep[u] < dep[v] ? u : v;}int main(int argc,char *argv[]) {    int n,m,Root,u,v; read(n),read(m),read(Root);    for(int i=1; i<=n-1; ++i) read(u),read(v),Add_Edge(u,v);    DFS(Root,-1,1);    Dfs(Root,Root);    for(int i=1; i<=m; ++i) {        read(u),read(v);        printf("%d\n",LCA(u,v));    }    return 0;}


//倍增是树剖两倍左右。。#include<iostream>#include<cstring>#include<cstdio>#include<cctype>#include<algorithm>#define MAXN 520010using namespace std;struct Edge{ int to,next; }e[MAXN << 1];int tot,head[MAXN],fa[MAXN][21],dep[MAXN];inline void read(int &x) {    x = 0; register char c = getchar();    while(!isdigit(c)) c = getchar();    while(isdigit(c)) x = x * 10 + c - '0',c = getchar();}inline void Add_Edge(int u,int v) {    e[++tot].to = v,e[tot].next = head[u],head[u] = tot;    e[++tot].to = u,e[tot].next = head[v],head[v] = tot;}void DFS(int u,int ft,int deepth) {    fa[u][0] = ft; dep[u] = deepth;    for(int i=head[u]; i; i=e[i].next) {        int v = e[i].to;        if(v == ft) continue;        DFS(v,u,deepth + 1);    }}int LCA(int u,int v) {    if(dep[u] < dep[v]) swap(u,v);    for(int j=20; j>=0; --j)        if(dep[fa[u][j]] >= dep[v]) u = fa[u][j];    if(u == v) return u;    for(int j=20; j>=0; --j)        if(fa[u][j] != fa[v][j]) {            v = fa[v][j],u = fa[u][j];        }    return fa[u][0];}int main(int argc,char *argv[]) {    int n,m,Root,u,v; read(n),read(m),read(Root);    for(int i=1; i<=n-1; ++i) read(u),read(v),Add_Edge(u,v);    DFS(Root,-1,1);    for(int j=1; j<=20; ++j)        for(int i=1; i<=n; ++i)            fa[i][j] = fa[fa[i][j-1]][j-1];    for(int i=1; i<=m; ++i) {        read(u),read(v);        printf("%d\n",LCA(u,v));    }    return 0;}