PAT 1094. The Largest Generation(BFS)

Posted yxysuanfa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}


以上是关于PAT 1094. The Largest Generation(BFS)的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1094. The Largest Generation(BFS)

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

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

PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]

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

1094 The Largest Generation (25)