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>
using namespace std;
#include<queue>
queue<int>mp;
#define N 105
#define INF 1<<21
int a[N][N];
int d[N];
int n;

void bfs(int s){
	mp.push(s);
	for (int i = 1;i <= n;i++){
		d[i] = INF;
	}
	d[s] = 0;
	int u;
	while (!mp.empty()){
		u = mp.front();
		mp.pop();
		for (int v = 1;v <= n;v++){
			if (a[u][v] == 0) continue;
			if (d[v] != INF) continue;
			d[v] = d[u] + 1;
			mp.push(v);
		}
	}
	for (int i = 1 ;i <= n;i++){
		if (d[i] == INF){
			cout << i << " " << "-1" << endl; 
		}
		else{
			cout << i << " " << d[i] << endl;
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++){
		for (int j = 1;j <= n;j++){
			a[i][j] = 0;
		}
	}
	int x,m,y;
	for (int i = 1;i <= n;i++){
		cin >> x >> m;
		while (m--){
			cin >> y;
			a[x][y] = 1;
		}
	}
	bfs(1);
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i= (a); i<(b); i++)
#define REP(i,n) for(int i=0;i < (n);i++)
#define ALL(obj) (obj).begin(),(obj).end()
using namespace std;

const long long INF = 1LL << 60;
const int MOD = (int)1e9 + 7;
typedef long long ll;
//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    //sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
vector<vector<int>> graph;
vector<int> ans;
signed main () {
    int n;scanf("%d",&n);
    graph.assign(n,vector<int>(n));
    ans.assign(n,-1);
    REP(i,n){
        int id,tmp;scanf("%d %d",&id,&tmp);
        REP(j,tmp){
            int t;scanf("%d",&t);
            graph[i].push_back(t-1);
        }
    }
    queue<pair<int,int>> q;
    q.push(make_pair(0,0));ans[0] = 0;
    while(!q.empty()){
        pair<int, int> tmp = q.front();q.pop();
        int ind = tmp.first;int cnt = tmp.second;
        for(int next : graph[ind]){
            if(ans[next]!=-1)continue;
            ans[next] = cnt+1;
            q.push(make_pair(next, cnt+1));
        }
    }
    REP(i,n){
        printf("%d %d\n",i+1,ans[i]);
    }
}

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


#define ALREADY 1
#define YET 0

int *passed;
int *distance_ps;

using namespace std;


void bfs(vector< vector<int> > a, int i, int dis){
  // i?????????vector<int>??????????????¢?????????
  dis++;
  vector<int> ai = a[i];
  int n = ai.size();
  // cout << "i: " << i << ",n: " << n << endl;
  for(int j=0;j<n;j++){
    int nexti=ai[j]-1;
    
    if(passed[nexti]==YET || distance_ps[nexti]>dis){
      passed[nexti]=ALREADY;
      distance_ps[nexti]=dis;
      bfs(a, nexti, dis);
    }
  }
}

int main(){
  int n;
  cin >> n;
  vector< vector<int> > a;
  for(int i=0;i<n;i++){
    int u;
    int k;
    cin >> u >> k;
    vector<int> tmpa;
    for(int j=0;j<k;j++){
      int tmp;
      cin >> tmp;
      tmpa.push_back(tmp);
    }
    a.push_back(tmpa);
  }
  
  int a2[n][n];
  passed = new int[n];
  distance_ps = new int[n];
  
  for(int i=0;i<n;i++){
    passed[i] = 0;
    distance_ps[i] = 1000;
  }

  distance_ps[0]=0;
  bfs(a, 0, 0);

  for(int i=0;i<n;i++){
    if(passed[i]==YET){
      distance_ps[i] = -1;
    }
  }
  distance_ps[0]=0;
  
  for(int i=0;i<n;i++){
    cout << i << " " << distance_ps[i] << endl;
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      a2[i][j]=0;
    }
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<(int)a[i].size();j++){
      a2[i][a[i][j]-1] = 1;
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      // if(j!=0)cout << " ";
      // cout << a2[i][j];
    }
    // cout << endl;
  }
      
  
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

void bfs(int s) {
	queue<int> q;
	q.push(s);
	int i, u, v;
	for ( i = 0; i < n; i++ ) d[i] = INFTY;
	d[s] = 0;

	while ( !q.empty() ) {
		u = q.front();
		q.pop();
		for ( v = 0; v < n; v++ ) {
			if ( M[u][v] == 0 ) continue;
			if ( d[v] != INFTY ) continue;
			d[v] = d[u] + 1;
			q.push(v);
			}
		}

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

int main() {
	int u, k, v, i, j;

	cin >> n;

	for ( i = 0; i < n; i++ ) {
		for ( j = 0; j < n; j++ ) M[i][j] = 0;
	}

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

	bfs(0);

	return 0;
}

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

#define REP(i, start, count) for(int i=(start); i<(int)(count); ++i)
#define rep(i, count) REP(i, 0, count)
#define ALLOF(c) (c).begin(), (c).end()

typedef long long ll;
typedef unsigned long long ull;

int n;
vector<vector<int>> G;

int main(void) {

    cin >> n;
    G.push_back(vector<int>(0));

    for (int i = 1; i <= n; ++i) {
        int u, k;
        cin >> u >> k;
        G.push_back(vector<int>());
        for (int m = 0; m < k; ++m) {
            int v;
            cin >> v;
            G[u].push_back(v);
        }
    }

    queue<int> Q;
    vector<int> color(n + 1, 0);
    vector<int> d(n + 1, -1);
    Q.push(1);
    color[1] = 1;
    d[1] = 0;

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

        for (int v : G[idx]) {
            // 未訪問
            if (color[v] == 0) {
                d[v] = d[idx] + 1;
                Q.push(v);
                color[v] = 1;
            }
        }

        color[idx] = 2;
        Q.pop();
    }

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

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

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)
using namespace std;

int main(){
    int n,u,k,v,src=0;
    cin >> n;
    int D[n],g[n][n];
    REP(i,n){
        D[i] = 0;
        REP(j,n){
            g[i][j] = 0;
        }    
    }
    REP(i,n){ 
        cin >> u >> k;
            REP(j,k){
                cin >> v;
                g[v-1][u-1] = 1;
            }
    }

    queue<int> Q;
    Q.push(src);
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
            REP(i,n){
                if(g[i][cur] == 1 && D[i] == 0){
                    D[i] = D[cur] + 1;
                    Q.push(i);
                }
            }
    }

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