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 <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

static const int N = 100;
int d[N];
int n,M[N][N];

void dfs() {
	queue<int> q;
	q.push(0);
	int i;
	rep(i, n) d[i] = INF;
	d[0] = 0;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		int v;
		rep(v, n) {
			if (M[u][v] == 0) continue;
			if (d[v] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	rep(u, n) {
		cout << u + 1 << ' ' << ((d[u] == INF) ? (-1) : d[u]) << '\n';
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin >> n;
	int i;
	rep(i, n) {
		int u, k, v;
		cin >> u >> k;
		u--;
		int j;
		rep(j, k) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	dfs();
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

struct VRTX {
	int v[101] = {}, dist = -1;
};

int n, curdist = 0;
VRTX u[101];

void breadthFirstSearch(int x, int dist);

int main() {
	int i, j, id, dim;
	
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> id >> dim;
		for (j = 0; j < dim; j++) {
			cin >> u[id].v[j];
		}
	}

	breadthFirstSearch(1, 0);

	for (i = 1; i <= n; i++) {
		cout << i << " " << u[i].dist << endl;
	}

	return 0;
}

void breadthFirstSearch(int x, int dist) {
	int i, nextdist;

	if (u[x].dist == -1)u[x].dist = dist;
	else if (u[x].dist <= dist)return;
	else u[x].dist = dist;
	
	i = 0;
	nextdist = u[x].dist + 1;
	while (u[x].v[i] != 0) {
		breadthFirstSearch(u[x].v[i], nextdist);
		i++;
	}
}

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

int M[200][200];
string color[200];
queue<int> q;
int d[200];
int n;
const int INF = 1001001001;

void bfs(int s){
    rep(i, n){
        color[i] = "WHITE";
        d[i] = -1;
    }

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

int main() {
    cin >> n;
    rep(i, n){
        int a, b; cin >> a >> b;
        rep(j, b){
            int x; cin >> x;
            x--;
            M[i][x] = 1;
        }
    }
    bfs(0);
    rep(i, n){
        cout << i+1 << " " << d[i] << endl;
    }

    return 0;
}

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

using namespace std;
using Graph=vector<vector<int>>;

void breadth_firstsearch(Graph &graph,int vertex,queue<int> qdata,vector<int> &distance){
  distance.at(vertex)=0;
  qdata.push(vertex);
  while(!qdata.empty()){
    int ver=qdata.front();
    qdata.pop();
    for(int v : graph.at(ver)){
      if(distance.at(v)!=-1) continue;
      distance.at(v)=distance.at(ver)+1;
      qdata.push(v);
    }
  }
}


int main(void) {
	int size, vnum, adjnum, temp;
	cin >> size;
	Graph graph(size);
	queue<int> qdata;
	vector<int> distance(size,-1);

	for (int i = 0; i < size; i++) {
		cin >> vnum >> adjnum;
		for (int j = 0; j < adjnum; j++) {
			cin >> temp;
			graph.at(vnum - 1).push_back(temp - 1);
		}
	}
  breadth_firstsearch(graph,0,qdata,distance);

	for (int i = 0; i < size; i++) {
		cout << i + 1 << " " <<distance.at(i)<< endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for(int i=0;i<(n);i++)
const int INF = 1001001001;
int n, u, k;
int D[100];
vector<int> edges[100];
void bfs(int src) {
	
	
	queue<int> Q;	
	Q.push(src);
	D[src] = 0;//D[*]が始点から点*への距離
	while (!Q.empty()) {
		int cur = Q.front();//curは今取り扱ってる点
		Q.pop();
		
		for (int dst = 0; dst < n; dst++) {//dstはcurから移動可能な点
			vector<int>::iterator result = find(edges[cur].begin(), edges[cur].end(), dst);
			if (result != edges[cur].end() && D[dst] == INF) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
}

int main() {
	cin >> n; //n頂点
	

	for (int i = 0; i < n; i++)//iは頂点
	{
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			int l;
			cin >> l;
			edges[u - 1].push_back(l-1);
		}//入力終わり
		D[i] = INF;
	}
		bfs(0);
	for (int i = 0; i < n; i++) {
		if (D[i] != INF)cout << i + 1 << " " << D[i] << endl;
		else cout << i + 1 << " " << -1 << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<deque>
#include<cmath>
#include<stack>
#define rep(i,n) for(int i = 0;i<n;i++)
#define _GLIBCXX_DEBUG
using ll = long long; 
using namespace std;

static const int N = 100;
static const int INF = (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] = 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++) {
        cout << i+1 << " " << ( (d[i] == INF) ? (-1):d[i]) << endl;
    }}

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