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 <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
mat g;

lli u,k,v;

queue<lli> q;
queue<lli> d;
vbl vis;
vll ans;


int main(){
    cin >> n;
    g = mat(n+1);
    vis = vbl(n+1);
    ans = vll(n+1);
    for(lli i = 0;i < n;i++){
        cin >> u >> k;
        for(lli j = 0;j < k;j++){
            cin >> v;
            g[u].push_back(v);
        }
    }
    q.push(1);
    d.push(0);
    while(!q.empty()){
        lli p = q.front();
        lli r = d.front();
        q.pop();
        d.pop();
        if(vis[p]) continue;
        vis[p] = true;
        ans[p] = r;
        for(lli i = 0;i < g[p].size();i++){
            if(!vis[g[p][i]]) q.push(g[p][i]),d.push(r+1);
        }
    }
    for(lli i = 1;i <= n;i++) cout << i << " " << ans[i] << endl;
    return 0;
}

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

int main() {
  int n;
  cin >> n;

  vector<vector<int>> M(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    int node;
    int d;
    cin >> node >> d;
    node--;

    for (int j = 0; j < d; j++) {
      int tmp;
      cin >> tmp;
      tmp--;
      M[i][tmp] = 1;
    }
  }

  vector<int> res(n, -1);
  vector<int> e(n);
  res[0] = 0;

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

  while (!q.empty()) {
    int num = q.front();
    q.pop();

    int cnt = res[num] + 1;

    for (int i = 0; i < M[num].size(); i++) {
      if (M[num][i] == 0) { continue; }
      if (e[i] == 1) { continue; }
      q.push(i);
      e[i]   = 1;
      res[i] = cnt;
    }
  }
  res[0] = 0;

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

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  rep(i,n){
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
       d[i][j]=100000;
      }
    }
  }
  rep(i,n){
    int u,k;
    cin >> u >> k;
    q1.push(u);
    if(k>0){
     int v[k];
     rep(j,k){
       cin >> v[j];
       q1.push(v[j]);
     }
     int f = q1.front()-1;
     q1.pop();
     rep(i,k){
       int e=q1.front()-1;
       d[f][e]=1;
       q1.pop();
     }
    }else{
     q1.pop();
    }
  }

  rep(i,n){
    for(j=2;j<n;j++){
    d[0][i]=min(d[0][i],d[0][j]+d[j][i]);
    }
  }
 
 rep(i,n){
   cout << i+1 << ' ' <<d[0][i] << endl;
 }
 
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
using Graph = vector<vector<int>>;

int main() {
    // 頂点数と辺数
    int N; cin >> N ;

    // グラフ入力受取 (ここでは無向グラフを想定)
    Graph G(N);
    for (int i = 0; i < N; ++i) {
        int a , b;
        cin >> a >> b;
        a = a - 1;
        if(b != 0){
        for(int j = 0; j < b; j++){
            int e1;
            cin >> e1;

            e1 = e1 - 1;
        //    G[e1].push_back(a);
            G[a].push_back(e1);
        }
        }

    }

    // BFS のためのデータ構造
    vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[0] = 0;
    que.push(0); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v]) {
            if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない
            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    // 結果出力 (各頂点の頂点 0 からの距離を見る)
    for (int v = 1; v <= N; ++v) cout << v << " " << dist[v - 1] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
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);
    }
}
#pragma endregion

int main()
{
    int n;
    cin >> n;
    vector<vector<int>> g(n, vector<int>());
    vector<int> d(n, -1);

    rep(i, n)
    {
        int k;
        cin >> k >> k;
        rep(j, k)
        {
            int t;
            cin >> t;
            t--;
            g[i].emplace_back(t);
        }
    }

    queue<int> q;
    q.push(0);
    d[0] = 0;
    while (q.size())
    {
        int v = q.front();
        q.pop();

        for (auto nv : g[v])
        {
            if (d[nv] >= 0)
                continue;

            d[nv] = d[v] + 1;
            q.push(nv);
        }
        // cout << v << " " << q.size() << endl;
    }

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

    return 0;
}

C++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=ja
*/

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

int main(){
    int n;
    cin >> n;
    vector<vector<int>> to(n);
    rep(i,n){
        int u,k;
        cin >> u >> k;
        rep(j,k){
            int v;
            cin >> v;
            v--;
            to[i].push_back(v);
        }
    }

    vector<int> dist(n,-1);
    queue<int> q;
    dist[0]=0;
    q.push(0);

    while(!q.empty()){
        int p;
        p = q.front();
        q.pop();
        //cout << "p" << p << endl;
        rep(i,to[p].size()){
            if(dist[to[p][i]]==-1){
                dist[to[p][i]]=dist[p]+1;
                q.push(to[p][i]);
            }
        }
    }

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