Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>

using namespace std;

const int MAX_N = 101;
const int DUMMY = 10000;

int N, u, k, v = 0;

vector<int>V [MAX_N];
int d [MAX_N];
bool visited [MAX_N];

bool bfs(int u, int depth)
{
    //if (V[u].size() == 0) return false;
    
    // for (int i = 0; i < V[u].size(); ++i)
    //     cout << V[u][i] << ' ';
    // cout << endl;
    
    for (int i = 0; i < V[u].size(); ++i)
        bfs(V[u][i] - 1, depth + 1);
    
    d[u] = min(d[u], depth);
    
    if (V[u].size() == 0 || u == 0)
        return true;
}

int main()
{
    //initialize
    for (int i = 0; i < MAX_N; ++i) {
        d[i]   = DUMMY;
        visited[i] = false;
    }
    
    //input
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) {
      scanf("%d %d", &u, &k);
      for (int j = 0; j < k; ++j) {
          scanf("%d", &v);
          V[i].push_back(v);
      }
      sort(V[i].begin(), V[i].end());
    }

    //serach
    bfs(1-1, 0);
    
    //output
    for (int i = 0; i < N; ++i) {
      if(d[i] == DUMMY) cout << i+1 << ' ' << -1 << endl;
      else cout << i+1 << ' ' << d[i] << endl;
    }

    return 0;
}

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;
}