首页 > 其他 > 详细

POJ(有向图求LCA)

时间:2016-01-30 18:14:42      阅读:206      评论:0      收藏:0      [点我收藏+]
Closest Common Ancestors
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 18013   Accepted: 5774

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
有向图求LCA,注意输入的处理。
有向图只有一个树根,利用并查集求树根。无向图中可选任意一点作树根,在dfs是比有向图多一个条件,详情见代码。
#include"cstdio"
#include"cstring"
#include"vector"
using namespace std;
const int MAXN=1005;
int V;
vector<int> G[MAXN];
int par[MAXN];
void prep()
{
    for(int i=0;i<=MAXN;i++)
    {
        par[i]=i;
    }
}
int fnd(int x)
{
    if(par[x]==x)
        return x;
    return par[x]=fnd(par[x]);
}
void unite(int father,int son)
{
    par[son]=fnd(father);
}
int fa[MAXN],dep[MAXN];
void dfs(int u,int father,int d)
{
    fa[u]=father,dep[u]=d;
    for(int i=0;i<G[u].size();i++)
         dfs(G[u][i],u,d+1);//在无向图中 需要加上 if(G[u][i]!=father) 
}
int lca[MAXN];
int LCA(int u,int v)
{
    while(dep[u]>dep[v])    u=fa[u];
    while(dep[v]>dep[u])    v=fa[v];
    while(u!=v)
    {
        u=fa[u];
        v=fa[v];
    }
    return u;
}
int main()
{
    while(scanf("%d",&V)!=EOF)
    {
        for(int i=0;i<=V;i++)    G[i].clear();
        prep();
        memset(lca,0,sizeof(lca));
        for(int i=0;i<V;i++)
        {
            int u,t;
            scanf("%d:(%d)",&u,&t);
            while(t--)
            {
                int v;
                scanf("%d",&v);
                G[u].push_back(v);
                unite(u,v);
            }
        }
        int root=fnd(1);
        dfs(root,-1,0);
        int Q;
        scanf("%d",&Q);
        while(true)
        {
            char ch=getchar();
            if(ch==()
            {
                int u,v;
                scanf("%d %d",&u,&v);
                int a=LCA(u,v);
                lca[a]++;
                Q--;
                getchar();//不能丢,有开就有闭 
            }
            if(Q==0)    break;
        }
        for(int i=0;i<=V;i++)
        {
            if(lca[i]!=0)    printf("%d:%d\n",i,lca[i]);
        }
    }
    return 0;
}

 

POJ(有向图求LCA)

原文:http://www.cnblogs.com/program-ccc/p/5171158.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!