Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <queue>
using namespace std;
static const int N = 100, WHITE = 0, GREY = 1, BLACK = 2;

int M[N][N], color[N], d[N], n, dis;
queue<int> q;


void label_not_reach()
{
	for(int u = 0; u < n; u++)
	{
		if(color[u] == WHITE) d[u] = -1;
	}
}


void bfs()
{
	int u = 0;
	
	q.push(u);
	
	color[u] = GREY;
	
	while(!q.empty())
	{
		u = q.front();
		for(int v = 0; v < n; v++)
		{
			if(M[u][v] == 1 && color[v] == WHITE)
			{
				color[v] = GREY;
				q.push(v);
				d[v] = d[u] + 1;
			}
		}
		color[u] = BLACK;
		q.pop();	
	}
	
	label_not_reach();
}




int main() 
{
	dis = 0;
	scanf("%d", &n); 
	
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			M[i][j] = 0;
		}
	}
	
	for(int i = 0; i < n; i++)
	{
		color[i] = WHITE;
		d[i] = 0;
	}
	
	for(int i = 1; i <= n; i++)
	{
		int u,k,v;
		scanf("%d %d", &u, &k);
		for(int j = 0; j < k; j++)
		{
			scanf("%d", &v);
			M[u-1][v-1] = 1;
		}
	}
	
	bfs();
	
	for(int i = 0; i < n; i++)
	{
		printf("%d %d\n", i+1, d[i]);
	}
	
//	system("pause");
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int G[223][223];
int dis[223];
int n;

struct st {
  int pos;
  int v;
};

int main() {
  cin >> n;
  for(int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    for(int j = 0; j < v; j++) {
      int t;
      scanf("%d", &t);
      G[u][t] = 1;
    }
  }

  memset(dis, 0x3f, sizeof dis);

  int start = 1;
  queue<st> q;
  q.push((st) {
    1, 0
  });

  while(!q.empty()) {

    st u = q.front();
    q.pop();
    if(dis[u.pos] < 1000000) continue;

    dis[u.pos] = u.v;

    for(int i = 1; i <= n; i++) {
      if(i == u.pos) continue;
      if(G[u.pos][i] == 0) continue;
      q.push((st) {
        i, u.v + 1
      });
    }
  }

  for(int i = 1; i <= n; i++) {
    printf("%d %d\n", i, dis[i] > 100000 ? -1 : dis[i]);
  }

  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;
static const int N=100;

int n,M[N][N];
int d[N];


void bfs(int s){
    queue<int> Q;
    Q.push(s);
    d[s]=0;
    int u,v;
    while(!Q.empty()){
        u=Q.front();Q.pop();
        for(v=0;v<n;v++){
            if(M[u][v]==0) continue;
            if(d[v]!=-1) continue;
            d[v]=d[u]+1;
            Q.push(v);
        }
    }
    for(int i=0;i<n;i++) printf("%d %d\n",i+1,d[i]);
}

int main()
{
    int u,k,v;
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++) M[i][j]=0;
    }
    for(int i=0;i<n;i++){
        cin>>u>>k;
        u--;
        for(int j=0;j<k;j++){
            cin>>v;
            v--;
            M[u][v]=1;
        }
    }
    for(int i=0;i<n;i++) d[i]=-1;
    bfs(0);
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)

int main() {
    int N; cin >> N;
    vector<vector<int>> G(N);
    rep(i, 0, N) {
        int u, k;
        cin >> u >> k;
        u--;
        rep(j, 0, k) {
            int v; cin >> v;
            v--;
            G[u].push_back(v);
        }
    }
    vector<int> dist(N, -1);
    queue<int> Q;
    Q.push(0);
    dist[0] = 0;
    while(!Q.empty()) {
        int cur = Q.front(); Q.pop();
        for(auto &to : G[cur]) {
            if(dist[to] != -1) continue;
            Q.push(to);
            dist[to] = dist[cur] + 1;
        }
    }
    rep(i, 0, N) cout << i + 1 << " " << dist[i] << '\n';
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define MAX 100
#define NIL -1

int n, G_list[MAX+1];
int G[MAX+1][MAX+1];
int d[MAX+1][2];

void make_graph(void){
  scanf("%d", &n);

  for(int i=0; i<n; i++){
    int cnt, num, m;

    scanf("%d %d", &num, &cnt);
    G_list[i]=num;

    for(int j=0; j<cnt; j++){
      scanf("%d", &m);
      G[num][m]=1;
    }
  }
}

void check_depth(void){
  queue<int> Q;
  Q.push(G_list[0]);
  d[G_list[0]][0]=0;
  d[G_list[0]][1]=NIL;
  
  while(!Q.empty()){
    int top=Q.front();
    Q.pop();
    
    for(int j=0; j<n; j++){
      if(G[top][G_list[j]] == 1 && d[G_list[j]][1] != NIL){
	d[G_list[j]][0]=d[top][0]+1;
	d[G_list[j]][1]=NIL;
	Q.push(G_list[j]);
      }
    }
  }
}

void print(void){

  for(int i=0; i<n; i++){
    printf("%d %d\n", G_list[i], d[G_list[i]][0]);
  }
}

int main(void){

  make_graph();
  check_depth();
  print();
  
  return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;

static const int N=100;
static const int INF=1e+9;

int n,M[N][N];
int d[N];

void bfs(int s){
	queue<int> q;
	q.push(s);

	for(int i=0;i<n;i++){
		d[i]=INF;
	}
	d[s]=0;
	int u;

	while(!q.empty()){
		u=q.front();
		q.pop();


		for(int v=0;v<n;v++){
			if(M[u][v]==0)continue;
			if(d[v]!=INF)continue;

			d[v]=d[u]+1;
			q.push(v);
		}
	}

	for(int i=0;i<n;i++){
		if(d[i]==INF){
			cout<<i+1<<" "<<-1<<endl;
		}else{
			cout<<i+1<<" "<<d[i]<<endl;
		}
	}
}
int main(){

	int u,k,v;

	cin>>n;

	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			M[i][j]=0;
		}
	}

	for(int i=0;i<n;i++){
		cin>>u>>k;
		u--;

		for(int j=0;j<k;j++){
			cin>>v;
			v--;
		
			M[u][v]=1;
		}
	}

	bfs(0);
	return 0;

}

C++: #include <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[102][102];
int A[100],f[100],w,h,n,sx,sy,gx,gy,e,g[100];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<100;i++){		
		A[i]=0;				
		f[i]=0;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
		//	printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			f[i]=g[d]+1;
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	return 0;
}