SPOJ LCA Lowest Common Ancestor

原题链接

LCA - Lowest Common Ancestor

no tags 

A tree is an undirected graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. - Wikipedia 

The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with N nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself). - Wikipedia

Your task in this problem is to find the LCA of any two given nodes v and w in a given tree T.

For example the LCA of nodes 9 and 12 in this tree is the node number 3.

Input

The first line of input will be the number of test cases. Each test case will start with a number N the number of nodes in the tree, 1 <= N <= 1,000. Nodes are numbered from 1 to N. The next N lines each one will start with a number M the number of child nodes of the Nth node, 0 <= M <= 999 followed by M numbers the child nodes of the Nth node. The next line will be a number Q the number of queries you have to answer for the given tree T, 1 <= Q <= 1000. The next Q lines each one will have two number v and w in which you have to find the LCA of v and w in T, 1 <= v, w <= 1,000.

Input will guarantee that there is only one root and no cycles.

Output

For each test case print Q + 1 lines, The first line will have “Case C:” without quotes where C is the case number starting with 1. The next Q lines should be the LCA of the given v and w respectively.

Example

Input:
1
7
3 2 3 4
0
3 5 6 7
0
0
0
0
2
5 7
2 7

Output:
Case 1:
3
1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define INF 1e9
#define MOD 1000000007
#define maxn 1005
using namespace std;
typedef long long ll;

vector<int> v[maxn];
int vis[maxn], d[maxn<<2], p[maxn<<2], c[maxn], cnt;
int dp[maxn<<2][20];
void dfs(int i, int dis, int f){
	
	c[i] = cnt;
	d[cnt] = dis;
	p[cnt++] = i;
	for(int j = 0; j < v[i].size(); j++){
		int h = v[i][j];
		if(h != f){
			dfs(h, dis+1, i);
			d[cnt] = dis;
			p[cnt++] = i;
		}
	}
}
void RMQ(){
	
	for(int i = 0; i < cnt; i++)
	  dp[i][0] = p[i];
	for(int i = 1; (1<<i) <= cnt; i++)
	  for(int j = 0; j+(1<<i) <= cnt; j++){
	  	int k1 = dp[j][i-1], k2 = dp[j+(1<<(i-1))][i-1];
	  	int ans = k2;
	  	if(d[c[k1]] < d[c[k2]])
	  	 ans = k1;
	  	dp[j][i] = ans;
	  }
	  
}
int main(){
	
//	freopen("in.txt", "r", stdin);
	int t, cas = 0;
	
	scanf("%d", &t);
	while(t--){
		
		cnt = 0;
		int n, m, a, b;
		scanf("%d", &n);
		memset(vis, 0, sizeof(vis));
		for(int i = 1; i <= n; i++)
		  v[i].clear();
		for(int i = 1; i <= n; i++){
			scanf("%d", &m);
			for(int j = 0; j < m; j++){
				scanf("%d", &a);
				vis[a] = 1;
				v[i].push_back(a);
			}
		}
		for(int i = 1; i <= n; i++){
			if(vis[i] == 0){
				dfs(i, 0, -1);
				break;
			}
		}
		RMQ();
		int q;
		scanf("%d", &q);
		printf("Case %d:\n", ++cas);
		while(q--){
			scanf("%d%d", &a, &b);
			int p1 = c[a], p2 = c[b];
			if(p1 > p2)
			 swap(p1, p2);
			int r = 0;
			while(1<<(r+1) <= p2-p1+1)
			 r++; 
			int k1 = dp[p1][r], k2 = dp[p2-(1<<r)+1][r];
			if(d[c[k1]] < d[c[k2]])
			  printf("%d\n", k1);
			else
			 printf("%d\n", k2);
		}
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值