uva 115
来源:互联网 发布:黑门市场 知乎 编辑:程序博客网 时间:2024/04/30 06:26
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<utility>
#include<algorithm>
#define N 301
using namespacestd;
typedef pair<int,int> Pair;
/*
node[x] = x 的祖先
注意 0號為沒有祖先了
*/
int node[N] = {};
Pair getRelation(int first,int second)
{
/*
如果關係為 cousin:
lev1,lev2 分別存共同祖先的各個順位
如果某一方為另一方的祖先:
lev1 = 存他祖先的順位
lev2 = -1
反之則關係相反
*/
int order1[N] = {}, order2[N];//[x] = x 的順位
int lev1 = -1, lev2 = -1;
int count =1;//計算目前順位
int next =node[first];
while (next)
{
order1[next] = count++;
next = node[next];
}
if (order1[second])//n是 this的祖先
lev1 = order1[second];
else
{
next = node[second];
count = 1;
while (next)
{
if (order1[next])//n和 this為 cousin
{
lev1 = order1[next];
lev2 = count;
break;
}
elseif (next == first)//this是 n的祖先
{
lev2 = count;
break;
}
order2[next] = count++;
next = node[next];
}
}
returnmake_pair(lev1, lev2);
}
int main()
{
string child, parent;
map<string,int> Map;
int mapId =1;//0是沒祖先
while (cin >> child >> parent)
{
if (!strcmp(child.c_str(),"no.child"))
break;
if (!Map.count(child))
Map[child] = mapId++;
if (!Map.count(parent))
Map[parent] = mapId++;
node[Map[child]] = Map[parent];
}
cout<<node[1]<<" "<<node[2]<<""<<node[3]<<endl;
string a, b;
pair<int,int> relation;
while (cin >> a >> b)
{
if (Map[a] == Map[b])//自己和自己沒關係
puts("no relation");
else
{
relation = getRelation(Map[a], Map[b]);
int par1 = relation.first, par2 = relation.second;
if (par1 != -1 && par2 != -1)
{
if (par1 ==1 && par2 == 1)//兄弟
puts("sibling");
else//cousin
{
int diff =abs(par1 - par2);
printf("%d cousin",min(par1, par2) - 1);
if (diff)
printf(" removed %d", diff);
putchar('\n');
}
}
elseif (par1 != -1)//a是 b的子孫
{
if (par1 >1)
{
for (int i =1; i < par1 - 1; i++)
printf("great ");
puts("grand child");
}
else
puts("child");
}
elseif (par2 != -1)//a是 b的祖先
{
if (par2 >1)
{
for (int i =1; i < par2 - 1; i++)
printf("great ");
puts("grand parent");
}
else
puts("parent");
}
else//沒關係
puts("no relation");
}
}
return0;
}
- uva 115
- uva 12096 115 页
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- Python内置函数_反射类
- Method.getParameterAnnotations()研究
- Linux下pppoe设置
- teamviewer 5分钟断线 隔几分钟断线 最新解决办法 专治5分钟限制 完美破解
- 2016工作总结MX
- uva 115
- Mybatis增删改查实例
- bzoj 1089: [SCOI2003]严格n元树 (dp+高精度)
- 边框奇偶数独
- Spring入门(5),Bean种类和作用域
- 面向对象_import关键字的概述和使用
- 找出参数注解和参数的方案(好用)
- 怎么解决win10以太网无有效ip
- 双曲抛物面