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 <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  int n; cin >> n;
  vector<int> u(n), k(n); 
  vector<vector<int>> v(n);
  REP(i, n) {
    cin >> u[i] >> k[i];
    REP(m, k[i]) {
      int p; cin >> p;
      v[i].push_back(p);
    }
  }

  vector<int> ans; ans.assign(n + 1, -1);
  queue<int> Q;
  Q.push(1); ans[Q.front()] = 0;
  while(!Q.empty()) {
    int i = Q.front();
    Q.pop();
    for(int m = 0; m < k[i - 1]; ++m) {
      if(ans[v[i - 1][m]] == -1) {
        Q.push(v[i - 1][m]);
        ans[v[i - 1][m]] = ans[i] + 1;
      }
    }
  }
  for(int i = 1; i < n + 1; ++i) {
    cout << i << " " << ans[i] << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <unordered_map>
#include <stdio.h>
#include <list>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; ++i)
typedef long long ll;

int main()
{
	int n; cin >> n;
	vector <vector<int>> v(n);
	rep(i,n)
	{
		int hoge;
		cin >> hoge;
		int k;
		cin >> k;
		rep(j,k)
		{
			int foo;
			cin >> foo;
			v[i].push_back(foo-1);
		}
	} 
	vector<int> color(n);
	rep(i,n)color[i]=0;
	queue<int> Q;
	vector <int> d(n);
	rep(i,n)d[i]=-1;

	color[0] = 1;
	d[0]=0;
	Q.push(0);
	while(!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		rep(i,v[u].size())
		{
			if(color[v[u][i]]==0)
			{
				color[v[u][i]]=1;
				d[v[u][i]]=d[u]+1;
				Q.push(v[u][i]);
			}
		}
	}

	rep(i,n)cout<<i+1<<' '<<d[i]<<endl;
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include <climits>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;



int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<vector<int> > edge(n,vector<int>());
	REP(i,n){
		int v;cin>>v;v--;int t;cin>>t;
		while(t--){
			int u;cin>>u;u--;
			edge[v].push_back(u);
		}
	}
	queue<pair<int,int> > que;
	int mindist[103];
	fill_n(mindist,n,INF);
	mindist[0] = 0;
	que.push(make_pair(0,0));
	while(!que.empty()){
		pair<int,int> v = que.front();
		que.pop();
		v.second++;
		REP(i,edge[v.first].size()){
			if(v.second < mindist[edge[v.first][i]]){
				mindist[edge[v.first][i]] = v.second;
				que.push(make_pair(edge[v.first][i],v.second));
			}
		}
	}
	REP(i,n){
		cout << i+1 << ' ';
		if(mindist[i]==INF)cout << -1 << endl;
		else	cout << mindist[i] << endl;
	}
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int main() {
    int N;
    cin >> N;
    vector< vector<int> > edges(N);
    REP(i, N) {
        int u, k;
        cin >> u >> k;
        u--;
        edges[u].resize(k);
        REP(j, k) {
            cin >> edges[i][j];
            edges[i][j]--;
        }
    }

    queue<int> que;
    vector<int> dist(N, -1);
    dist[0] = 0;
    que.push(0);
    while(!que.empty()) {
        int v = que.front();
        que.pop();

        for(int next: edges[v]) {
            if(dist[next] == -1) {
                dist[next] = dist[v] + 1;
                que.push(next);
            }
        }
    }

    REP(i, N) cout << i+1 << " " << dist[i] << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int n,M[101][101];
int d[101];

void bfs(int s){
  
  queue<int>q;
  
  q.push(s);
  
  for(int i=0;i<n;i++) d[i]=INF;
  d[s]=0;
  
  while(!q.empty()){
    
    int 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++){   
    cin>>u>>k;
    u--;
    
    for(int 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,n) for(int i=0; i<(n); i++)
using ll = long long;
using P = pair<int,int>;
int n;
vector<int> graph[110];
int visited[110],dist[110];
int main()
{
    cin >>n;
    rep(i,n){
        int u;
        
        cin >> u;
        u--;
        int k;
        cin >> k;
        rep(j,k){
            int v;
            cin >> v;
            v--;
            graph[u].push_back(v);
            
        }

    }
    queue<int> q;
    
    q.push(0);
    visited[0] = 1;
    while(!q.empty()){
        auto now = q.front();q.pop();
        
        for(auto next :graph[now]){
            if(visited[next])continue;
            dist[next] = dist[now] +1;
            q.push(next);
            visited[next] = 1;
        }
    }

    rep(i,n){
        if (i && dist[i] == 0)cout << i+1 << " " << -1 << endl;
        else cout << i+1 << " " <<  dist[i] << endl; 
    }
    

    return 0;
}