1. 程式人生 > >PAT 1094. The Largest Generation(BFS)

PAT 1094. The Largest Generation(BFS)

tracking empty ont for rst -a pty pri mod

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)