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>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
//#define INF 1e18
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;

vector<int> E[SIZE];
int d[SIZE];

void bfs(int s){
  queue<int> que;
  que.push(s);
  fill(d,d+SIZE,INF);
  d[s] = 0; 
  while(!que.empty()){
    int v = que.front();que.pop();
    for(int to : E[v]) if(d[to] == INF){
      d[to] = d[v]+1;
      que.push(to);
    }
  }
}

int main(){
  cin >> n;
  rep(i,0,n){
    int u,k;
    cin >> u >> k;
    rep(i,0,k){
      int v;
      cin >> v;
      E[u].push_back(v);
    }
  }
  bfs(1);
  rep(i,1,n+1){
    cout << i << " ";
    if(d[i] == INF)cout << -1 << endl;
    else cout << d[i] << endl;
  }

  return 0;
}

C++: // 幅優先探索
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

#include<queue>

using namespace std;

#define MAX_N 100
// 値の定義
const int INV = -1;
const int EXIST = 1;
const int NO_EXIST = 0;

const int YES = 0;
const int NO = -1;

// グラフ情報を保持する配列
int ary_graph[MAX_N + 1][MAX_N + 1];

// 頂点１からの距離を保持する配列
int vertex_distance[MAX_N + 1];
// 頂点１からの訪問状態を保持する配列
int ary_visit[MAX_N + 1];

int n = 0;
int nt[MAX_N + 1];

void widthFirstSearch(int start_vertex);

int main(void)
{
    int u, num, v_num;

    for(int i = 0; i < MAX_N + 1; i++)
    {
    	vertex_distance[i] = 0;
        ary_visit[i] = NO;

        for(int j = 0; j < MAX_N + 1; j++)
        {
            // 配列の初期化
            ary_graph[i][j] = NO_EXIST;
        }
    }

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> u >> num;

        // 0オリジンにする
        u--;

        for(int j = 0; j < num; j++)
        {
            // 隣接点を記憶する
            cin >> v_num;

            // 0オリジンにする
            v_num--;
            ary_graph[u][v_num] = EXIST;
        }
    }

    // 頂点１から幅優先探索を実施
    widthFirstSearch(0);

    // 結果の表示
    for(int i = 0; i < n; i++)
    {
        if( i != 0 && vertex_distance[i] == 0 )
        {
            // 開始点から到達不可能な点は, 距離に-1を設定
            // ただし, 開始点自身は除く
            vertex_distance[i] = INV;
        }

        printf("%d %d\n", i + 1, vertex_distance[i]);
    }

    return 0;
}

// u に隣接する v を番号順に取得
int next(int u)
{
    int ret = INV;

    for(int v = nt[u]; v < n; v++)
    {
        nt[u] = v + 1;

        if( ary_graph[u][v] == EXIST )
        {
            // 隣接点が存在する
            ret = v;
            break;
        }
    }

    return ret;
}

// 幅優先探索を実施する関数
// start_vertex : 起点となる頂点のNo.
// vertex_num : グラフの頂点数
void widthFirstSearch(int start_vertex)
{

    queue<int> Q;

    Q.push(start_vertex);
    vertex_distance[start_vertex] = 0;
    ary_visit[start_vertex] = YES;

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

        for(int i = 0; i < n; i++)
        {
            // 隣接点を全て検索する
            int v = next(u);

            if( v == INV )
            {
                // 隣接点が無くなったらキューから削除して, 処理終了
                Q.pop();
                break;
            }

            if( ary_visit[v] == NO
                && ary_graph[u][v] == EXIST )
            {
                // 訪問済み
                ary_visit[v] = YES;

                Q.push(v);
                vertex_distance[v] = vertex_distance[u] + 1;
            }
        }
    }

    return;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;


int main(){
        int n;
        cin >> n;
        vector<vector<int>> Graph(n,vector<int>());
        rep(i,n){
                 int u,k;
                cin >>u >>k;
                --u;
                rep(j,k){
                         int p;
                        cin >> p;
                        --p;
                        Graph.at(u).push_back(p);
                }
        }
        
        vector<int> seen(n,-1);
        queue<int> q;
        q.push(0);
        seen.at(0) = 0;
        while(!q.empty()){
                int now = q.front();
                q.pop();
                for(int p:Graph.at(now)){
                        if(seen.at(p) >= 0) continue;
                        seen.at(p) = seen.at(now) + 1;
                        q.push(p);
                }
        }
        
        rep(i,n){
                 cout << i+1 << " " << seen.at(i) << endl;
        }                

        return 0;
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

typedef long long int ll;

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 ll INF=(ll)1e19;
//const int INF=(1<<30);

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

  vector<vector<int>> edge(n,vector<int>());
  REP(i,n){
    int node, k;
    cin >> node;
    cin >> k;
    REP(j,k){
      int v;
      cin >> v;
      v--;
      edge[i].emplace_back(v);
    }
  }

  VI dist(n,-1);

  queue<int> q;
  q.push(0);
  dist[0]=0;
  
  while(!q.empty()){
    int v = q.front(); q.pop();

    for(auto &iter: edge[v]){
      if(dist[iter]!=-1)continue;

      dist[iter] = dist[v]+1;
      q.push(iter);
    }
  }

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