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.

题目描述:
题目分析:








- 初始化:建图,建记录每个结点的上一个结点的(父节点)数组,建深度数组
- 用dfs记录一遍深度
- 假如两个要找lca的结点的深度不同,先通过父节点数组使两个结点的深度相同,再一起通过父节点数组找lca






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 ...
随机推荐
- P2P协议初步
今天看到一个问题,如何把一个文件快速下发到100w个服务器 如果我们将文件集中式地放在一个服务器或缓存上的话,带宽.连接都会遇到问题. 树状: 1. 每个服务器既具有文件存储能力也应具有 ...
- MDK5生成BIn文件的方法
配置MDK5 生成bin文件的 第一步:方法打开option for Target 第二步:选择 user 第三步:找到After Build/Rebuild 第四步:勾选run,点击文件选择小图标选 ...
- Linux bash fi
Linux bash fi if..else..fi allows to make choice based on the success or failure of a command. if..e ...
- TypeScript Learning Paths
TypeScript Learning Paths TypeScript Expert refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以 ...
- p5.js
p5.js p5.js是一个用于创意编码的JavaScript库,其重点是使艺术家,设计师,教育者,初学者以及其他任何人都可以访问并包含所有编码! https://p5js.org/ https: ...
- 微软 AI 公开课
微软 AI 公开课 https://github.com/microsoft/ai-edu https://school.azure.cn/ https://docs.microsoft.com/le ...
- js 触发长按事件
为网站添加触摸功能 <button id="btn1">长按触发</button> <button id="btn2">长按 ...
- CentOS 7.6.1810 运行pupperteer
故障排除 安装puppeteer,使用cnpm 解决依赖 $ yum -y update $ yum install -y pango libXcomposite libXcursor libXdam ...
- HANNAH WHITE:不拖延的人生是什么样子的?
不拖延的人生,究竟是什么样子呢?近日,星盟投资总经理HANNAH在一档人物采访栏目中表示,不拖延的人生,真的是太爽了! HANNAH在栏目中讲了一个曾经公司同事的故事.她说,那位同事总是喜欢拖延.每次 ...
- 翻译:《实用的Python编程》01_06_Files
目录| 上一节(1.5 列表) | 下一节 (1.7 函数) 1.6 文件管理 大多数的程序需要从某处读取输入.本节讨论文件访问. 文件输入和输出 打开一个文件: f = open('foo.txt' ...