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 ...
随机推荐
- 一篇文章图文并茂地带你轻松实践 HTML5 history api
HTML5 history api 前言 由于笔者在网络上没有找到比较好的关于 history api 的实践案例,有的案例过于杂乱,没有重点,有些案例只是告诉读者 api 是什么,却没告诉怎么用,本 ...
- codeforces 1039B Subway Pursuit【二分+随机】
题目:戳这里 题意:一个点在[1,n]以内,我们可以进行4500次查询,每次查询之后,该点会向左或向右移动0~k步,请在4500次查询以内找到该点. 解题思路:一边二分,一边随机. 交互题似乎有好多是 ...
- 2018ACM上海大都会赛 F Color it【基础的扫描线】
题目:戳这里 题意:有n*m个点全为白色,q个圆,将q个圆内所有的点都染成黑色,问最后剩下多少白色的点. 解题思路:每一行当做一个扫描线,扫描所有的圆,记录每一行在圆中的点即可,O(n*q). 附ac ...
- HDU 6623 Minimal Power of Prime(思维)题解
题意: 已知任意大于\(1\)的整数\(a = p_1^{q_1}p_2^{q_2} \cdots p_k^{q_k}\),现给出\(a \in [2,1e18]\),求\(min\{q_i\},q ...
- 如何使用 Python 编写后端 API 接口
如何使用 Python 编写后端 API 接口 get API Python3 # coding:utf-8 import json # ModuleNotFoundError: No module ...
- node.js 中间件
node.js 中间件 node.js middleware Express middleware body-parser cookie-parser cookie-session cors csur ...
- linux move file / folder bash command
linux move file / folder bash command mv $ which mv $ man mv # mv [-f] source target/ target folder ...
- js add Struct to ArrayBuffer
使用struct-buffer为ArrayBuffer添加结构体 $ npm i struct-buffer 1. 创建结构体 import { DWORD, string_t, StructBuff ...
- NGK新加坡峰会:超级节点和开源代码为DeFi生态带来新曙光!
据伦敦金融时报以及纽约商业报等多家媒体报道的消息,1月31日,2021 NGK区块链峰会于新加坡正式开幕,全球多位区块链研究所专家线上受邀出席参会,NGK灵石技术研发Clifton先生,法国区块链专家 ...
- ASP.NET Core中如何对不同类型的用户进行区别限流
老板提出了一个新需求,从某某天起,免费用户每天只能查询100次,收费用户100W次. 这是一个限流问题,聪明的你也一定想到了如何去做:记录用户每一天的查询次数,然后根据当前用户的类型使用不同的数字做比 ...