Closest Common Ancestors(POJ 1470 最近公共祖先)

链式前向星

题目链接

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

Hint

Huge input, scanf is recommended.

Source

大致题意给你一个图,n个顶点,m次询问

如果对于每次询问就进行一边LCA查询的话,不用想一定会时间超限。

把要查询的数对存下来,一次查询记录下所有的公共祖先;

#include <iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<stack>
#include<climits>
#include<map>
#include<ctime>
using namespace std;
#define eps 1e-10
#define pi acos(-1)
#define e exp(1)
#define INF 0x3f3f3f3f
#define MAX INT_MAX
typedef long long LL;
const int  MAX1 = 1e4+50;
struct node{
    int to,next;
}edge[MAX1];
struct A{
    int x,y;
}cin1[MAX1];
int father[MAX1],head[MAX1];
vector <int>hh[MAX1],hhh[MAX1];
bool is_root[MAX1],vis[MAX1];
int n,m;
int cnt,length,res;
int root,cx,cy,ans[MAX1];
void add(int x,int y)
{
    edge[cnt].to=y;
    edge[cnt].next=head[x];
    head[x]=cnt++;
}
void init()
{

    cnt=0;

    for(int i=1;i<=n;i++)
        father[i]=i;
    memset(is_root,true,sizeof(is_root));
    memset(vis,false,sizeof(vis));
    memset(head,-1,sizeof(head));
    memset(hh,0,sizeof(hh));
    memset(hhh,0,sizeof(hhh));
    for(int i=1;i<length;i++)
    {
        is_root[cin1[i].y]=false;
        add(cin1[i].x,cin1[i].y);
    }
    for(int i=1;i<=n;i++)
        if(is_root[i])
            root=i;
}
int Find(int x)
{
    if(x!=father[x])
        father[x]=Find(father[x]);
    return father[x];
}
void Join(int x1,int x2)
{
    int root1=Find(x1);
    int root2=Find(x2);
    if(root1!=root2)
        father[root2]=root1;
}
void LCA(int u)
{
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        LCA(v);
        Join(u,v);
        vis[v]=true;
    }
    if(hh[u].size())
    {
        for(int i=0;i<hh[u].size();i++)
            if(vis[hh[u][i]])
            ans[Find(hh[u][i])]++;
    }

    if(hhh[u].size())
    {
        for(int i=0;i<hhh[u].size();i++)
            if(vis[hhh[u][i]])
            ans[Find(hhh[u][i])]++;
    }

}
void solve()
{
    char l,ll;
    scanf("%d",&m);
    while(m--)
    {
        scanf(" %c%d%d%c",&l,&cx,&cy,&ll);
        hh[cx].push_back(cy);
        hhh[cy].push_back(cx);
    }
    LCA(root);
}
int main()
{
    while(cin>>n)
    {
        length=1;
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++)
        {
            int x,j;
            scanf("%d:(%d)",&x,&j);
            while(j--)
            {
                int y;
                scanf("%d",&y);
                cin1[length].x=x;
                cin1[length++].y=y;
            }
        }
        init();
        solve();
        for(int i=1;i<=n;i++)
            if(ans[i])
        cout<<i<<":"<<ans[i]<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值