UVa Problem 115 - Climbing Trees

// UVa Problem 115 - Climbing Trees   
  1. // Verdict: Accepted   
  2. // Submission Date: 2011-11-27   
  3. // UVa Run Time: 0.008s   
  4. //   
  5. // 版权所有(C)2011,邱秋。metaphysis # yeah dot net   
  6. //   
  7. // [解题方法]   
  8. // 模拟题。由于只需要保存父子关系,不一定要构建树。虽然方法是直接的,但是有点繁琐。   
  9.   
  10. #include <iostream>   
  11. #include <map>   
  12.   
  13. using namespace std;  
  14.   
  15. #define MAXN 310   
  16. #define DUMMY (-1)   
  17.   
  18. int parent[MAXN];  
  19. map < string, int > genealogy;  
  20.       
  21. bool isAncestor(string fName, string sName)  
  22. {  
  23.     int nFirst = genealogy[fName];  
  24.     int nSecond = genealogy[sName];  
  25.     int depth = 0;  
  26.     bool found = false;  
  27.   
  28.     while (parent[nSecond] != DUMMY)  
  29.     {  
  30.         if (parent[nSecond] == nFirst)  
  31.         {  
  32.             found = true;  
  33.             break;  
  34.         }  
  35.         nSecond = parent[nSecond];  
  36.         depth++;  
  37.     }  
  38.       
  39.     if (found)  
  40.     {  
  41.         if (depth == 0)  
  42.             cout << "parent\n";  
  43.         else if (depth == 1)  
  44.             cout << "grand parent\n";  
  45.         else  
  46.         {  
  47.             for (int i = 1; i < depth; i++)  
  48.                 cout << "great ";  
  49.             cout << "grand parent\n";       
  50.         }  
  51.   
  52.         return true;  
  53.     }  
  54.       
  55.     return false;  
  56. }  
  57.   
  58. bool isDescendent(string fName, string sName)  
  59. {  
  60.     int nFirst = genealogy[fName];  
  61.     int nSecond = genealogy[sName];  
  62.     int depth = 0;  
  63.     bool found = false;  
  64.     while (parent[nFirst] != DUMMY)  
  65.     {  
  66.         if (parent[nFirst] == nSecond)  
  67.         {  
  68.             found = true;  
  69.             break;  
  70.         }  
  71.         nFirst = parent[nFirst];  
  72.         depth++;  
  73.     }  
  74.       
  75.     if (found)  
  76.     {  
  77.         if (depth == 0)  
  78.             cout << "child\n";  
  79.         else if (depth == 1)  
  80.             cout << "grand child\n";  
  81.         else  
  82.         {  
  83.             for (int i = 1; i < depth; i++)  
  84.                 cout << "great ";  
  85.             cout << "grand child\n";        
  86.         }  
  87.   
  88.         return true;  
  89.     }  
  90.       
  91.     return false;  
  92. }  
  93.   
  94. bool isInTree(string fName, string sName)  
  95. {  
  96.     if (genealogy.find(fName) == genealogy.end() ||  
  97.             genealogy.find(sName) == genealogy.end())  
  98.     {  
  99.         cout << "no relation\n";  
  100.         return false;  
  101.     }  
  102.       
  103.     return true;  
  104. }  
  105.   
  106. bool isInSameTree(string fName, string sName)  
  107. {  
  108.     int nFirst = genealogy[fName];  
  109.     int nSecond = genealogy[sName];  
  110.   
  111.     while (parent[nFirst] != DUMMY)  
  112.         nFirst = parent[nFirst];  
  113.     while (parent[nSecond] != DUMMY)  
  114.         nSecond = parent[nSecond];  
  115.   
  116.     if (nFirst != nSecond)  
  117.     {  
  118.         cout << "no relation\n";  
  119.         return false;         
  120.     }  
  121.       
  122.     return true;  
  123. }  
  124.   
  125. bool isSibling(string fName, string sName)  
  126. {  
  127.     int nFirst = genealogy[fName];  
  128.     int nSecond = genealogy[sName];   
  129.     if (parent[nFirst] != DUMMY && parent[nSecond] != DUMMY &&  
  130.         parent[nFirst] == parent[nSecond])  
  131.     {  
  132.         cout << "sibling\n";  
  133.         return true;  
  134.     }  
  135.       
  136.     return false;  
  137. }  
  138.   
  139. bool isCousin(string fName, string sName)  
  140. {  
  141.     int nFirst = genealogy[fName];  
  142.     int nSecond = genealogy[sName];  
  143.     int n = 0, m = 0;  
  144.   
  145.     while (parent[nFirst] != DUMMY)  
  146.     {  
  147.         nFirst = parent[nFirst];  
  148.         n++;  
  149.     }  
  150.     while (parent[nSecond] != DUMMY)  
  151.     {  
  152.         nSecond = parent[nSecond];  
  153.         m++;  
  154.     }  
  155.   
  156.     nFirst = genealogy[fName];  
  157.     nSecond = genealogy[sName];  
  158.     int backN = n, backM = m;  
  159.   
  160.     if (n > m)  
  161.     {  
  162.         while (n > m)  
  163.         {  
  164.             nFirst = parent[nFirst];  
  165.             n--;  
  166.         }  
  167.         while (parent[nFirst] != parent[nSecond])  
  168.         {  
  169.             nFirst = parent[nFirst];  
  170.             nSecond = parent[nSecond];  
  171.             n--;  
  172.         }  
  173.           
  174.         backN -= n;  
  175.         backM -= n;  
  176.           
  177.         cout << backM << " cousin removed " << (backN - backM) << "\n";  
  178.     }  
  179.     else if (m > n)  
  180.     {  
  181.         while (m > n)  
  182.         {  
  183.             nSecond = parent[nSecond];  
  184.             m--;  
  185.         }  
  186.         while (parent[nFirst] != parent[nSecond])  
  187.         {  
  188.             nFirst = parent[nFirst];  
  189.             nSecond = parent[nSecond];  
  190.             m--;  
  191.         }  
  192.           
  193.         backN -= m;  
  194.         backM -= m;  
  195.           
  196.         cout << backN << " cousin removed " << (backM - backN) << "\n";   
  197.     }  
  198.     else  
  199.     {  
  200.         while (parent[nFirst] != parent[nSecond])  
  201.         {  
  202.             nFirst = parent[nFirst];  
  203.             nSecond = parent[nSecond];  
  204.             n--;  
  205.             m--;  
  206.         }  
  207.           
  208.         cout << (backN - n) << " cousin\n";  
  209.     }  
  210. }  
  211.   
  212. int main (int argc, char const* argv[])  
  213. {  
  214.     int nNames = 0;  
  215.     string allNames[MAXN];  
  216.     string childName, parentName;  
  217.   
  218.     for (int i = 0; i < MAXN; i++)  
  219.         parent[i] = DUMMY;  
  220.   
  221.     while (cin >> childName >> parentName)  
  222.     {  
  223.         if (childName == "no.child" && parentName == "no.parent")  
  224.             break;  
  225.         if (genealogy.find(childName) == genealogy.end())  
  226.         {  
  227.             genealogy[childName] = nNames;  
  228.             allNames[nNames++] = childName;  
  229.         }  
  230.   
  231.         if (genealogy.find(parentName) == genealogy.end())  
  232.         {  
  233.             genealogy[parentName] = nNames;  
  234.             allNames[nNames++] = parentName;  
  235.         }  
  236.   
  237.         parent[genealogy[childName]] = genealogy[parentName];  
  238.     }  
  239.   
  240.     while (cin >> childName >> parentName)  
  241.     {  
  242.         if (!isInTree(childName, parentName))  
  243.             continue;  
  244.           
  245.         if (!isInSameTree(childName, parentName))  
  246.             continue;  
  247.   
  248.         if (isAncestor(childName, parentName))  
  249.             continue;  
  250.   
  251.         if (isDescendent(childName, parentName))  
  252.             continue;  
  253.   
  254.         if (isSibling(childName, parentName))  
  255.             continue;  
  256.   
  257.         if (isCousin(childName, parentName))  
  258.             continue;  
  259.     }  
  260.   
  261.     return 0;  
  262. }  
// UVa Problem 115 - Climbing Trees
// Verdict: Accepted
// Submission Date: 2011-11-27
// UVa Run Time: 0.008s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
//
// [解题方法]
// 模拟题。由于只需要保存父子关系,不一定要构建树。虽然方法是直接的,但是有点繁琐。

#include <iostream>
#include <map>

using namespace std;

#define MAXN 310
#define DUMMY (-1)

int parent[MAXN];
map < string, int > genealogy;
	
bool isAncestor(string fName, string sName)
{
	int nFirst = genealogy[fName];
	int nSecond = genealogy[sName];
	int depth = 0;
	bool found = false;

	while (parent[nSecond] != DUMMY)
	{
		if (parent[nSecond] == nFirst)
		{
			found = true;
			break;
		}
		nSecond = parent[nSecond];
		depth++;
	}
	
	if (found)
	{
		if (depth == 0)
			cout << "parent\n";
		else if (depth == 1)
			cout << "grand parent\n";
		else
		{
			for (int i = 1; i < depth; i++)
				cout << "great ";
			cout << "grand parent\n";		
		}

		return true;
	}
	
	return false;
}

bool isDescendent(string fName, string sName)
{
	int nFirst = genealogy[fName];
	int nSecond = genealogy[sName];
	int depth = 0;
	bool found = false;
	while (parent[nFirst] != DUMMY)
	{
		if (parent[nFirst] == nSecond)
		{
			found = true;
			break;
		}
		nFirst = parent[nFirst];
		depth++;
	}
	
	if (found)
	{
		if (depth == 0)
			cout << "child\n";
		else if (depth == 1)
			cout << "grand child\n";
		else
		{
			for (int i = 1; i < depth; i++)
				cout << "great ";
			cout << "grand child\n";		
		}

		return true;
	}
	
	return false;
}

bool isInTree(string fName, string sName)
{
	if (genealogy.find(fName) == genealogy.end() ||
			genealogy.find(sName) == genealogy.end())
	{
		cout << "no relation\n";
		return false;
	}
	
	return true;
}

bool isInSameTree(string fName, string sName)
{
	int nFirst = genealogy[fName];
	int nSecond = genealogy[sName];

	while (parent[nFirst] != DUMMY)
		nFirst = parent[nFirst];
	while (parent[nSecond] != DUMMY)
		nSecond = parent[nSecond];

	if (nFirst != nSecond)
	{
		cout << "no relation\n";
		return false;		
	}
	
	return true;
}

bool isSibling(string fName, string sName)
{
	int nFirst = genealogy[fName];
	int nSecond = genealogy[sName];	
	if (parent[nFirst] != DUMMY && parent[nSecond] != DUMMY &&
		parent[nFirst] == parent[nSecond])
	{
		cout << "sibling\n";
		return true;
	}
	
	return false;
}

bool isCousin(string fName, string sName)
{
	int nFirst = genealogy[fName];
	int nSecond = genealogy[sName];
	int n = 0, m = 0;

	while (parent[nFirst] != DUMMY)
	{
		nFirst = parent[nFirst];
		n++;
	}
	while (parent[nSecond] != DUMMY)
	{
		nSecond = parent[nSecond];
		m++;
	}

	nFirst = genealogy[fName];
	nSecond = genealogy[sName];
	int backN = n, backM = m;

	if (n > m)
	{
		while (n > m)
		{
			nFirst = parent[nFirst];
			n--;
		}
		while (parent[nFirst] != parent[nSecond])
		{
			nFirst = parent[nFirst];
			nSecond = parent[nSecond];
			n--;
		}
		
		backN -= n;
		backM -= n;
		
		cout << backM << " cousin removed " << (backN - backM) << "\n";
	}
	else if (m > n)
	{
		while (m > n)
		{
			nSecond = parent[nSecond];
			m--;
		}
		while (parent[nFirst] != parent[nSecond])
		{
			nFirst = parent[nFirst];
			nSecond = parent[nSecond];
			m--;
		}
		
		backN -= m;
		backM -= m;
		
		cout << backN << " cousin removed " << (backM - backN) << "\n";	
	}
	else
	{
		while (parent[nFirst] != parent[nSecond])
		{
			nFirst = parent[nFirst];
			nSecond = parent[nSecond];
			n--;
			m--;
		}
		
		cout << (backN - n) << " cousin\n";
	}
}

int main (int argc, char const* argv[])
{
	int nNames = 0;
	string allNames[MAXN];
	string childName, parentName;

	for (int i = 0; i < MAXN; i++)
		parent[i] = DUMMY;

	while (cin >> childName >> parentName)
	{
		if (childName == "no.child" && parentName == "no.parent")
			break;
		if (genealogy.find(childName) == genealogy.end())
		{
			genealogy[childName] = nNames;
			allNames[nNames++] = childName;
		}

		if (genealogy.find(parentName) == genealogy.end())
		{
			genealogy[parentName] = nNames;
			allNames[nNames++] = parentName;
		}

		parent[genealogy[childName]] = genealogy[parentName];
	}

	while (cin >> childName >> parentName)
	{
		if (!isInTree(childName, parentName))
			continue;
		
		if (!isInSameTree(childName, parentName))
			continue;

		if (isAncestor(childName, parentName))
			continue;

		if (isDescendent(childName, parentName))
			continue;

		if (isSibling(childName, parentName))
			continue;

		if (isCousin(childName, parentName))
			continue;
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值