ACM UVA-10959

bfs

int ans[1000];
int main(int argc, char const* argv[])
{
    int t;
    bool first = true;
    while(~scanf("%d",&t)){
        while(t--){
            vector<int> P[1000];
            bool visit[1000] = {false};
            if(!first){
                printf("\n");
            }
            int p,d;
            scanf("%d %d",&p,&d);
            while(d--){
                int p1,p2;
                scanf("%d %d",&p1,&p2);
                P[p1].push_back(p2);
                P[p2].push_back(p1);
            }
            queue<int> q;
            q.push(0);
            visit[0] = true;
            ans[0] = 0;
            while(!q.empty()){
                int  next = q.front();
                q.pop();
                int l = P[next].size();
                for(int i = 0;i<l;i++){
                    if(!visit[P[next][i]]){
                        //printf("next = %d,P[next][i] = %d\n",next,P[next][i]);
                        q.push(P[next][i]);
                        visit[P[next][i]] = true;
                        ans[P[next][i]] = ans[next]+1;
                    }
                }

            }

            for(int i = 1;i<p;i++){
                printf("%d\n",ans[i]);
            }
            first = false;
        }
    }
    return 0;
}