PAT 1094. The Largest Generation(BFS)

CODE:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

bool mat[105][105];
bool root[105];
int n,m;
int R;
int cnt[105];
int ans1,ans2;

struct TNode
{
    int num;
    int level;
};

void BFS()
{
    queue<TNode> Q;
    TNode first;
    first.num=R;
    first.level=1;
    TNode next;
    Q.push(first);
    while(!Q.empty())
    {
        first=Q.front();
        cnt[first.level]++;
        Q.pop();
        for(int j=1;j<=n;j++)
        {
            if(mat[first.num][j]==true)
            {
                next.num=j;
                next.level=first.level+1;
                Q.push(next);
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
       if(ans1<cnt[i])
            {ans1=cnt[i];
            ans2=i;
            }
    }
}

int main()
{
    int id,k;
    while(scanf("%d%d",&n,&m)==2)
    {
        memset(mat,false,sizeof(mat));
        memset(root,true,sizeof(root));
        memset(cnt,0,sizeof(cnt));
        int flag=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&id,&k);
            int id1;
            while(k--)
            {
                scanf("%d",&id1);
                mat[id][id1]=true;
                root[id1]=false;
            }
            for(int i=1;i<=n;i++)
            {
                if(root[i]==true)
                {
                    for(int j=1;j<=n;j++)
                    {
                        if(mat[i][j]==true)
                        {
                            flag=1;
                            R=i;
                            break;
                        }
                    }
                }
                if(flag)
                    break;
            }
        }
        ans1=-1;
        BFS();
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}
时间: 2024-11-05 18:40:06

PAT 1094. The Largest Generation(BFS)的相关文章

PAT 1094 The Largest Generation[bfs][一般]

1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population. Input Specification: Ea

1094. The Largest Generation (25) 此题和六度空间一个道理,记录BFS的层次

1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find

1094. The Largest Generation (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1094. The Largest Generation (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the

PAT (Advanced Level) 1094. The Largest Generation (25)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; const int maxn=200; int n,m; vector<int>

1094. The Largest Generation (25)

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population. Input Specification: Each input file contains one test ca

1094 The Largest Generation

题意:略. 思路:层序遍历:在结点中增加一个数据域表示结点所在的层次. 代码: #include <cstdio> #include <queue> #include <vector> using namespace std; struct Node{ int layer; vector<int> child; }; vector<Node> tree(110); int level[110]={0}; int maxLevel=1,maxNum

1094 The Largest Generation (25point(s)) Easy only once

基本思想: 层序遍历问题: 关键点: 无: #include<iostream> #include<stdlib.h> #include<stdio.h> #include<vector> #include<string> #include<math.h> #include<algorithm> #include<cstring> #include<map> #include<queue>

pat1094. The Largest Generation (25)

1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find

The Largest Generation (25)(BFS)(PAT甲级)

#include<bits/stdc++.h>using namespace std;int n,m,l,t;int a[1307][137][67];int vis[1307][137][67];typedef struct{    int x,y,z;}node;int xx[6]={0,0,0,0,1,-1};int yy[6]={0,0,1,-1,0,0};int zz[6]={1,-1,0,0,0,0};node p;int bfs(int x,int y,int z){    qu