• PAT天梯赛 L2-026. 小字辈 【BFS】


    题目链接

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

    思路
    用一个二维vector 来保存 每个人的子女

    然后用BFS 广搜下去,当目前的状态 是搜完的时候

    那么此时队列里的人都是最小的一辈 标记一下 CUR 然后 讲答案压入VECTOR 然后排序一下 输出来就可以

    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>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    #define pb push_back
    
    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-30;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e5 + 5;
    const int MOD = 1e9 + 7;
    
    vector <int> arr[maxn], ans;
    
    int n, Cur;
    
    queue <int> q;
    
    int Count;
    
    void bfs(int cur)
    {
        int len = q.size();
        for (int i = 0; i < len; i++)
        {
            int num = q.front();
            q.pop();
            vector <int>::iterator it;
            for (it = arr[num].begin(); it != arr[num].end(); it++)
            {
                q.push(*it);
                Count++;
            }
        }
        if (Count == n)
        {
            while (!q.empty())
            {
                int num = q.front();
                q.pop();
                ans.pb(num);
            }
            sort(ans.begin(), ans.end());
            Cur = cur + 1;
            return;
        }
        bfs(cur + 1);
    }
    
    int main()
    {
        scanf("%d", &n);
        int vis;
        int num;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &num);
            if (num != -1)
                arr[num].pb(i);
            else
                vis = i;
        }
        if (n == 1)
            printf("1
    1
    ");
        else
        {
            Count = 1;
            q.push(vis);
            bfs(1);
            printf("%d
    ", Cur);
            vector <int>::iterator it;
            for (it = ans.begin(); it != ans.end(); it++)
            {
                if (it != ans.begin())
                    printf(" ");
                printf("%d", *it);
            }
            printf("
    ");
        }
    }
    
  • 相关阅读:
    PHP脚本 校验/补全身份证号码
    常用解压缩软件打包速度比拼
    用ffmpeg对视频转码,视频格式转换
    PHP使用readability提取任意网页正文内容
    PHP获取/判断HTML/网页的charset/编码
    缺少HTTPOnly和Secure属性解决方案
    7Zip/7z命令行中英文对照说明
    RSA加解密、签名验签算法,附代码
    WPF 笔记 十 项目资源收集
    WPF 笔记 九 通过UserControl实例说明依赖属性和绑定
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433156.html
Copyright © 2020-2023  润新知