poj-1470 Closest Common Ancestors(最近公共祖先lca+Tarjan离线)

该博客介绍如何利用Tarjan算法处理输入的树结构数据,找到每对节点的最近公共祖先,并统计每个节点作为最近公共祖先的次数。输入包含树的节点信息和查询对,输出为每个节点作为祖先的频率,按节点编号升序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)

Input

The data set, which is read from a the std input, starts with the tree description, in the form: 

nr_of_vertices 
vertex:(nr_of_successors) successor1 successor2 ... successorn 
...
where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form: 
nr_of_pairs 
(u v) (x y) ... 

The input file contents several data sets (at least one). 
Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

Output

For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times 
For example, for the following tree: 

Sample Input

5
5:(3) 1 4 2
1:(0)
4:(0)
2:(1) 3
3:(0)
6
(1 5) (1 4) (4 2)
      (2 3)
(1 3) (4 3)

Sample Output

2:1
5:5

题意:首先给了n个点,然后接下来n行描述了每一个点的儿子信息,接下来有q组查询,最后让你统计一下每个顶点作为最近公共祖先的次数

思路:Tarjan离线 记录每个节点最为最近公共祖先的次数

/*

*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <map>
#include <vector>
#include <set>
#include <bitset>
#include <stack>
#define ull unsigned long long
#define mems(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const ll mod=1000000007;
const double pi=acos(-1);
const int N=1004;
struct node//建树连边
{
    int u,v,next;
} g[N*N];
struct nod
{
    int u,v,next;
} G[N*N];
int n,m,ans;
int head[N],hd[N];
int tot;
int res[N];//记录次数
int vis[N],pre[N],fa[N];
void init(int n)
{
    tot=0;
    memset(vis,0,sizeof(vis));
    mems(fa,-1);
    mems(res,0);
    memset(head,-1,sizeof(head));
    memset(hd,-1,sizeof(hd));
//    memset(g,0,sizeof(g));
//    memset(G,0,sizeof(G));//会内存超限?
    for(int i=1;i<=n;i++)
        pre[i]=i;
}
void addg(int u,int v)
{
    g[tot].u=u;
    g[tot].v=v;
    g[tot].next=head[u];
    head[u]=tot++;
}
void addG(int u,int v)
{
    G[tot].u=u;
    G[tot].v=v;
    G[tot].next=hd[u];
    hd[u]=tot++;
}
int Find(int x)
{
    if(x==pre[x])
        return x;
    else
        return pre[x]=Find(pre[x]);
}
void Lca(int u,int fa)
{

    for(int i=head[u]; ~i; i=g[i].next)
    {
        int v=g[i].v;
        if(v==fa)
            continue;
        if(!vis[v])
        {
            Lca(v,u);
            pre[v]=u;
        }
    }vis[u]=1;
    for(int i=hd[u]; ~i; i=G[i].next)
    {
        int v=G[i].v;
        if(vis[v])
        {
            res[Find(v)]++;//最近的公共祖先Find(v)
        }
    }
}
int main()
{
    int n;

    while(~scanf("%d",&n))
    {
        init(n);
        int a,b,c;
        for(int i=0; i<n; i++)
        {
            scanf("%d:(%d)",&a,&b);
            for(int j=0; j<b; j++)
            {
                scanf("%d",&c);
                addg(a,c);
                addg(c,a);
                fa[c]=a;
            }
        }
        int root=1;//寻找根节点
        while(fa[root]!=-1)
            root=fa[root];
        int m;
        scanf("%d",&m);
        tot=0;
        for(int i=0; i<m; i++)
        {
            scanf(" (%d %d)",&a,&b);
            addG(a,b);
            addG(b,a);
        }
        Lca(root,root);
        for(int i=1; i<=n; i++)
            if(res[i])
                printf("%d:%d\n",i,res[i]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值