hiho_1062_最近公共祖先

题目大意

    给出一棵家谱树,树中的节点都有一个名字,保证每个名字都是唯一的,然后进行若干次查询,找出两个名字的最近公共祖先。

分析

    数据量较小,对于每次查询都进行如下操作: 
先找出person1到达根节点的路径path,然后再从person2开始向上,每经过一个节点都查询一下该节点是否在path中出现,如果出现,则为该节点。

实现
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
using namespace std;

unordered_set<string> path;	//存储从一个节点到根节点的路径
unordered_map<string, string> pre; //存储图的结构,通过 <key, value> key为子节点,value为 父节点

int main(){
	int n, m;
	string father, son, person1, person2;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> father >> son;
		pre[son] = father;
	}
	cin >> m;
	for (int i = 0; i < m; i++){
		cin >> person1 >> person2;
		
		path.clear();
		//存储从person1 到根节点的路径
		while (pre.find(person1) != pre.end()){
			path.insert(person1);
			person1 = pre[person1];
		}
		path.insert(person1);

		//进行查找
		bool find = false;
		while (pre.find(person2) != pre.end()){
			person2 = pre[person2];
			if (path.find(person2) != path.end()){
				find = true;
				cout << person2 << endl;
				break;
			}			
		}
		//person2此时为根节点,需要注意(不要忘记查找)!
		if (!find &&path.find(person2) != path.end()){
			cout << person2 << endl;
		}
		if (!find)
			cout << -1 << endl;
		
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/gtarcoder/p/5540062.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值