Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include <climits>
#include <map>
#include <set>
const int mod = 1e9 + 7;
const long long INF = 1LL << 60;
using namespace std;
typedef long long ll;
 
static const int N = 100;
static const int INFTY = (1<<21);

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] = INFTY;
    } 
    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] != INFTY){
                continue;
            }
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for(int i = 0; i < n; i++){
        cout  << i + 1 << ' ' << ((d[i] == INFTY) ? (-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 <cstdio>
#include <queue>
using namespace std;

int dt[101][101];
int d[101];

int n;
int t=0;
void bfs(int s){
	priority_queue<int, vector<int>, greater<int> > que;
	for(int i=1;i<=n;i++)
		d[i]=-1;
	d[s]=0;
	que.push(s);
	while(!que.empty()){
		int nxt=que.top();que.pop();
		for(int i=1;i<=n;i++){
			if(dt[nxt][i]&&d[i]==-1){
				que.push(i);
				d[i]=d[nxt]+1;
			}
		}
	}
}
int main(){
	int fi, v, g;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d %d",&fi,&v);
		for(int j=1;j<=v;j++){
			scanf("%d",&g);
			dt[fi][g]=1;
		}
	}
	d[1]=0;
	bfs(1);
	for(int i=1;i<=n;i++){
		if(!d[i]){
			bfs(i);
		}
	}
			
	for(int i=1;i<=n;i++){
		printf("%d %d\n",i,d[i]);
	}
	return 0;
}

C++: #include <cstdio>
#include <queue>
using namespace std;
#define MAX_N 100

int g[MAX_N+1][MAX_N+1];
int d[MAX_N+1];
bool visited[MAX_N+1];
queue<int> que;

int n;
int t = 1;

void bfs(int id) {
  visited[id]=true;
  que.push(id);
  queue<int> next_que;
  while(!que.empty()) {
    int v = que.front(); que.pop();
    for(int u=1;u<=n;u++) {
      if(g[v][u]!=0 && !visited[u]) {
        visited[u]=true;
        next_que.push(u);
        d[u] = t;
      }
    }
    if(que.empty()){
      que = next_que;
      while(!next_que.empty()) next_que.pop();
      t++;
    }
  }
}

int main() {
  scanf("%d\n",&n);
  int u,k,v;
  for(int i=0;i<n;i++) {
    scanf("%d %d ",&u,&k);
    for(int j=0;j<k;j++) {
      scanf("%d ",&v);
      g[u][v] = 1;
    }
  }
  bfs(1);
  for(int i=1;i<=n;i++) {
    if(visited[i])
      printf("%d %d\n",i,d[i]);
    else
      printf("%d -1\n",i);
  }
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int N = 100;
static const int INFTY = (1 << 21);

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

void bfs(int s){
    for(int i = 0; i < n; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }

    queue<int> q;
    color[s] = GRAY;
    d[s] = 0;
    q.push(s);

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

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

int main(){
    cin >> n;
    int u, k, v;
    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;
        for(int j = 0; j < k; j++){
            cin >> v;
            M[u-1][v-1] = 1;
        }
    }
    bfs(0);

    return 0;
}

C++: // 2015/01/05 Tazoe

#include <iostream>
#include <queue>
using namespace std;

int main()
{
	int n;
	cin >> n;

	int G[100][100] = {0};

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

		for(int j=0; j<k; j++){
			int v;
			cin >> v;

			G[u-1][v-1] = 1;
		}
	}

	int d[100];
	for(int i=0; i<n; i++){
		d[i] = -1;
	}

	queue<int> Q;

	Q.push(0);
	d[0] = 0;

	while(!Q.empty()){
		int u = Q.front();

		int v = -1;
		for(int j=0; j<n; j++){
			if(G[u][j]==1 && d[j]==-1){
				v = j;
				break;
			}
		}

		if(v!=-1){
			Q.push(v);
			d[v] = d[u]+1;
		}
		else{
			Q.pop();
		}
	}

	for(int i=0; i<n; i++){
		cout << i+1 << ' ' << d[i] << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N=100;
static const int INFTY=(1<<21);

int n,M[N][N];
int d[N];
void kansuu(int s){
  queue<int>q;
  q.push(s);
  for(int i=0;i<n;i++){
    d[i]=INFTY;
  }
  d[s]=0;
  int u;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int i=0;i<n;i++){
      if(M[u][i]==0) continue;
      if(d[i]!=INFTY) continue;
      d[i]=d[u]+1;
      q.push(i);
    }
  }
  for(int i=0;i<n;i++){
    if(d[i]==INFTY) d[i]=-1;
    cout<<i+1<<" "<<d[i];
    printf("\n");
  }
}
int main(void){
  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;
    }
  }
  kansuu(0);
  return 0;
}