Closest Common Ancestors【POJ1470】——LCA

来源:互联网 发布:solus linux 编辑:程序博客网 时间:2024/06/13 21:32

Time Limit: 2000MS Memory Limit: 10000K

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

Southeastern Europe 2000

求最近公共祖先的裸题

大神的博客

Tarjan算法

#include <cstdio>#include <iostream>#include <vector>#include <cstring>#include <algorithm>#include <algorithm>using namespace std;const int Max = 1100;int n,m;vector<int>E[Max];vector<int>S[Max];int pre[Max];bool vis[Max];int num[Max];int Du[Max];void Init(){    for(int i  =0 ;i<Max;i++)    {        E[i].clear(); S[i].clear();        pre[i] = i; vis[i] = false;        num[i] = 0; Du[i] = 0 ;    }}int find(int x){    return pre[x]==x?x:pre[x] = find(pre[x]);}void dfs(int u){    for(int i = 0 ;i<E[u].size();i++)    {        dfs(E[u][i]);        pre[E[u][i]] = u;    }    vis[u] = true;    for(int i = 0;i<S[u].size();i++)    {        if(vis[S[u][i]])        {            num[find(S[u][i])]++;        }    }}int main(){    int u,v;    while(~scanf("%d",&n))    {        Init();        for(int i = 0;i<n;i++)        {            scanf("%d:(%d)",&u,&m);            for(int j =  0;j<m;j++)            {                scanf("%d",&v);                Du[v]++;                E[u].push_back(v);            }        }        scanf("%d",&m);        for(int  i = 0;i<m;i++)        {            while(getchar()!='(');            scanf("%d %d",&u,&v);            S[u].push_back(v);            S[v].push_back(u);        }        while(getchar()!=')');        for(int i = 1;i<=n;i++)        {            if(!Du[i])            {                dfs(i);            }        }        for(int i = 1;i<=n;i++)        {            if(num[i]) printf("%d:%d\n",i,num[i]);        }    }    return 0;

RMQ算法

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <vector>#include <algorithm>using namespace std;const int Max = 2100;int RMQ[Max][16];int e[Max],r[Max],d[Max];int Du[Max];bool vis[Max];int num[Max];vector<int>E[Max];void Init(){    for(int i = 0;i<Max;i++)    {        E[i].clear();        Du[i] = 0;        num[i] = 0;        vis[i] = false;    }}int n,m,tot;void dfs(int u,int dep){    vis[u] = true;    e[++tot] = u;    d[u] = tot;    r[tot] = dep;    for(int i = 0;i<(int)E[u].size();i++)     {        if(!vis[E[u][i]])        {            dfs(E[u][i],dep+1);            e[++tot] = u;            r[tot] = dep;        }    }}void RMQ_Min(){    for(int i = 1;i<=tot;i++)    {        RMQ[i][0] = i;    }    for(int i = 1;i<16;i++)    {        for(int j = 1;j<=tot;j++)        {            if(j+(1<<i)-1<=tot)            {                RMQ[j][i] = r[RMQ[j][i-1]]<r[RMQ[j+(1<<(i-1))][i-1]]?RMQ[j][i-1]:RMQ[j+(1<<(i-1))][i-1];            }        }    }}int Find(int u,int v){    if(d[u]>d[v])        swap(u,v);    int L = d[u],R = d[v];    int k = (int)(log(R-L+1)/log(2.0));    int ans =r[RMQ[L][k]]<r[RMQ[R-(1<<k)+1][k]]?RMQ[L][k]:RMQ[R-(1<<k)+1][k];    return e[ans];}int main(){    int u,v;    while(~scanf("%d",&n))    {        Init();        for(int i= 0 ;i<n;i++)        {            scanf("%d:(%d)",&u,&m);            for(int j = 0;j<m;j++)            {                scanf("%d",&v);                Du[v]++;                E[u].push_back(v);            }        }        tot = 0;        for(int i = 1;i<=n;i++)        {            if(!Du[i])             {                dfs(i,1);                break;            }        }        RMQ_Min();        scanf("%d",&m);        while(m--)        {            while(getchar()!='(');            scanf("%d %d",&u,&v);            int ans = Find(u,v);            num[ans]++;        }        while(getchar()!=')');        for(int  i =1;i<=n;i++)            if(num[i]) printf("%d:%d\n",i,num[i]);    }    return  0;}
1 0
原创粉丝点击