Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;

#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[N+1][N+1];  // 隣接行列

void bfs(int n, int d[], int color[]) {
  int vertex;
  queue<int> Q;
  
  // 初期状態
  for (int i = 1; i <= n; i++) {
    d[i] = -1;
    color[i] = WHITE;
  }
  vertex = 1;  // 始点
  d[vertex] = 0;
  Q.push(vertex);
  //bfs
  while (!Q.empty()) {
    vertex = Q.front();
    Q.pop();
    color[vertex] = GRAY;
    // 見訪問の隣接する頂点を探してキューに入れる
    for (int i = 1; i <= n; i++) {
      if (M[vertex][i] && color[i] == WHITE) {
        color[i] = GRAY;
        Q.push(i);
        d[i] = d[vertex] + 1;  // 距離
      }
    }
    color[vertex] = BLACK;  // 処理終了
  }
}

int main() {
  int i, j, n, u, k, v, d[N+1], color[N+1];

  cin >> n;
  // 与えられた隣接リストを隣接行列表現に直す
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      M[i][j] = 0;
    }
  }
  for (i = 0; i < n; i++) {
    cin >> u >> k;
    for (j = 0; j < k; j++) {
      cin >> v;
      M[u][v] = 1;
    }
  }
      
  // bfs
  bfs(n, d, color);

  // 出力
  for (i = 1; i <= n; i++) cout << i << " " << d[i] << endl;

  return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
 
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
 
using namespace std;
 
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
 
const int INF=1<<29;
const double EPS=1e-9;
 
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

std::vector<vector<int> > G;
std::vector<int> flag;
std::vector<int> in;
int N;
typedef pii P;

int main(int argc, char const *argv[]) {
	
	cin >> N;

	for (int i = 0; i < N; ++i) {
		G.push_back(vector<int>());
		flag.push_back(0);
		in.push_back(-1);
		for (int j = 0; j < N; ++j) {
			G[i].push_back(0);
		}
	}

	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][v] = 1;
		}
	}
	queue<P> q;
	q.push(mp(0, 0));
	while(!q.empty()) {
		P u = q.front(); q.pop();
		int dist = u.second;
		in[u.first] = dist;
		flag[u.first] = 1;
		for (int i = 0; i < N; ++i) {
			if (flag[i] == 0 && G[u.first][i] == 1) {
				q.push(mp(i, dist + 1));
			}
		}
	}

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

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define ll long long
typedef complex<double> P;

int main() {
  int n; cin >> n;
  int adj[n][n] = {};

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

  int d[n];
  fill(d, d+n, -1);

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

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

    for (int i = 0; i < n; i++) {
      if(adj[v][i] == 1 && d[i] == -1) {
        que.push(i);
        d[i] = d[v] + 1;
      }
    }

  }

  for (int i = 0; i < n; i++) {
    cout << i+1 << ' ' << d[i] << 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);
  for(int i=0;i<n;i++){
    d[i]=INFTY;
  }
  d[s]=0;
  int u;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int 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(int i=0;i<n;i++){
    cout<<i+1<<" "<<((d[i]==INFTY)?(-1):d[i])<<endl;
  }
}

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

C++: //ALDS1_11_B:Depth First Search
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <random>
#include <time.h>
#include <cassert>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> v[101];
    for(int i=0; i<n; i++){
        int u,k;
        cin >> u >> k;
        for(int j=0; j<k; j++){
            int tmp;
            cin >> tmp;
            v[u].push_back(tmp);
        }
    }

    int d[101];
    for(int i=1; i<=n; i++) d[i] = -1;

    queue<int> que;
    que.push(1);
    int now = 0;
    while(!que.empty()){
        int tmp = que.size();
        for(int i=0; i<tmp; i++){
            if(d[que.front()] != -1){
                que.pop();
                continue;
            }

            d[que.front()] = now;
            for(int j=0; j<v[que.front()].size(); j++){
                if(d[v[que.front()][j]] == -1) que.push(v[que.front()][j]);
            }
            que.pop();
        }
        now++;
    }

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

C++: #include <iostream>
#include <queue>
#define INFTY -1
using namespace std;



int a[124][124] = {{}};
int d[124] = {};
int n;
void bfs(int k){
  queue <int> q;
  q.push(k);
  int u;


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

  }
}

int main(){

  int u,k,x;
  cin >> n;

d[0] = 0;
for(int i = 1; i < n;i++) d[i] = INFTY;

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

  bfs(0);

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

C++: #include <iostream>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <utility>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int g[101][101],n,d[101],f[101];

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][v]++;
        }
    }
    queue<int> q;
    q.push(1);
    d[1] = 0;
    while(!q.empty()) {
        int u = q.front();q.pop();
        for(int i = 1;i <= n;i++) {
            if(g[u][i] && d[i] == 0)
            {
                d[i] = d[u]+1;
                q.push(i);
            }
        }
    }
    for(int i = 1;i <=n;i++) {
        cout << i << " " << d[i] <<endl;
    }
}