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


    题目链接

    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
    ");
            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
    ");
                else
                    printf("No
    ");
            }
        }
    }
  • 相关阅读:
    开源项目之Android StandOut(浮动窗口)
    小智慧7
    安卓学习
    asp.net学习Request和Response的其他属性
    bash中的转义
    POJ 1833 排列
    Django点滴(四)ORM对象存取
    POJ 1681 Painter's Problem
    linux2.6.32在mini2440开发板上移植(21)之WebServer服务器移植
    [gkk传智]static与多态及向下向上转型,及多态调用总结
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433233.html
Copyright © 2020-2023  润新知