Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>
#define rep(i, to) for (int i = 0; i < (to); i++)
#define rrep(i, to) for (int i = (to) - 1; i >= 0; i--)
#define repf(i, from, to) for (int i = (from); i < (to); i++)
#define all(v) v.begin(), v.end()
#define unless(cond) if (!(cond))
using namespace std;
using ll = long long;
template <typename T>
using V = vector<T>;
template <typename T, typename U>
using P = pair<T, U>;

V<int> dist;
V<V<int>> graph;

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

  graph.resize(n);
  dist.resize(n, -1);

  rep(i, n) {
    int u, k;
    cin >> u >> k;
    u--;

    rep(j, k) {
      int adj;
      cin >> adj;
      adj--;
      graph[u].push_back(adj);
    }

    dist[i] = -1;
  }


  queue<int> will;
  dist[0] = 0;
  will.push(0);

  while(!will.empty()) {
    int current_i = will.front();
    will.pop();

    for(auto next_i : graph[current_i]) {
      if(dist[next_i] != -1) {
        continue;
      }
      dist[next_i] = dist[current_i] + 1;
      will.push(next_i);
    }
  }


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

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

int main()
{
    int n;
    cin >> n;
    int Adj[n][n] = {};
    int d[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;
        }
        d[i] = -1;
    }

    int src = 0;
    queue<int> q;
    d[src] = 0;
    q.push(src);
    while (!q.empty())
    {
        int cur = q.front();
        q.pop();
        for (int dst = 0; dst < n; dst++)
        {
            if (Adj[cur][dst] && d[dst] == -1)
            {
                d[dst] = d[cur] + 1;
                q.push(dst);
            }
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <iomanip>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;

vector<int> to[101];
vector<int> d(101,-1);

int main(){
    int n;
    cin >> 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;
            to[u].push_back(v);
        }
    }

    queue<pair<int, int> > q;
    q.push(make_pair(1,0));

    while(q.size() > 0){
        pair<int, int> p = q.front();
        q.pop();
        int i = p.first;
        int t = p.second;

        if(d[i] != -1) continue;
        d[i] = t;
        for(int j = 0; j < to[i].size(); j++){
            q.push(make_pair(to[i][j], t+1));
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,m,k,a,b,ch[100];
vector<int>v[100];
queue<int>que;
int main(void){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>m>>k;
        for(int i=0;i<k;i++){
            cin>>a;
            v[m-1].push_back(a-1);
        }
    }
    que.push(0);
    ch[0]=1;
    while(que.size()){
        int q=que.front();
        que.pop();
        for(auto x:v[q]){
            if(ch[x]==0){
                ch[x]=ch[q]+1;
                que.push(x);
            }
        }
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<ch[i]-1<<endl;
    }
}

C++: #include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = (int)1e2 + 10;

int n;
vector<int> g[MAXN];
int vis[MAXN];
int dep[MAXN];

void BFS(){
    int d=0;
    queue<int> q;
    for(int i=1;i<=n;i++) dep[i]=-1;
    dep[1]=0;
    q.push(1);
    vis[1]=1;
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i];
            if(vis[v]) continue;
            dep[v]=dep[u]+1;
            vis[v]=1;
            q.push(v);
        }
    }
}

void work(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int u,k; scanf("%d%d",&u,&k);
        while(k--){
            int v; scanf("%d",&v);
            g[u].push_back(v);
        }
        sort(g[u].begin(),g[u].end());
    }
    BFS();
    for(int i=1;i<=n;i++) printf("%d %d\n",i,dep[i]);
}

int main(){
    work();
    return 0;
}

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

int main(void)
{
     //??£??\??????????????£??\???????????????
    int n, k;

    cin >> n;
    vector<vector<bool> > A(n, vector<bool>(n, false));
    for(int i = 0; i < n; i++){
        cin >> k >> k;
        for(int j = 0; j < k; j++){
            int input;
            cin >> input;
            A[i][input-1] = true;
        }
    }

    vector<int> u(n, 1000000);
    u[0] = 0;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(u[j] == i){
                for(int k = 0; k < n; k++){
                    if(A[j][k] == 1) u[k] = min(i+1, u[k]);
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout << i << " " << u[i] << endl;
    }
    return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
int a[101][101],num[101],dis[101];
bool vis[101];
int n,u,k,x,t=1;
queue<int> q;
int main(){
	memset(a,0,sizeof(a));
	memset(vis,false,sizeof(vis));
	memset(dis,0x1f1f1f1f,sizeof(dis));
	cin>>n;
	for (int i=0; i<n; i++){
		cin>>u>>k;
		num[u]=k;
		int j=0;
		while (k--){
			  cin>>x;
			  a[u][++j]=x;
		}
	}
	q.push(1);
	dis[1]=0;
	while (!q.empty()){
		  x=q.front(); q.pop(); vis[x]=true;
		  for (int i=1; i<=num[x]; i++){
		  	  if (!vis[a[x][i]]){
		  	     dis[a[x][i]]=min(dis[a[x][i]],dis[x]+1);
		  	     q.push(a[x][i]);
              }
		  }
	}
    for (int i=1; i<=n; i++){
    	cout<<i<<' ';
    	if (dis[i]==0x1f1f1f1f) dis[i]=-1;
		cout<<dis[i]<<endl;
	}
	return 0;
}