poj 1470 Closest Common Ancestors LCA

时间:2021-11-16 09:52:39

题目链接:http://poj.org/problem?id=1470

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)

题目描述:给出一棵树的节点之间的信息,然后给出一些询问,每次询问是求出两个节点的LCA。统计每个节点被作为询问中LCA的次数并输出。

算法分析:LCA离线算法,从算法思想和思维上并不难,可以说是一道模板题,但这道题的输入有点麻烦,处理一下输入就可以了。

另外ZOJ1141也是这道题,我在ZOJ上面AC了

poj 1470 Closest Common Ancestors LCA

但是在POJ上WA了,最后处理输入之后AC了。

看看程序想了想,应该POJ上面形如5:(5)这样的输入中有可能不是冒号和括号,而是其他的符号吧。

poj 1470 Closest Common Ancestors LCA

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int max_log_maxn=; int n,root;
vector<int> G[maxn];
int father[max_log_maxn][maxn],d[maxn];
int vis[maxn]; void dfs(int u,int p,int depth)
{
father[][u]=p;
d[u]=depth;
int num=G[u].size();
for (int i= ;i<num ;i++)
{
int v=G[u][i];
if (v != p) dfs(v,u,depth+);
}
} void init()
{
dfs(root,-,);
for (int k= ;k+<max_log_maxn ;k++)
{
for (int i= ;i<=n ;i++)
{
if (father[k][i]<) father[k+][i]=-;
else father[k+][i]=father[k][father[k][i] ];
}
father[k][root]=root;
}
} int LCA(int u,int v)
{
if (d[u]>d[v]) swap(u,v);
for (int k= ;k<max_log_maxn ;k++)
{
if ((d[v]-d[u])>>k & )
v=father[k][v];
}
if (u==v) return u;
for (int k=max_log_maxn- ;k>= ;k--)
{
if (father[k][u] != father[k][v])
{
u=father[k][u];
v=father[k][v];
}
}
return father[][u];
} int main()
{
while (scanf("%d",&n)!=EOF)
{
int u,num,v;
for (int i= ;i<=n ;i++) G[i].clear();
memset(vis,,sizeof(vis));
char str[];
memset(str,,sizeof(str));
char ch[],ch2[],ch3[];
for (int i= ;i<n ;i++)
{
int u=,num=;
scanf("%d%1s%1s%d%1s",&u,ch,ch2,&num,ch3);
// scanf("%s",str);
// int u=0,num=0;
// int len=strlen(str);
// int j=0;
// for (j=0 ;j<len && str[j]!=':';j++) u=u*10+str[j]-'0';
// for (j=j+2 ;j<len && str[j]!=')';j++) num=num*10+str[j]-'0';
while (num--)
{
scanf("%d",&v);
G[u].push_back(v);
vis[v]=;
}
}
for (int i= ;i<=n ;i++) if (!vis[i]) {root=i;break; }
init();
memset(vis,,sizeof(vis));
scanf("%d",&num);
memset(str,,sizeof(str));
//getchar();
for (int j= ;j<num ;j++)
{
scanf("%1s%d%d%1s",ch,&u,&v,ch2);
// gets(str);
// int u=0,v=0;
// int len=strlen(str);
// int i=0;
// for (i=1 ;i<len && str[i]>='0' && str[i]<='9' ;i++)
// u=u*10+str[i]-'0';
// while (i<len)
// {
// if (str[i]>='0' && str[i]<='9') break;
// i ++ ;
// }
// while (i<len)
// {
// if (str[i]==')') break;
// v=v*10+str[i]-'0';
// i ++ ;
// }
int ans=LCA(u,v);
vis[ans]++;
}
for (int i= ;i<=n ;i++)
if (vis[i]) printf("%d:%d\n",i,vis[i]);
}
return ;
}