zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    丑数系列
    452. 用最少数量的箭引爆气球
    406. 根据身高重建队列
    763. 划分字母区间
    所有二叉树题目记录
    二叉树前中后序遍历非递归(迭代)解法
    二叉树的层序遍历题目汇总
    442. 数组中重复的数据&&448. 找到所有数组中消失的数字
    225. 用队列实现栈(Easy)
    使用ClosedXML读写excel
  • 原文地址:https://www.cnblogs.com/gtarcoder/p/5540062.html
Copyright © 2011-2022 走看看