HDU 1330 Nearest Common Ancestors(求两个点的近期公共祖先)

题目链接:

id=1330">传送门

在线算法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 40010;

struct nod{
    int to,next,w;
}edge[maxn*2];

int head[maxn],ip,tot;
bool vis[maxn];
int R[maxn*2],ver[maxn*2];
int dp[maxn*2][25];
int first[maxn];
int dis[maxn];
bool isroot[maxn];

void init(){
    memset(head,-1,sizeof(head));
    memset(isroot,0,sizeof(isroot));
    memset(vis,false,sizeof(vis));
    dis[1]=0,ip=0,tot=0;
}

void add(int u,int v){
    edge[ip].to=v;
    edge[ip].next=head[u];
    head[u]=ip++;
}
/***
ver[i]=x:第i个点是x.
first[i]=x: 点i第一次出现的位置是x
R[i]=x:第i个点的深度为x;
dis[i]=x;点i到根节点的距离为x.
***/
void dfs(int u,int dept){
    vis[u]=true,ver[++tot]=u,first[u]=tot,R[tot]=dept;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(!vis[v]){
            dfs(v,dept+1);
            ver[++tot]=u,R[tot]=dept;
        }
    }
}

void ST(int n){
    for(int i=1;i<=n;i++) dp[i][0]=i;
    for(int i=1;(1<<i)<=n;i++){
        for(int j=1;j+(1<<i)<=n;j++){
            int a = dp[j][i-1],b=dp[j+(1<<(i-1))][i-1];
            if(R[a]<R[b]) dp[j][i]=a;
            else dp[j][i]=b;
        }
    }
}

int RMQ(int l,int r){
    int k=0;
    while(1<<(k+1)<=r-l+1)
        k++;
    int x = dp[l][k], y=dp[r-(1<<k)+1][k];
    if(R[x]<R[y]) return x;
    else return y;
}

int LCA(int u,int v){
    u=first[u],v=first[v];
    if(u>v) swap(u,v);
    return ver[RMQ(u,v)];
}

int main(){
    int t,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        init();
        for(int i=0;i<n-1;i++){
            int u,v,w;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
            isroot[v]=1;
        }
        int x,y;
        scanf("%d%d",&x,&y);
        int root;
        for(int i=1;i<=n;i++){
            if(!isroot[i]){
                root=i;
                break;
            }
        }
        dfs(root,1);
        ST(n*2-1);
        printf("%d\n",LCA(x,y));
    }
    return 0;
}

离线算法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 40010;

struct nod{
    int u,v,next,w,lca;
}edge[maxn*2],edge1[maxn];

int par[maxn],ancestors[maxn];
int head[maxn];
int dis[maxn],ip;
int x,y;
bool vis[maxn];
bool root[maxn];

void init(){
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    for(int i=0;i<maxn;i++) root[i]=true;
    for(int i=1;i<maxn;i++) par[i]=i;
    ip=0;
}

int find_par(int x){
    if(x!=par[x]) return par[x]=find_par(par[x]);
    return par[x];
}

void Union(int u,int v){
    u=find_par(u);
    v=find_par(v);
    if(u!=v) par[v]=u;
}

void add(int u,int v){
    edge[ip].v=v;
    edge[ip].next=head[u];
    head[u]=ip++;
}

bool ans;

void tarjan(int u){
    vis[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v = edge[i].v;
        if(!vis[v]){
            dis[v]=dis[u]+edge[i].w;
            tarjan(v);
            Union(u,v);
        }
    }
    if(u==x&&vis[y]&&!ans){
        ans=1;
        printf("%d\n",find_par(y));
        return;
    }
    if(u==y&&vis[x]&&!ans){
        ans=1;
        printf("%d\n",find_par(x));
        return;
    }
}

int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        init();
        for(int i=0;i<n-1;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            root[v]=false;
            add(u,v);
            add(v,u);
        }
        ans=0;
        scanf("%d%d",&x,&y);
        for(int i=1;i<=n;i++){
            if(root[i]){
                tarjan(i);
                break;
            }
        }
    }
    return 0;
}
时间: 2024-10-24 19:39:47

HDU 1330 Nearest Common Ancestors(求两个点的近期公共祖先)的相关文章

HDU 1330 Nearest Common Ancestors(求两个点的最近公共祖先)

题目链接:传送门 在线算法: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 40010; struct nod{ int to,next,w; }edge[maxn*2]; int head[maxn],ip,tot; bool vis[maxn]; int R[maxn*2

POJ 1330 Nearest Common Ancestors LCA题解

本题是一个多叉树,然后求两点的最近公共单亲节点. 就是典型的LCA问题.这是一个很多解法的,而且被研究的很透彻的问题. 原始的解法:从根节点往下搜索,若果搜索到两个节点分别在一个节点的两边,那么这个点就是最近公共单亲节点了. Trajan离线算法:首次找到两个节点的时候,如果记录了他们的最低单亲节点,那么答案就是这个最低的单亲节点了. 问题是如何有效记录这个最低单亲节点,并有效根据遍历的情况更新,这就是利用Union Find(并查集)记录已经找到的节点,并及时更新最新访问的节点的当前最低单亲节

POJ 1330 Nearest Common Ancestors LCA(在线RMQ,离线Tarjan)

链接:http://poj.org/problem?id=1330 题意:只看题目就知道题目是什么意思了,最近公共祖先,求在一棵树上两个节点的最近公共祖先. 思路:求最近公共祖先有两种算法,在线和离线,在线方法是用RMQ求LCA,一句话总结就是在从DFS时,从第一个点到第二个点的最短路径中深度最浅的点就是公共祖先,用RMQ处理,一般问题的最优解决方式的复杂度是O(NlogN)的预处理+N*O(1)的查询.离线方法是Tarjan算法,将所有询问的两个点都记录下来,在DFS过程中不断将每个点自身作为

poj 1330 Nearest Common Ancestors 【LCA】

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20073   Accepted: 10631 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

POJ - 1330 Nearest Common Ancestors(基础LCA)

POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu Submit Status Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In t

poj 1330 Nearest Common Ancestors

题目连接 http://poj.org/problem?id=1330 Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each node is labeled with an integer from {1, 2,...,

POJ 1330 Nearest Common Ancestors 倍增算法的LCA

POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节点的第2j个父亲是多少   这个代码不是我的,转自 邝斌博客 1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-9-5 9:45:17 4 File Name :F

POJ 1330 Nearest Common Ancestors(树)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17628   Accepted: 9335 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

[POJ 1330] Nearest Common Ancestors (朴素方法)

POJ 1330: Nearest Common Ancestors Time Limit: 1000ms Memory Limit: 32Mb Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each node is labeled with an integer fro