Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

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

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;
#define N 100
#define INF 1e9

queue<int> Q;
int n,M[N][N];
int dist[N];

void bsf(int l){
	Q.push(l);
	for(int i = 0;i < n;i++) dist[i] = INF;

	dist[l]=0;
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		for(int i=0;i<n;i++){
			if(M[u][i] == 0)continue;
			if(dist[i] != INF)continue;
			dist[i] = dist[u] + 1;
			Q.push(i);
		}
	}
}

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;
		}
	}
	bsf(0);
	return 0;
}

C++: #include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>

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<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);

}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;

void solve(){
  int n; cin >> n;
  vector<vector<int> > v(n + 1, vector<int>(n + 1, 0));
  for(int i = 0; i < n; i++){
    int u, k; cin >> u >> k;
    for(int j = 0; j < k; j++){
      int x; cin >> x;
      v[u][x] = 1;
    }
  }
  queue<int> q;
  vector<int> ans(n + 1, -1), c(n + 1, 0);
  q.push(1);
  ans[1] = 0;
  c[1] = 1;
  while(q.size() != 0){
    int x = q.front();
    q.pop();
    for(int i = 1; i <= n; i++){
      if(v[x][i] == 1 && c[i] == 0){
        q.push(i);
        ans[i] = ans[x] + 1;
        c[i] = 1;
      }
    }
  }
  for(int i = 1; i <= n; i++){
    cout << i << " " << ans[i] << endl;
  }
  return;
}

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

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;

class Queue{
private:
  int inf[MAX];
  int head,tail;
 
public:
 
  Queue(){
    syokika();
  }
 
  void syokika(){
    head = 0;
    tail = 0;
  }
 
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
 
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
 
  int front(){
    return inf[head];
  }
 
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
 
  int size(){
    return tail - head;
  }
 
};

int imap[200][200];
int k[200] = {0}; 
int ans[200] = {0};
bool used[200];

int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }

   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j; 
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }

  Queue que = Queue();

  que.push(u[0]);

  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
	que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
	used[imap[p][i]] = true;
	ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;
 
  for(int i=0;i<n;i++){
    cout << u[i]-1 << " " << ans[u[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;    
    }
}

C++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

#define MAX 100
//#define MAX_ 1000
struct Graph
{
	int id, k;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
//int cou = 0;
void Breadth_cou(Graph *g,int cou)
{
	for (auto i = g->node.begin(); i != g->node.end(); ++i) {
		if (G[*i].d == -1 || G[*i].d > cou) {
			if (G[*i].d > cou)
				Breadth_cou(&G[*i], G[*i].d + 1);
			G[*i].d = cou;
			
		}
	}
	g->k = 0;
}
void Breadth(Graph *g)
{
	if (g->k > 0) {
		Breadth_cou(g, g->d + 1);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			Breadth(&G[*i]);
		}
	}
}
int main(void)
{
	int u, xk, v, n;
	scanf_s("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf_s("%d %d", &u, &xk);
		G[u - 1].id = u - 1;
		G[u - 1].k = xk;
		for (int j = 0; j < xk; ++j) {
			scanf_s("%d", &v);
			G[u - 1].node.push_back(v - 1);
		}
	}
	G[0].d = 0;
	Breadth(&G[0]);
	for (int i = 0; i < n; ++i) {
		printf("%d %d\n", G[i].id + 1, G[i].d);
	}
}