Thursday, April 28, 2011

924 - Spreading The News


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define M 2500
using namespace std;

int FList[M][15],adjNo[M];
int BFS(int,int&,int&);

int main()
{
    int E,t,i,j,s,f,mbs,fbd;
    freopen("in.txt","r",stdin);

    scanf("%d",&E);
    for(i=0;i<E;i++)
    {
        scanf("%d",&adjNo[i]);
        for(j=0;j<adjNo[i];j++)
            scanf("%d",&FList[i][j]);
    }

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&s);
        f = BFS(s,mbs,fbd);
        if(!f)
            printf("0\n");
        else
            printf("%d %d\n",mbs,fbd);
    }
    return 0;
}

int BFS(int s,int& mbs,int& fbd)
{
    int i,h,x,t,st=0,pd=0,cd=0;
    queue<int>Q;
    int color[M],d[M];
    memset(color,0,sizeof(color));
    mbs=0;color[s]=1;d[s]=0;
    Q.push(s);t=0;

    while(!Q.empty())
    {
        h=Q.front();Q.pop();
        pd= cd;cd = d[h];
        if(pd<cd)t=0;
        for(i=0;i<adjNo[h];i++)
        {
            x = FList[h][i];
            if(color[x]==0)
            {
                d[x] = d[h] + 1;
                color[x] = 1;
                t++;
                Q.push(x);
            }
        }
        if(mbs<t)
            mbs=t,fbd=cd+1,st=1;
    }
    return st;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.