Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

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

#define int long long
 	
const int N = 100 + 1;
vector< int > g[N];
int d[N]; // stores the depth of all nodes

void BFS(int u){
   queue<int> q;
   q.push(u);
   d[u] = 0;
   while (!q.empty()){
	   int x = q.front(); q.pop();
	   for(auto it : g[x]){
		     if (d[it] != -1) continue;
		     d[it] = d[x] + 1;
		     q.push(it);
		 }  
	 }
}

int32_t main(){
   int n, m, deg;
   cin >> n;
   for (int i = 0; i < n; i++){
	    cin >> m >> deg;
	    m--;
	    for(int i = 0; i < deg; i++){
		    int x;
		    cin >> x;
		    x--;
		    g[m].push_back(x);
	    }  
	 }
	for (int i = 0; i < n; i++) d[i] = -1;
    BFS(0);
    for (int i = 0; i < n; i++){
	   int idx = i + 1;
	   if (d[i] == -1) cout << idx << " " << -1 << endl;
	   else cout << idx << " " << d[i] << endl; 
	 }
}

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

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

int d[MAX_NODE];
vector<int> adj[MAX_NODE];

int bfs(int n, int start){
	queue<int> que;
	for(int i=0;i<n;i++){
		d[i]=(i==start)?0:INF;
	}
	que.push(start);
	while(que.empty()==0){
		int from=que.front();
		que.pop();
		for(int i=0;i<adj[from].size();i++){
			int to=adj[from][i];
			if(d[to]==INF){
				d[to]=d[from]+1;
				que.push(to);
			}
		}
	}
}	

int main(){
	int n,u,k,v;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>u>>k;
		for(int j=0;j<k;j++){
			cin>>v;
			v--;
			adj[u-1].push_back(v);
		}
	}
	cout<<adj[0].size()<<endl;
	bfs(n,0);
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<d[i]<<endl;
	}
	return 0;
}

C++: #include <queue>
#include <vector>
#include <iostream>
using namespace std;
int N;
int D[110];
int G[110][110];
void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;
	while(!Q.empty()){
		int cur = Q.front();
		Q.pop();
		for(int i=0;i<N;i++){
			if(G[cur][i] && D[i]==0){
				D[i] = D[cur]+1;
				Q.push(i);
			}
		}
	}
	for(int i=0;i<N;i++){
		if(i!=src && D[i]==0) D[i]=-1;
	}
	D[src]=0;
}

int main(){
	while(cin >> N && N>0){
		vector<vector<int> > g(110,vector<int>(110,0));
		int u,k;
		for(int i=0;i<N;i++){
			cin >> u >> k;
			for(int j=0;j<k;j++){
				int a;
				cin >> a;
				g[u-1].push_back(a); 
			}
		}
		for(int i=0;i<N;i++){
			for(int j=0;j<g[i].size();j++){
				int f = g[i][j];
				G[i][f-1] = 1;
			}
		}
		bfs(0);
		for(int i=0;i<N;i++){
			cout << i+1 << " " << D[i] << endl;
		}
	}
}

#if 0
無限ループに陥らないようにするのに少し苦労した。プログラム上では番号を0から始めたのもよくないかもしれない。
#endif

C++: #include<stdio.h>
#include<queue>
#include<algorithm>

#define MAX 100

int G[MAX+1][MAX+1], d[MAX+1];
int n;
std::queue<int> Q;

void bfs() {
    d[1] = 0;

    Q.push(1);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 1; i <= n; ++i) {
            if (G[cur][i] == 0) continue;
            if (d[i] != -1) continue;

            Q.push(i);
            d[i] = d[cur] + 1;
        }
    }

}

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

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

}

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

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(q->size() > 0)
    {
        int u = q->front();
        q->pop();

        for (int i = 0; i < N; i++)
        {
            if (M[u][i] && C[i] == WHITE)
            {
                C[u] = GRAY;
                D[i] = D[u] + 1;
                q->push(i);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

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

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

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

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

    for (int i = 0; i < N; i++)
    {
        cout << i+1 << " " << D[i] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using Graph = vector<vector<int>>;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

int N, M, K, Q, W, H, R, C;

int main() {
    // cin.tie(0);
    // ios::sync_with_stdio(false);

    cin >> N;
    Graph G(N);
    REP(i, N) {
        int u, k;
        cin >> u >> k;
        u--;
        REP(j, k) {
            int v;
            cin >> v;
            v--;
            G[i].push_back(v);
        }
    }

    vector<int> dis(N + 5, -1);
    dis[0] = 0;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int u = que.front();
        que.pop();
        for (auto v : G[u]) {
            if (dis[v] == -1) {
                que.push(v);
                dis[v] = dis[u] + 1;
            }
        }
    }
    REP(i, N) { cout << i + 1 << " " << dis[i] << endl; }

    return 0;
}