团体天梯练习 L2-016 愿天下有情人都是失散多年的兄妹
L2-016 愿天下有情人都是失散多年的兄妹
呵呵。大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人、父母、祖父母、曾祖父母、高祖父母)则不可通婚。本题就请你帮助一对有情人判断一下,他们究竟是否可以成婚?
输入格式:
输入第一行给出一个正整数
本人
其中
接下来给出一个正整数
注意:题目保证两个人是同辈,每人只有一个性别,并且血缘关系网中没有乱伦或隔辈成婚的情况。
输出格式:
对每一对有情人,判断他们的关系是否可以通婚:如果两人是同性,输出
输入样例:
24
00001 M 01111 -1
00002 F 02222 03333
00003 M 02222 03333
00004 F 04444 03333
00005 M 04444 05555
00006 F 04444 05555
00007 F 06666 07777
00008 M 06666 07777
00009 M 00001 00002
00010 M 00003 00006
00011 F 00005 00007
00012 F 00008 08888
00013 F 00009 00011
00014 M 00010 09999
00015 M 00010 09999
00016 M 10000 00012
00017 F -1 00012
00018 F 11000 00013
00019 F 11100 00018
00020 F 00015 11110
00021 M 11100 00020
00022 M 00016 -1
00023 M 10012 00017
00024 M 00022 10013
9
00021 00024
00019 00024
00011 00012
00022 00018
00001 00004
00013 00016
00017 00015
00019 00021
00010 00011
输出样例:
Never Mind
Yes
Never Mind
No
Yes
No
Yes
No
No
解题思路
这道题需要处理多个关于两个节点五层之内是否存在公共祖先的询问,我一开始直观感觉是最近公共祖先 不过似乎团体天梯L2级别也不太会考到什么LCA这个略有难度的知识点。
所以对于每个节点,需要维护其父亲和母亲,不仅如此还需要标记其性别,因为题目中说如果询问的两个人是同性......
但是要注意的是,题中给出的
判断五代之内是否存在公共祖先,主要方法是
/* 一切都是命运石之门的选择 El Psy Kongroo */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
typedef __int128 int128;
#define PI acos(-1.0)
#define x first
#define y second
//int dx[4] = {1, -1, 0, 0};
//int dy[4] = {0, 0, 1, -1};
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;
const int N = 1e5 + 10;
struct node{
int fa, mo;
char gen;
}p[N];
int n, m;
bool dfs(int a, int b, int cnt){
if(a == -1 || b == -1) return true;
if(cnt <= 4 && a == b) return false; //五代以内出现公共祖先
if(cnt >= 5) return true; //五代以外还未出现公共祖先
//递归
return dfs(p[a].fa, p[b].fa, cnt + 1) && dfs(p[a].fa, p[b].mo, cnt + 1)
&& dfs(p[a].mo, p[b].fa, cnt + 1) && dfs(p[a].mo, p[b].mo, cnt + 1);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
for(int i = 0; i < N; i ++ ) p[i].fa = p[i].mo = -1;
cin >> n;
while(n -- ){
int id, fa, mo; cin >> id;
char gen;
cin >> gen >> fa >> mo;
p[id] = {fa, mo, gen};
if(~fa) p[fa].gen = 'M';
if(~mo) p[mo].gen = 'F';
}
cin >> m;
while(m -- ){
int a, b; cin >> a >> b;
if(p[a].gen == p[b].gen){
cout << "Never Mind" << endl;
continue;
}
if(dfs(a, b, 0)) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
一切都是命运石之门的选择,本文章来源于博客园,作者:MarisaMagic,出处:https://www.cnblogs.com/MarisaMagic/p/17327713.html,未经允许严禁转载
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具