天梯赛决赛l2

编程入门 行业动态 更新时间:2024-10-08 04:29:06

<a href=https://www.elefans.com/category/jswz/34/1767727.html style=天梯赛决赛l2"/>

天梯赛决赛l2

原文:
L2-016. 愿天下有情人都是失散多年的兄妹
时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
呵呵。大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人、父母、祖父母、曾祖父母、高祖父母)则不可通婚。本题就请你帮助一对有情人判断一下,他们究竟是否可以成婚?

输入格式:

输入第一行给出一个正整数N(2 <= N <= 104),随后N行,每行按以下格式给出一个人的信息:

本人ID 性别 父亲ID 母亲ID

其中ID是5位数字,每人不同;性别M代表男性、F代表女性。如果某人的父亲或母亲已经不可考,则相应的ID位置上标记为-1。

接下来给出一个正整数K,随后K行,每行给出一对有情人的ID,其间以空格分隔。

注意:题目保证两个人是同辈,每人只有一个性别,并且血缘关系网中没有乱伦或隔辈成婚的情况。

输出格式:

对每一对有情人,判断他们的关系是否可以通婚:如果两人是同性,输出“Never Mind”;如果是异性并且关系出了五服,输出“Yes”;如果异性关系未出五服,输出“No”。

输入样例:
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
数组标记,和两个BFS 判断是否在五代以内,
//注意第三个点答案错误就是 输入可能自己的id是-1的情况,如果不进行下标+1(或者大于1)那么-1进行数组下标访问时是不行的,

//注意第三个点答案错误就是  输入可能自己的id是-1的情况,如果不进行下标+1(或者大于1)那么-1进行数组下标访问时是不行的,  
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
struct Man {int id;char sex;int fa;int ma;
}man[maxn];
int mother[maxn], father[maxn];
int vis[maxn];
void find1(int st) {queue<int> q;q.push(st);int num = 0;while (!q.empty()) {int f = q.front(); q.pop();if (num >= 30) break;if (f != -1 && father[f] != -1) {vis[father[f]] = 1;}if (f != -1 && mother[f] != -1) {vis[mother[f]] = 1;}q.push(father[f]);q.push(mother[f]);num += 2;}}
int find2(int st) {queue<int> q;q.push(st);int num = 0;while (!q.empty()) {int f = q.front(); q.pop();if (num >= 30) break;if (f != -1 && father[f] != -1 && vis[father[f]] == 1) {return 0;   }if (f != -1 && mother[f] != -1 && vis[mother[f]] == 1) {return 0;}q.push(father[f]);q.push(mother[f]);num += 2;}return 1;
}
int main() {fill(father, father+maxn, -1);fill(mother, mother+maxn, -1);int m; cin >> m;while (m-- != 0) {int id; cin >> id; man[id].id = id; cin >> man[id].sex >> man[id].fa >> man[id].ma;if (man[id].fa != -1) {father[id] = man[id].fa;man[man[id].fa].sex = 'M';}if (man[id].ma != -1) {mother[id] = man[id].ma;man[man[id].ma].sex = 'F';}if (man[id].fa == -1) {father[id] = -1;}if (man[id].ma == -1) {mother[id] = -1;}}int k; cin >> k;while (k-- != 0) {//每次都要初始化 memset(vis, 0, sizeof(vis));int a, b; cin >> a >> b;if (man[a].sex == man[b].sex) puts("Never Mind");else {find1(a);if (find2(b) == 1) {puts("Yes");} else {puts("No");}}}return 0;
} 

以下正确满分

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
struct Man {int id;char sex;int fa;int ma;
}man[maxn];
int mother[maxn], father[maxn];
int vis[maxn];
void find1(int st) {queue<int> q;q.push(st);int num = 0;while (!q.empty()) {int f = q.front(); q.pop();if (num >= 30) break;if (f != -1 && father[f+1] != -1) {vis[father[f+1]] = 1;}if (f != -1 && mother[f+1] != -1) {vis[mother[f+1]] = 1;}q.push(father[f+1]);q.push(mother[f+1]);num += 2;}}
int find2(int st) {queue<int> q;q.push(st);int num = 0;while (!q.empty()) {int f = q.front(); q.pop();if (num >= 30) break;if (f != -1 && father[f+1] != -1 && vis[father[f+1]] == 1) {return 0;   }if (f != -1 && mother[f+1] != -1 && vis[mother[f+1]] == 1) {return 0;}q.push(father[f+1]);q.push(mother[f+1]);num += 2;}return 1;
}
int main() {fill(father, father+maxn, -1);fill(mother, mother+maxn, -1);int m; cin >> m;while (m-- != 0) {int id; cin >> id; man[id].id = id; cin >> man[id].sex >> man[id].fa >> man[id].ma;if (man[id].fa != -1) {father[id+1] = man[id].fa;man[man[id].fa].sex = 'M';}if (man[id].ma != -1) {mother[id+1] = man[id].ma;man[man[id].ma].sex = 'F';}if (man[id].fa == -1) {father[id+1] = -1;}if (man[id].ma == -1) {mother[id+1] = -1;}}int k; cin >> k;while (k-- != 0) {//每次都要初始化 memset(vis, 0, sizeof(vis));int a, b; cin >> a >> b;if (man[a].sex == man[b].sex) puts("Never Mind");else {find1(a);if (find2(b) == 1) {puts("Yes");} else {puts("No");}}}return 0;
} 

更多推荐

天梯赛决赛l2

本文发布于:2024-02-19 18:56:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:天梯   决赛

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!