Closest Common Ancestors

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)

The data set 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. The tree description is followed by a list of pairs of vertices, in the form:

nr_of_pairs
(u v) (x y) …

The input contents several data sets (at least one).

Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

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:

the program input and output is:

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)

Output

2:1
5:5
#include<stdio.h>
#include
#include
#include
using namespace std;
const int N=100000;
long long bit[N];
int depth[N],f[N][30];
vector g[N];
int in[N],b[N];
void init()//进行标记
{
bit[0]=1;
for(int i=1;i<=29;i++)
bit[i]=bit[i-1]*2;
}
void dfs(int u,int pre)//把图中的点都与他的祖先联系起来
{
depth[u]=depth[pre]+1;
f[u][0]=pre;//父节点
for(int i=1;i<=29;i++)
f[u][i]=f[f[u][i-1]][i-1];//父亲的父亲是爷爷
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(v==pre) continue;
dfs(v,u);
}
}

int lca(int a,int b)//寻找a,b的最近公共祖先
{
if(depth[a]<depth[b]) swap(a,b);
int dif=depth[a]-depth[b];//两点深度差
for(int i=29;i>=0;i–)
{
if(dif>=bit[i])
{
a=f[a][i];
dif-=bit[i];
}//到达同一深度
}
if(a==b) return a;//如果相等返回值
for(int i=29;i>=0;i–)//不相等一块查询
{
if(depth[a]>=bit[i]&&f[a][i]!=f[b][i])
{
a=f[a][i];
b=f[b][i];
}
}
return f[a][0];

}

int main()
{
int n;

while(scanf("%d",&n)!=EOF)
{
	for(int i=1;i<=n;i++)
		g[i].clear();
	memset(f,0,sizeof(f));
	memset(b,0,sizeof(b));
	memset(depth,0,sizeof(depth));
	memset(in,0,sizeof(in));//初始化 
	init();
	int m=n;
	int num,t,v;
	for(int i=1;i<=n;i++)
	{
		scanf("%d:(%d)",&num,&t);
		for(int j=1;j<=t;j++)
		{
			scanf(" %d",&v);//输入 
			in[v]++;//计算点的入度 
			g[num].push_back(v);
			g[v].push_back(num);//两点联系 
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(in[i]==0)
		{
			dfs(i,0);//把所有点的祖先查找 
			break;
		}
	}
	int flag;
	scanf(" %d",&flag);
	for(int i=1;i<=flag;i++)
	{
		scanf(" (%d,%d)",&num,&v);
		int q=lca(num,v);
		b[q]++;//如果为所求加1 
	}
	for(int i=1;i<=n;i++)
	{
		if(b[i])
		{
			printf("%d:%d\n",i,b[i]);//打印出符合要求的点 
		}
	}	
}
return 0;

}
模板!!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值