Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INF = (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] = INF;
	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] != INF)continue;
			d[v] = d[u]+1;
			q.push(v);
		}
	}
	for(int i=0;i<n;i++){
		cout << i+1 << " " <<((d[i] == INF)?(-1):d[i])<<endl;
	}
}

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

C++: #include<iostream>
#include<queue>
using namespace std;
const int MAX = 100;
int n, G[MAX + 1][MAX + 1];
bool book[MAX + 1] = { false };
int path[MAX + 1];
void breathFSearch(int orign)
{
	queue<int> Q;
	Q.push(orign);
	book[orign] = true;
	while (!Q.empty())
	{
		int u = Q.front();
		for (int v = 1; v <= n; v++)
		{
			if (G[u][v] && !book[v])
			{
				Q.push(v);
				book[v] = true;
				if(path[u]!=-1)
					path[v] = path[u]+1;
			}
		}
		Q.pop();
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int u, d;
		cin >> u >> d;
		for (int j = 1; j <= d; j++)
		{
			int v;
			cin >> v;
			G[u][v] = 1;
		}
	}
	for (int i = 1; i <= n; i++)
		path[i] = -1;
	path[1] = 0;
	for(int i=1;i<=n;i++)
		if(!book[i])
			breathFSearch(i);
	for (int i = 1; i <= n; i++)
		cout << i << " " << path[i] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

vector<vector<int> > G;
// bfs 幅優先探索
int bfs(int s)
{
    vector<int> cost(G.size(), -1);

    cost[s] = 0;
    queue<int> pos_queue;
    pos_queue.push(s);

    while (pos_queue.size())
    {
        int pos = pos_queue.front();
        pos_queue.pop();
        rep(i, G[pos].size())
        {
            int next = G[pos][i];

            if (cost[next] == -1)
            {
                cost[next] = cost[pos] + 1;
                pos_queue.push(next);
            }
        }
    }
    rep(i, G.size())
    {
      cout << i + 1 << " " << cost[i] << endl;
    }
}

int main()
{
  int N;
  cin >> N;
  G.resize(N);
  rep(i, N)
  {
    int u, k;
    cin >> u >> k;
    u--;
    rep(j, k)
    {
      int v;
      cin >> v;
      G[u].push_back(v - 1);
    }
  }
  bfs(0);
  return 0;
}

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

int main(){
  int n, u, k, tmp, lp;
  vector<int> d;
  vector< vector<int> > v;
  vector< queue<int> > q;
  
  cin >> n;
  v.resize(n+1);
  d.resize(n+1, 1000000007);
  q.resize(1);
  d[1]=0;
  q[0].push(1);
  
  for (int r = 0; r < n; r++) {
	cin>> u >> k;
    for(int i=0;i<k;i++){
      cin>> tmp;
      v[u].push_back(tmp);
    }
  }
  
  for(int r=0;r<n;r++){
    //cout<< "r=" << r <<endl;
    q.resize(q.size()+1);
    lp=q[r].size();
    for(int i=0;i<lp;i++){
      tmp=q[r].front();
      //cout<< "tmp=" << tmp <<endl;
      q[r].pop();
      for(int j=0;j<v[tmp].size();j++){
        //cout<< "push " << v[tmp][j] << "  val " << min(v[tmp][j], d[tmp]+1) <<endl;
        if(d[v[tmp][j]]>d[tmp]+1){
          q[r+1].push(v[tmp][j]);
      		d[v[tmp][j]]=min(d[v[tmp][j]], d[tmp]+1);
        }
      }
    }
  }
  
  for(int i=1;i<=n;i++){
    cout<< i << " ";
    if(d[i]==1000000007) cout<< "-1" <<endl;
    else cout<< d[i] <<endl;
  }
  
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define REAP(i,a,n) for(int i=(a);i<(n);i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(),v.end()
typedef long long ll;

int table[101][101];

void init(){
    REP(i,101){
        REP(j,101){
            table[i][j]=1e9;
            if(i==j)table[i][j]=0;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    init();
    int u,k,v;
    REP(i,n){
        cin >> u >> k;
        REP(j,k){
            cin >> v;
            table[u-1][v-1]=1;
        }
    }

    REP(k,n){
        REP(i,n){
            REP(j,n){
                table[i][j]=min(table[i][j],table[i][k]+table[k][j]);
            }
        }
    }
    REP(i,n){
        cout << i+1 << " " << ((table[0][i]<n)?table[0][i]:-1);
        cout << endl;
    }
}

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

int main() {
    vector<int> G[101];
    int N; cin >> N;
    for(int i = 0; i < N; i++) {
        int u, k; cin >> u >> k;
        while(k > 0) {
          int v; cin >> v; 
          G[u].push_back(v);
          k--;
        }
    }

    vector<int> dist(N+1, -1);
    queue<int> Q;
    Q.push(1);
    dist[1] = 0;

    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(int i = 0; i < (int)G[u].size(); i++) {
            int v = G[u][i];
            if(dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            Q.push(v);
        }
    }
    for(int i = 1; i < N+1; i++) cout << i << ' ' << dist[i] << endl;
}

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

using namespace std;

#define MAX 100
#define INF 1e9

int n;
int G[MAX][MAX];
int dist[MAX];

void bfs(){
  bool used[MAX];
  fill(dist,dist+n,INF);
  fill(used,used+n,false);
  dist[0] = 0;

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

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

    for(int i = 0 ; i < n ; i++){
      if(!used[i] && G[v][i]){
	used[i] = true;
	dist[i] = dist[v] + 1;
	Q.push(i);
      }
    }
  }
}

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-1][v-1] = 1;
    }
  }

  bfs();

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

  return 0;
}