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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

vector<vector<int> > G;
// bfs 幅優先探索
int bfs(int s)
{
    vector<int> cost(G.size(), -1);

    cost[s] = 0;
    queue<int> pos_queue;
    pos_queue.push(s);

    while (pos_queue.size())
    {
        int pos = pos_queue.front();
        pos_queue.pop();
        rep(i, G[pos].size())
        {
            int next = G[pos][i];

            if (cost[next] == -1)
            {
                cost[next] = cost[pos] + 1;
                pos_queue.push(next);
            }
        }
    }
    rep(i, G.size())
    {
      cout << i + 1 << " " << cost[i] << endl;
    }
}

int main()
{
  int N;
  cin >> N;
  G.resize(N);
  rep(i, N)
  {
    int u, k;
    cin >> u >> k;
    u--;
    rep(j, k)
    {
      int v;
      cin >> v;
      G[u].push_back(v - 1);
    }
  }
  bfs(0);
  return 0;
}

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

int main(){
  int n, u, k, tmp, lp;
  vector<int> d;
  vector< vector<int> > v;
  vector< queue<int> > q;
  
  cin >> n;
  v.resize(n+1);
  d.resize(n+1, 1000000007);
  q.resize(1);
  d[1]=0;
  q[0].push(1);
  
  for (int r = 0; r < n; r++) {
	cin>> u >> k;
    for(int i=0;i<k;i++){
      cin>> tmp;
      v[u].push_back(tmp);
    }
  }
  
  for(int r=0;r<n;r++){
    //cout<< "r=" << r <<endl;
    q.resize(q.size()+1);
    lp=q[r].size();
    for(int i=0;i<lp;i++){
      tmp=q[r].front();
      //cout<< "tmp=" << tmp <<endl;
      q[r].pop();
      for(int j=0;j<v[tmp].size();j++){
        //cout<< "push " << v[tmp][j] << "  val " << min(v[tmp][j], d[tmp]+1) <<endl;
        if(d[v[tmp][j]]>d[tmp]+1){
          q[r+1].push(v[tmp][j]);
      		d[v[tmp][j]]=min(d[v[tmp][j]], d[tmp]+1);
        }
      }
    }
  }
  
  for(int i=1;i<=n;i++){
    cout<< i << " ";
    if(d[i]==1000000007) cout<< "-1" <<endl;
    else cout<< d[i] <<endl;
  }
  
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define REAP(i,a,n) for(int i=(a);i<(n);i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(),v.end()
typedef long long ll;

int table[101][101];

void init(){
    REP(i,101){
        REP(j,101){
            table[i][j]=1e9;
            if(i==j)table[i][j]=0;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    init();
    int u,k,v;
    REP(i,n){
        cin >> u >> k;
        REP(j,k){
            cin >> v;
            table[u-1][v-1]=1;
        }
    }

    REP(k,n){
        REP(i,n){
            REP(j,n){
                table[i][j]=min(table[i][j],table[i][k]+table[k][j]);
            }
        }
    }
    REP(i,n){
        cout << i+1 << " " << ((table[0][i]<n)?table[0][i]:-1);
        cout << endl;
    }
}

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

int main() {
    vector<int> G[101];
    int N; cin >> N;
    for(int i = 0; i < N; i++) {
        int u, k; cin >> u >> k;
        while(k > 0) {
          int v; cin >> v; 
          G[u].push_back(v);
          k--;
        }
    }

    vector<int> dist(N+1, -1);
    queue<int> Q;
    Q.push(1);
    dist[1] = 0;

    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(int i = 0; i < (int)G[u].size(); i++) {
            int v = G[u][i];
            if(dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            Q.push(v);
        }
    }
    for(int i = 1; i < N+1; i++) cout << i << ' ' << dist[i] << endl;
}

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

using namespace std;

#define MAX 100
#define INF 1e9

int n;
int G[MAX][MAX];
int dist[MAX];

void bfs(){
  bool used[MAX];
  fill(dist,dist+n,INF);
  fill(used,used+n,false);
  dist[0] = 0;

  queue<int> Q;
  Q.push(0);

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

    for(int i = 0 ; i < n ; i++){
      if(!used[i] && G[v][i]){
	used[i] = true;
	dist[i] = dist[v] + 1;
	Q.push(i);
      }
    }
  }
}

int main(){
  cin >> n;

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

  bfs();

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

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll, ll> Pll;
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
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; }

const int MOD = 1000000007;
const int INF = 1e9;

const int mod = 1000000007;
const int inf = 1e9;
#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};

const int mxN = 103;
vector<int> adj[mxN];

int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int v, k;
        cin >> v >> k;
        for(int j = 0; j < k; j++) {
            int a;
            cin >> a;
            a--;
            adj[i].push_back(a);
        }
    }

    vector<int> dis(n, INF);
    dis[0] = 0;
    queue<int> Q;
    Q.push(0);

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

        for(auto e: adj[p]) {
            if(dis[e] == INF) {
                dis[e] = dis[p] + 1;
                Q.push(e);
            }
        }
    }

    for(int i = 0; i < n; i++) {
        cout << i+1 << " " << (dis[i]==INF?-1:dis[i]) << endl;
    }
}