poj——1470 Closest Common Ancestors

时间:2020-12-05 09:52:41
                      Closest Common Ancestors
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 20804   Accepted: 6608

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: 
poj——1470  Closest Common Ancestors

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

 
代码:
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 10100
using namespace std;
char ch;
vector<int>vec[N],que[N];
int t,s,n,m,x,y,num,qx[N],qy[N],fa[N],dad[N],ans[N],root,ans1[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
int find(int x)
{
    if(fa[x]==x) return x;
    fa[x]=find(fa[x]);
    return fa[x];
}
int tarjan(int x)
{
    fa[x]=x;
    ;i<vec[x].size();i++)
     if(vec[x][i]!=dad[x])
      dad[vec[x][i]]=x,tarjan(vec[x][i]);
    ;i<que[x].size();i++)
     if(dad[y=qx[que[x][i]]^qy[que[x][i]]^x])
      ans1[que[x][i]]=find(y);
    fa[x]=dad[x];
}
void begin()
{
    ;i<=n;i++)
     vec[i].clear(),que[i].clear();
    memset(fa,,sizeof(fa));
    memset(ans,,sizeof(ans));
    memset(dad,,sizeof(dad));
    memset(ans1,,sizeof(ans1));
}
int main()
{
    while(scanf("%d",&t)!=EOF)
    {
        s=t;begin();
        while(t--)
        {
            x=read();
            n=read();
            ;i<=n;i++)
            {
                y=read();fa[y]=x;
                vec[x].push_back(y);
                vec[y].push_back(x);
             }
        }
        ;i<=s;i++)
         if(!fa[i]) root=i;
        memset(fa,,sizeof(fa));
        memset(ans,,sizeof(ans));
        m=read();
        ;i<=m;i++)
        {
            qx[i]=read(),qy[i]=read();
            que[qx[i]].push_back(i);
            que[qy[i]].push_back(i);
        }
        tarjan(root);
        ;i<=m;i++)
          ans[ans1[i]]++;
        ;i<=s;i++)
         if(ans[i]) printf("%d:%d\n",i,ans[i]);
    }
    ;
}

tarjan暴空间、、、

O(≧口≦)O气死了,蒟蒻表示以后再也不用tarjan了!!!!!!!!!!!!

#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 910
using namespace std;
vector<int>vec[N];
int n,m,s,x,y,dad[N],fa[N],top[N],deep[N],size[N],ans[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
int lca(int x,int y)
{
    for(;top[x]!=top[y];)
    {
        if(deep[top[x]]<deep[top[y]])
         swap(x,y);
        x=fa[x];
    }
    if(deep[x]>deep[y])
     swap(x,y);
    return x;
}
int dfs(int x)
{
    size[x]=;
    deep[x]=deep[fa[x]]+;
    ;i<vec[x].size();i++)
     if(vec[x][i]!=fa[x])
    {
        fa[vec[x][i]]=x;
        dfs(vec[x][i]);
        size[x]+=size[vec[x][i]];
    }
}
int dfs1(int x)
{
    ;
    if(!top[x]) top[x]=x;
    ;i<vec[x].size();i++)
     if(vec[x][i]!=fa[x]&&size[t]<size[vec[x][i]])
      t=vec[x][i];
    if(t) top[t]=top[x],dfs1(t);
    ;i<vec[x].size();i++)
     if(vec[x][i]!=fa[x]&&vec[x][i]!=t)
      dfs1(vec[x][i]);
}
int begin()
{
    ;i<=n;i++)
      vec[i].clear();
    memset(fa,,sizeof(fa));
    memset(top,,sizeof(top));
    memset(ans,,sizeof(ans));
    memset(dad,,sizeof(dad));
    memset(deep,,sizeof(deep));
    memset(size,,sizeof(size));
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        s=n;begin();
        while(n--)
        {
            x=read();m=read();
            ;i<=m;i++)
            {
                y=read();dad[y]=x;
                vec[x].push_back(y);
                vec[y].push_back(x);
            }
        }
        ;i<=s;i++)
         if(!dad[i])
          {dfs(i);dfs1(i);break;}
        m=read();
        ;i<=m;i++)
        {
            x=read(),y=read();
            ans[lca(x,y)]++;
        }
        ;i<=s;i++)
         if(ans[i]) printf("%d:%d\n",i,ans[i]);
    }
    ;
}