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 ...
随机推荐
- docker的网络-单主机(三种原生网络)none、host、bridge
docker的网络分为:单主机.跨主机 这篇先说:单主机 我们先说一下docker的原生网络模式 网络模式 简介 优点 使用场景 none 空网络,没有网络 此网络与外界隔离,安全度非常高 适合公司内 ...
- 网络安全-WEB基础,burpsuite,WEB漏洞
1. web基础 HTTP: GET POST REQUEST RESPONSE... JDK robots.txt 网页源代码/注释 目录扫描--御剑,dirmap 端口信息--nmap 备份文件- ...
- 智能社讲解js基础
一,Javascript的组成ECMAScript:翻译,核心,解释器,DOM:文档对象模型,Document Object Model 操作HTML的能力,document对象BOM:浏览器对象模型 ...
- POJ - 3665 icow
Fatigued by the endless toils of farming, Farmer John has decided to try his hand in the MP3 player ...
- Hacker101 CTF-Micro-CMS v2
一.打开网站是这个样子 找到一个登录框,存在注入漏洞 3.我们可以这样更改用户名中的输入: admin' or 1=1 -- 4.错误消息显示Invalid Password,因此我们也应该尝试构造一 ...
- js closures all in one
js closures all in one setTimeout 闭包,log(i, arr[¡]) var, let, closures, IIFE "use strict"; ...
- Linux & change username & computer name & .bashrc
Linux & change username & computer name ubuntu change username and computer name https://ask ...
- React Native & Android & Text Input
React Native & Android & Text Input react native clear input value https://stackoverflow.com ...
- Typescript & classes & public shorthand
classes & public shorthand Also of note, the use of public on arguments to the constructor is a ...
- CSS & SASS & SCSS & less
CSS & SASS & SCSS & less less vs scss https://github.com/vecerek/less2sass/wiki/Less-vs. ...