Saturday, 1 October 2011

ACM 10959-The Party, Part I

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cctype>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<sstream>
#include<utility>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#define M 1000
using namespace std;
int G[M][M],P,D,d[M];
void BSF(int s)
{
    queue<int>Q;int color[M],h,i;
    memset(color,0,sizeof(color));
    d[s]=0;color[s]=1;
    Q.push(s);
    while(!Q.empty()) //if Q is not empty,it return false
    {
        h=Q.front();Q.pop();
        for(i=0;i<P;i++)
        {
            if(G[h][i]==1 && color[i]==0)
            {
                color[i]=1;
                d[i]=d[h]+1;//d=depth
                Q.push(i);
            }
            color[h]=2;
        }
    }
}
int main()
{
    int i,j,t,r,c;
    //freopen("in.txt","r",stdin);
    cin>>t;
    while(t--)
    {
        cin>>P>>D;
        for(i=0;i<P;i++)
            for(j=0;j<P;j++)
                G[i][j]=0;

        for(i=0;i<D;i++)
        {
            cin>>r>>c;
            G[r][c]=G[c][r]=1;
        }
        BSF(0);
        for(i=1;i<P;i++)
        {
            cout<<d[i]<<'\n';
        }
        if(t)
            cout<<endl;
    }
    return(0);
}

No comments:

Post a Comment