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;
const int INF=1145141919;

#define debug cout<<"debug"<<endl;

int main(){

  int n;
  cin>>n;

  vector <int> g[n];

  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--;
      g[u].push_back(v);
    }

  }

  int dis[n];
  fill(dis,dis+n,INF);
  dis[0]=0;

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

  for(;!bfs.empty();){

    int check=bfs.front(); bfs.pop();

    for(int i=0;i<(int)g[check].size();i++){

      int next=g[check][i];

      if(dis[next]>dis[check]+1){
	dis[next]=dis[check]+1;
	bfs.push(next);
      }

    }
  }

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

  return 0;

}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int matrix[110][110];
int dist[110];
int visited[110];
int n;
queue<int> Q;

int main() {
  REP(x, 110) {
    REP(y, 110) {
      matrix[x][y] = 0;
    }
    dist[x] = (1 << 15);
    visited[x] = 0;
  }

  // int n;
  cin >> n;

  REP(_, n) {
    int v, num;
    cin >> v >> num;

    REP(i, num) {
      int cv;
      cin >> cv;
      matrix[v][cv] = 1;
    }
  }

  dist[1] = 0;
  Q.push(1);

  while (!Q.empty()) {
    int f = Q.front(); Q.pop();
    visited[f] = 1;

    // int updated = 0;
    FOR(v, 1, n + 1) {
      if (matrix[f][v]) {
        if (dist[v] > dist[f] + 1) {
          dist[v] = dist[f] + 1;
          // updated = 1;
          // cout << "update for " << v  << endl;
        }
        if (!visited[v]) {
          Q.push(v);
        }
      }
    }
    // if (updated == 0 && Q.empty()) {
    //   cout << "break!" << endl;
    //   break;
    // }
  }


  FOR(v, 1, n + 1) {
    if (dist[v] == (1 << 15)) {
      cout << v << " -1" << endl;
    } else {
      cout << v << " " << dist[v] << endl;
    }
  }


  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stack>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <stdio.h>
#include <math.h>


using namespace std;

typedef long long ll;

typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;

typedef pair<int, int> P;

typedef queue<int> qi;
typedef queue<string> qs;


#define rep(i, n) for(int i=0; i<(n); ++i)
#define repp(i, a, b) for(int i = (a); i <= (b); i++)
#define Rep(i, n) for(int i=n; i >= 0; i--)

#define all(v) v.begin(), v.end()
//sort( all(v) )　などと使える

int main() {
	int n; cin >> n;
	vector<vector<int>> G(n);
	rep(i, n) {
		int u, k; cin >> u >> k;
		u--;
		//G[u].resize(k);

		rep(j, k) {
			int v; cin >> v;
			v--;
			G[u].push_back(v);
		}
	}

	vector<int> dist(n, -1);

	dist[0] = 0;
	queue<int> que;
	que.push(0);

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

		for (auto nv : G[v]) {
			if (dist[nv] != -1) continue;
			else {
				dist[nv] = dist[v] + 1;
				que.push(nv);
			}
		}
	}

	rep(i, n) {
		cout << i + 1 << " " << dist[i] << endl;
	}

	//return 0;
}

C++: #include <cstdio>

void search(int (*A)[101], int *dis, int *found, int n){
    int hoge;
    int time=0;
    for(int k=1;k<n;k++){
        hoge = 1;
        for(int i=1;i<=n;i++){
            if(dis[i]==time){
                for(int j=1;j<=n;j++){
                    if(A[i][j]&&found[j]==0){
                        found[j]=1;
                        dis[j]=time+1;
                        hoge=0;
                    }
                }
            }
        }
        time++;
        if(hoge)return;
    }
}

int main(){
    int i, j, n, u, k, v;
    scanf("%d",&n);
    int found[n+1];
    int dis[n+1];
    dis[1]=0;
    found[1]=1;
    for(i=2;i<=n;i++){
        dis[i]=-1;
        found[i]=0;
    }
    int A[101][101];
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            A[i][j]=0;
        }
    }
    for(i=1;i<=n;i++){
        scanf("%d %d",&u,&k);
        for(j=0;j<k;j++){
            scanf("%d",&v);
            A[u][v]=1;
        }
    }
    search(A,dis,found,n);
    for(i=1;i<=n;i++)printf("%d %d\n",i,dis[i]);
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <queue>
#define N 110
#define INF 0x3f3f3f3f
using namespace std;
//stack<int> s;
int n;
int m[N][N];
int used[N];
int d[N];
void bfs(int root){
    queue<int>q;
    while(!q.empty()) q.pop();
    q.push(root);
    d[root] = 0;
    while(!q.empty()){
        int u = q.front();
        used[u] = 1;
        q.pop();
        for(int i = 1; i <= n; i++){
            if(used[i] == 0 && m[u][i]){
                q.push(i);
                d[i] = d[u] + 1;
                used[i] = 1;
            }
        }
    }
}
int main(){
    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++) used[i] = 0;
    for(int i = 0; i < N; i++) d[i] = INF;
    scanf("%d", &n);
    for(int p = 0; p < n; p++){
        int node, k;
        scanf("%d %d", &node, &k);
        for(int i = 0; i < k; i++){
            int b;
            scanf("%d", &b);
            m[node][b] = 1;
        }
    }
    bfs(1);
    //for(int i = 1; i <= n; i++) if(used[i] == 0) bfs(i);
    for(int i = 1; i <= n; i++){
        printf("%d %d\n", i, d[i] == INF ? -1 : d[i]);
    }
    return 0;
}