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 ...
随机推荐
- C++ STL (基础)
STL是什么(STL简介) 本节主要讲述 STL 历史.STL 组件.STL 基本结构以及 STL 编程概述.STL 历史可以追溯到 1972 年 C 语言在 UNIX 计算机上的首次使用.直到 19 ...
- FZU 2082 过路费(树链剖分 边权)题解
题意:给出每条边权值,可以更新每条边权值,询问两个点路径的最小权值 思路:重链剖分边权化点权,让每个儿子节点继承边权. 插点权的时候比较边的两个节点的深度,插进儿子节点中. 代码: #include& ...
- mysql(一)--mysql架构和执行流程
1. 一条查询 SQL 语句是如何执行的? 我们的程序或者工具要操作数据库,第一步要做什么事情? 跟数据库建立连接. 1.1. 通信协议 首先,MySQL 必须要运行一个服务,监听默认的 3306 ...
- 使用opencv-python实现MATLAB的fspecial('Gaussian', [r, c], sigma)
reference_opencv实现高斯核 reference_MATLAB_fspecial函数说明 # MATLAB H = fspecial('Gaussian', [r, c], sigma) ...
- JSON-LD & SEO
JSON-LD & SEO https://json-ld.org/ https://en.wikipedia.org/wiki/JSON-LD Google Search structure ...
- SQL All In One
SQL All In One Structured Query Language SQL is an ANSI (American National Standards Institute) stan ...
- Frameworkless Movement
Frameworkless Movement 无框架运动 https://www.frameworklessmovement.org/ vanilla javascript https://githu ...
- App Store Previewer
App Store Previewer App Store 模拟器 https://www.storepreviewer.com/ xgqfrms 2012-2020 www.cnblogs.com ...
- Chrome DevTools & console & filter warning
Chrome DevTools & console & filter warning
- c++指针练习
Pointers 在getchar处断点,断点后,调试->窗口->反汇编 查看数据 main #include <iostream> #include <Windows. ...