题目链接

https://www.patest.cn/contests/gplt/L2-016

思路

用BFS 每层 遍历当代 并且查找当代是否有重复 有重复就跳出 然后 POP 并且将他们的下一代 压入 队列

但是有一个点 要注意

就是 如果存在两个人 他们的上一代 都不可考

那么就默认没有血缘关系

那么就要根据 性别来判断

如果 这两个人 是出现在 某个人的父亲 或 母亲中呢 所以在输入的时候 对于父亲和母亲的性别是没有标记的 那么就会出错

所以要加入 父亲和母亲的性别标记

其实还有种简单的方法 就是用SET就可以了 两个 分别压入 然后判断 SET的大小有没有改变 如果没有改变 就是有重复

或者 用数组标记

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6; const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
const int MOD = 1e9 + 7; map <int, pii> m;
map <int, int> judge[2]; queue <int> q[2]; int ans; void bfs(int cur)
{
if (cur == 7)
return;
for (int i = 0; i < 2; i++)
{
judge[i].clear();
while (!q[i].empty())
{
int num = q[i].front();
q[i].pop();
if (num != -1)
judge[i][num] = 1;
}
}
map <int, int>::iterator it;
for (it = judge[0].begin(); it != judge[0].end(); it++)
{
if (judge[1][it->first])
{
ans = cur;
return;
}
else
judge[1].erase(it -> first);
}
for (int i = 0; i < 2; i++)
{
for (it = judge[i].begin(); it != judge[i].end(); it++)
{
if (m[it->first].first != -1)
q[i].push(m[it->first].first);
if (m[it->first].second != -1)
q[i].push(m[it->first].second);
}
}
bfs(cur + 1);
} int main()
{
map <int, int> vis, opt;
int n;
scanf("%d", &n);
int a, b, c, d;
char code;
for (int i = 0; i < n; i++)
{
scanf("%d %c %d %d", &a, &code, &b, &c);
opt[a] = 1;
if (code == 'M')
vis[a] = 0;
else
vis[a] = 1;
m[a].first = b;
if (b != -1)
{
if (opt[b] == 0)
{
m[b].first = -1;
m[b].second = -1;
}
vis[b] = 0;
}
m[a].second = c;
if (c != -1)
{
if (opt[c] == 0)
{
m[c].first = -1;
m[c].second = -1;
}
vis[c] = 1;
}
}
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d%d", &a, &b);
if (vis[a] == vis[b])
printf("Never Mind\n");
else
{
while (!q[0].empty())
q[0].pop();
while (!q[1].empty())
q[1].pop();
q[0].push(a);
q[1].push(b);
ans = INT_MAX;
bfs(1);
if (ans > 5)
printf("Yes\n");
else
printf("No\n");
}
}
}

PAT 天梯赛 L2-016. 愿天下有情人都是失散多年的兄妹 【BFS】的更多相关文章

  1. 【PTA 天梯赛】L2-016. 愿天下有情人都是失散多年的兄妹(深搜)

    呵呵.大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人.父母.祖父母.曾祖父母.高祖父母)则不可通婚.本题就请你帮助一对有情人判断一下,他们究竟是否可以成婚? 输入格式: 输入 ...

  2. PAT L2-016. 愿天下有情人都是失散多年的兄妹 (BFS)

    L2-016. 愿天下有情人都是失散多年的兄妹 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 呵呵.大家都知道五服以内不得通婚 ...

  3. 愿天下有情人都是失散多年的兄妹(bfs)

    L2-016. 愿天下有情人都是失散多年的兄妹 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 呵呵.大家都知道五服以内不得通婚 ...

  4. L2-016. 愿天下有情人都是失散多年的兄妹(深搜)*

    L2-016. 愿天下有情人都是失散多年的兄妹 参考博客 #include<iostream> #include<cstdio> #include<cstring> ...

  5. L2-016 愿天下有情人都是失散多年的兄妹

    L2-016 愿天下有情人都是失散多年的兄妹 (25 分)   呵呵.大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人.父母.祖父母.曾祖父母.高祖父母)则不可通婚.本题就请你 ...

  6. PAT 天梯赛练习集 L2-016. 愿天下有情人都是失散多年的兄妹

    题目链接:https://www.patest.cn/contests/gplt/L2-016 呵呵.大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人.父母.祖父母.曾祖父母. ...

  7. 团体程序设计天梯赛 L2-016. 愿天下有情人都是失散多年的兄妹

    同时也要记录父母的性别,输出询问时要用到 #include <stdio.h> #include <stdlib.h> #include <string.h> #i ...

  8. PAT L2-016 愿天下有情人都是失散多年的兄妹

    https://pintia.cn/problem-sets/994805046380707840/problems/994805061769609216 呵呵.大家都知道五服以内不得通婚,即两个人最 ...

  9. PAT L2-016 愿天下有情人都是失散多年的兄妹(深搜)

    呵呵.大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人.父母.祖父母.曾祖父母.高祖父母)则不可通婚.本题就请你帮助一对有情人判断一下,他们究竟是否可以成婚? 输入格式: 输入 ...

随机推荐

  1. Bestcoders

    Senior's Fish Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  2. [linux]netstat命令详解-显示linux中各种网络相关信息

    1.功能与说明 netstat 用于显示linux中各种网络相关信息.如网络链接 路由表  接口状态链接 多播成员等等. 2.参数含义介绍 -a (all)显示所有选项,默认不显示LISTEN相关-t ...

  3. IBM Security AppScan Glass Box:一种全新的漏洞扫描思想

    IBM Security AppScan Glass Box:一种全新的漏洞扫描思想 Glass Box 是 IBM Security AppScan Standard Edition(以下简称 Ap ...

  4. Unmapped Spring configuration files found.

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架后,IDEA弹出如下提示: 2.解决方案: File --> Project Structure --> M ...

  5. Linux 下安装PHPunit

    PHP 档案包 (PHAR)  要获取 PHPUnit,最简单的方法是下载 PHPUnit 的 PHP 档案包 (PHAR),它将 PHPUnit 所需要的所有必要组件(以及某些可选组件)捆绑在单个文 ...

  6. mysql命令行导入和导出数据

    首先打开命令窗口,输入命令:mysql -h localhost -u selffabu -p 连接成功后,进行下面的操作 MySQL中导出CSV格式数据的SQL语句样本如下: select * fr ...

  7. C#多线程简单例子讲解

    C#多线程简单例子讲解 标签: 多线程c#threadobjectcallbacktimer 分类: C#(7) 转载网址:http://www.knowsky.com/540518.html .NE ...

  8. 时下世界上最先进的液晶面板技术---ips

    IPS是英文In-Plane Switching的缩写,英文含义为平面转换屏幕技术,由日立公司于2001推出的液晶面板技术,俗称“Super TFT”,是目前世界上最先进的液晶面板技术,目前已经广泛使 ...

  9. C语言合并两个集合(L,L1) 将L1中不在L中的元素插入到L线性表中

    void main(){ Sqlist L,L1; InitList(&L); InitList(&L1); ListInsert(&L, 1, 2); ListInsert( ...

  10. "活在未来" VS “活在当下”(通向财富自由学习笔记六)

    之前读过一些灵修类的书籍,<遇见未知的自己>.<当下的力量>等都在告诉我们活在当下很重要,这里笑来老师提出了一个问题,是活在当下重要呢?还是活在未来?,笑来老师给出了很好的答案 ...