2019 GDUT Rating Contest III : Problem E. Family Tree
题面:
E. Family Tree
- You should output "SIBLINGS"if BESSIE and ELSIE have the same mother.
- BESSIE might be a direct descendant of ELSIE, meaning that ELSIE is either the mother, grandmother, great-grand-mother, great-great-grand-mother, etc., of BESSIE. If this is the case, you should print "ELSIE is the (relation) of BESSIE where (relation) is the appropriate relationship, for example "great-great-grand-mother".
- If ELSIE is a child of an ancestor of BESSIE (and ELSIE is not herself an ancestor or sister of BESSIE), then ELSIE is BESSIE’s aunt. You should output "ELSIE is the aunt of BESSIE"if ELSIE is a child of BESSIE’s grand-mother, "ELSIE is the great-aunt of BESSIE"if ELSIE is a child of BESSIE’s great-grand-mother, "ELSIE is the great-great-aunt of BESSIE"if ELSIE is a child of BESSIE’s great-great-grand-mother, and so on.
- If BESSIE and ELSIE are related by any other means (i.e., if they share a common ancestor), they are cousins, and you should simply output "COUSINS".
- You should output "NOT RELATED"if BESSIE and ELSIE have no common ancestor, or neither is directly descended from the other.
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225083238226-1692588691.png)
题目描述:
题目分析:
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225085945501-1506753115.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225090316970-55516169.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225090833918-1352591777.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225091247040-1138962942.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225092116735-325462312.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225092745402-684508973.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225093435321-1682634913.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225093953416-93199424.png)
- 初始化:建图,建记录每个结点的上一个结点的(父节点)数组,建深度数组
- 用dfs记录一遍深度
- 假如两个要找lca的结点的深度不同,先通过父节点数组使两个结点的深度相同,再一起通过父节点数组找lca
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225153918082-968892113.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225155610089-938870198.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225162058546-805110375.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225162203132-1153493461.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225162740813-552001100.png)
![](https://img2018.cnblogs.com/blog/1591738/201902/1591738-20190225163721758-741045826.png)
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <cmath>
5 #include <set>
6 #include <map>
7 #include <algorithm>
8 using namespace std;
9 int n;
10 int cnt; //牛的个数
11 int G[205][205]; //存图
12 int lca; //最近公共祖先
13 string u, v;
14
15 map<string, int> name; //为每头牛分配编号用
16 int mother[205]; //每头牛的MOTHER
17 int len[205]; //每头牛到最开始那头牛的距离
18 int in[205]; //每头牛的入度
19
20 void dfs_deep(int u, int deep){
21 if(len[1] && len[2]) return; //如果都获得了距离就结束
22 len[u] = deep; //填入距离
23 for(int i = 1; i <= cnt; i++){
24 if(G[u][i]) dfs_deep(i, deep+1); //往下继续遍历
25 }
26 }
27
28 void find_lca(){
29 int AA = 1, BB = 2;
30 if(len[AA] < len[BB]) swap(AA, BB); //交换下标
31 while(len[AA] != len[BB]) AA = mother[AA]; //把距离远的拉到同一距离
32 while(AA != BB){ //一起找最近公共祖先
33 AA = mother[AA];
34 BB = mother[BB];
35 }
36 lca = AA; //存起来
37 }
38
39 void print(){
40 int a = 1, b = 2;
41 if(len[a] > len[b]) {
42 swap(a, b); //交换下标
43 swap(u, v); //交换名字
44 }
45
46 int disa = len[a]-len[lca], disb = len[b]-len[lca];
47
48 if(disa == 1 && disb == 1) //情况1
49 cout << "SIBLINGS\n";
50 else if(disa > 1) //情况4
51 cout << "COUSINS\n";
52 else{
53 cout << u << " is the ";
54 if(disa == 0){ //情况2
55 for(int i = 0; i < disb-2; i++)
56 cout << "great-";
57 if(disb > 1) cout << "grand-";
58 cout << "mother";
59 }
60 else{ //情况3
61 for(int i = 0; i < disb-2; i++)
62 cout << "great-";
63 cout << "aunt";
64 }
65 cout << " of " << v << endl;
66 }
67 }
68
69 int main(){
70 cin >> n;
71 cin >> u >> v;
72 name[u] = ++cnt; //分配编号
73 name[v] = ++cnt;
74
75 string x, y;
76 for(int i = 0; i < n; i++){
77 cin >> x >> y;
78 if(!name[x]) name[x] = ++cnt; //分配编号
79 if(!name[y]) name[y] = ++cnt;
80
81 int s = name[x], to = name[y];
82 G[s][to] = 1; //存边
83 in[to] = 1; //标记入度不为0的点
84 mother[to] = s; //存MOTHER
85 }
86
87 for(int i = 1; i <= cnt; i++){
88 if(!in[i]) {
89 dfs_deep(i, 1); //计算距离
90 if(len[1] && len[2]) break; //有一个尚未被标记就继续
91 len[1] = len[2] = 0;
92 }
93 }
94
95 if(!len[1] && !len[2]){ //情况5:找不到最近公共祖先等价于两个点没有被标记
96 cout << "NOT RELATED\n";
97 return 0;
98 }
99
100 find_lca(); //找最近公共祖先
101 print(); //输出
102 return 0;
103 }
2019 GDUT Rating Contest III : Problem E. Family Tree的更多相关文章
- 2019 GDUT Rating Contest III : Problem D. Lemonade Line
题面: D. Lemonade Line Input file: standard input Output file: standard output Time limit: 1 second Memo ...
- 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe
题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...
- 2019 GDUT Rating Contest III : Problem A. Out of Sorts
题面: 传送门 A. Out of Sorts Input file: standard input Output file: standard output Time limit: 1 second M ...
- 2019 GDUT Rating Contest II : Problem F. Teleportation
题面: Problem F. Teleportation Input file: standard input Output file: standard output Time limit: 15 se ...
- 2019 GDUT Rating Contest I : Problem H. Mixing Milk
题面: H. Mixing Milk Input file: standard input Output file: standard output Time limit: 1 second Memory ...
- 2019 GDUT Rating Contest I : Problem A. The Bucket List
题面: A. The Bucket List Input file: standard input Output file: standard output Time limit: 1 second Me ...
- 2019 GDUT Rating Contest I : Problem G. Back and Forth
题面: G. Back and Forth Input file: standard input Output file: standard output Time limit: 1 second Mem ...
- 2019 GDUT Rating Contest II : Problem G. Snow Boots
题面: G. Snow Boots Input file: standard input Output file: standard output Time limit: 1 second Memory ...
- 2019 GDUT Rating Contest II : Problem C. Rest Stops
题面: C. Rest Stops Input file: standard input Output file: standard output Time limit: 1 second Memory ...
随机推荐
- 牛客国庆2 F-平衡二叉树【非原创】
题目:戳这里 学习博客:戳这里
- 近期做的一些DP
UVa 1625 color length https://blog.csdn.net/Dylan_Frank/article/details/52261424 https://www.cnblogs ...
- JavaScript调试技巧之console.log()
与alert()函数类似,console.log()也可以接受变量并将其与别的字符串进行拼接: 代码如下: //Use variable var name = "Bob"; con ...
- 如何在 VSCODE 中高效使用 R 语言
VSCODE 配置 R 一.功能特性展示 之前一直在用 Rstudio 来编写 R,也尝试用过 Pycharm 配置 R 环境. 但是由于现在需求要同时满足 Python,R 和网站要同时开发,为了避 ...
- Fullscreen API All In One
Fullscreen API All In One 全屏显示 https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API https ...
- Azure & Serverless
Azure & Serverless https://azure.microsoft.com/en-us/get-started/webinar/on-demand/ blob:https:/ ...
- website 性能检测 & 前端性能优化
website 性能检测 & 前端性能优化 https://cdn.xgqfrms.xyz/ https://mobile.xgqfrms.xyz/ PageSpeed Insights ht ...
- 10月上线的NGK global有怎样的发展前景?
随着NGK global 10月份的上线时间将近,社区中也开始纷纷讨论.预测起NGK global上线后的表现,对此小编也有一些自己的理解,就此分享给大家. 在基于实体生态的赋能下,NGK globa ...
- 新手如何通过内存和NGK DeFi Baccarat进行组合投资?
区块链市场在2020年迎来了大爆发,资本市场异常火热.无论是内存,还是DeFi,都无疑是这个火爆的区块链市场中的佼佼者.通过投资内存和DeFi,很多投资者都已经获取了非常可观的收益,尝到了资本市场带来 ...
- [转]在ROS下使用zeroconf配置多机通信
原文地址:http://www.corvin.cn/635.html,转载主要方便随时查阅,如有版权要求,请及时联系. 0x00 为何需要配置ROS多机通信 众所周知ROS是分布式系统,因此可以将机器 ...