POJ1330Nearest Common Ancestors——最近公共祖先

http://poj.org/problem?id=1330

给一个有根树,一个查询节点(u,v)的最近公共祖先

836K 16MS

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
const int maxn=10005;
using namespace std;
int n,m;//结点数,查询数
int f[maxn];//并查集
bool vis[maxn];//访问标记
int ancestor[maxn];//祖先
int answer[2];//存储最后的查询结果
int in[maxn];//入度
vector<int> que[maxn];
struct Edge{
    int to;
    int next;
}edge[maxn<<1];int head[maxn],tot;

void addedge(int u,int v){
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void Init(){
    tot=0;
    memset(in,0,sizeof(in));
    memset(f,-1,sizeof(f));
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    memset(ancestor,0,sizeof(ancestor));
    for(int i=1;i<=n;++i){
        que[i].clear();
    }
}
int find(int x){
    return f[x]==-1 ?x:f[x]=find(f[x]);
}
void Union(int u,int v){
    int t1=find(u);
    int t2=find(v);
    if(t1!=t2) f[t2]=t1;
}
void LCA(int u){
    ancestor[u]=u;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(vis[v]) continue;
        LCA(v);
        Union(u,v);
        ancestor[find(v)]=u;//可省略
    }
    vis[u]=true;
    int sz=que[u].size();
    for(int i=0;i<sz;++i){
        int v=que[u][i];
        if(vis[v]){
            printf("%d\n",ancestor[find(v)]);//可替换find(v)
        }
    }
}
int main()
{
//freopen("in.txt","r",stdin);
    int T,u,v,w;
    cin>>T;
    while(T--){
        scanf("%d",&n);
        Init();
        for(int i=0;i<n-1;++i){
            scanf("%d%d",&u,&v);
            if(u!=v){
                addedge(u,v);
                in[v]++;
            }
        }
        scanf("%d%d",&u,&v);
        que[u].push_back(v);
        que[v].push_back(u);
        for(int i=1;i<=n;++i){
            if(in[i]==0){
                LCA(i);
                break;
            }
        }
    }
    return 0;
}
时间: 2024-10-11 04:15:56

POJ1330Nearest Common Ancestors——最近公共祖先的相关文章

POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

A - 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 the figu

poj1330 Nearest Common Ancestors (最近公共祖先)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24236   Accepted: 12619 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(最近公共祖先)

传送门:http://poj.org/problem?id=1330 题意:很裸的最近公共祖先,看题就知道…模板题. 代码: /* *********************************************** Author :Torrance_ZHANG Created Time :2016/4/29 22:11:34 File Name :ceshi2.cpp ************************************************ */ #inclu

POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

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,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if n

POJ 1330 Nearest Common Ancestors 最近公共祖先

LCA模板题,用倍增法去写 首先把每一个节点的向上\(2^i (i \in \mathbb N)\)个祖先给枚举出来 再把要求公共祖先的两个节点拉到同一深度 向上不断利用倍增一起跳跃同样层数到他们各自的非公共祖先的祖先节点 最后他们一起到达共同祖先节点的子节点,再同时向上走一位即可 在这个过程中,同时维护cost即可 #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long

POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA 1 public int query(Node t, Node u, Node v) { 2 int left = u.value; 3 int right = v.value; 4 5 //二叉查找树内,如果左结点大于右结点,不对,交换 6 if (left > right) { 7 int temp = left; 8 left = right; 9 right = temp; 10 } 11 12 whi

kyeremal-poj1330-Nearest Common Ancestors-最近公共祖先

poj1330-Nearest Common Ancestors 训练暑期课面向全球招生,欲报名从速! Language: Default Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20814   Accepted: 10960 Description A rooted tree is a well-known data structure in computer s

poj1330Nearest Common Ancestors以及讲解倍增法求lca

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20487   Accepted: 10784 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

Poj1330Nearest Common Ancestors LCA

题意给一颗树,再给一个查询两点之间的最近公共祖先. #include<iostream> #include<cstdio> #include<cstring> #include<map> #include<vector> using namespace std; const int maxn = 111111; struct edge { int to; int next; }e[maxn * 10]; int len; int head[max