Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

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

func (q *Queue) pop() Pair {
	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([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	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<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<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 <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
const ll MOD=1e9+7;

int main() {
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<vector<int>> r(n);
    rep(i,n) {
        int u,k,v;
        cin >> u >> k;
        rep(j,k) {
            cin >> v;
            r[i].push_back(v-1);
        }
    }
    queue<int> q;
    vector<int> cost(n,-1);
    q.push(0);
    cost[0]=0;
    while (!q.empty()) {
        int now=q.front();
        q.pop();
        rep(i,r[now].size()) {
            int next=r[now][i];
            if (cost[next]==-1) {
                cost[next]=cost[now]+1;
                q.push(next);
            }
        }
    }
    rep(i,n) cout << i+1 << ' ' << cost[i] << endl;
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)
#define ll long long

static const int INFTY = (1<<21);

int n, M[100][100];

void BFS(void) {
	queue<int> Q;
	int d[100];
	REP(i, n) d[i] = INFTY;
	d[0] = 0;

	Q.push(0);//?§???????0

	int u;
	while (!Q.empty()) {
		u = Q.front(); Q.pop();
		REP(i, n) {
			if (M[u][i]) {
				if (d[i] == INFTY) {
					d[i] = d[u] + 1;
					Q.push(i);
				}
			}
		}
	}

	REP(i, n) {
		cout << i + 1 << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
	}
}

int main() {
	cin >> n;

	REP(i, n) {
		REP(j, n) M[i][j] = 0;
	}

	int u, k, v;
	REP(i, n) {
		cin >> u >> k;
		REP(j, k) {
			cin >> v;
			M[u - 1][v - 1] = 1;//0-origin
		}
	}

	BFS();

	return 0;
}

C++: #include <iostream>
#include <queue> 
using namespace std;
int V[100][100];
int d[100];
int t[100];
int n;
queue<int> qu;
void bfs()
{
	t[0] = 1;
	d[0] = 0;
	qu.push(0);
	while (!qu.empty())
	{
		int u = qu.front(); qu.pop();
		for (int i = 0; i < n; i++)
		{
			if (V[u][i] == 1 && t[i] == 0)
			{
				t[i] = 1;
				d[i] = d[u] + 1;
				qu.push(i);
			}
		}
		t[u] = 2;
	}
	return;
}

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

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

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

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define INF 1000000000

class Node{//普通の木用のデータ
//ノードはポインタでもっておこう
public:
  ///ノードの番号
  int num;
  //そのノードの親
  //Node* p;(親の情報が必要ない)
  //そのノードの子(ここ、vectorで管理してる場合はintの方が扱いやすい)
  vector<int> v;
  //探索済みかどうか
  bool visit=false;
};

void breadth_search(queue<int> vs,vector<Node>& vn,vector<int>& vd){
  int d=0;//vsにすでに格納しているものは距離が決定している前提
  int l=vs.size();
  while(l!=0){
    d++;
    l=vs.size();
    for(int i=0;i<l;i++){
      int x=vs.front();
      int l_sub=vn[x].v.size();
      for(int j=0;j<l_sub;j++){
        if(vn[vn[x].v[j]].visit==false){
          vs.push(vn[x].v[j]);
          vn[vn[x].v[j]].visit=true;
          vd[vn[x].v[j]]=d;
        }
      }
      vs.pop();
    }
  }
}


int main(){
  int n;cin >> n;
  vector<Node> vn(n);for(int i=0;i<n;i++){vn[i].num=i;}
  vector<int> vd(n,-1);//深さを保存(辿れないと-1)
  queue<int> vs;//今見ているNodeのnumを入れておく

  for(int i=0;i<n;i++){
    int k;cin >> k >> k;
    for(int j=0;j<k;j++){
      int l;cin >>l;l-=1;
      //vn[l].p=&vn[i];
      vn[i].v.push_back(l);
    }
  }

  vn[0].visit=true;
  vd[0]=0;
  vs.push(0);
  breadth_search(vs,vn,vd);
  for(int i=0;i<n;i++){
    cout << i+1 << " " << vd[i] << endl;
  }



}

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

struct Node{
	int s;
	queue<int> Q;
};

int main(){
	int n;
	cin >> n;
	Node node[n + 1];
	for(int i = 1; i <= n; i++){
		int u, k, v;
		cin >> u >> k;
		node[u].s = -1;
		while(k--){
			cin >> v;
			node[u].Q.push(v);
		}
	}

	int p = 1, next, m;
	queue<int> move;
	node[p].s = 0;
	move.push(p);

	while(!move.empty()){
		p = move.front();
		//cout << "Pivot is " << p << endl;
		if(node[p].Q.empty()){
			//cout << "Nowhere to go from " << p << endl;
			move.pop();
		} else {
			m = node[p].Q.size();
			while(m--){
				next = node[p].Q.front();
				node[p].Q.pop();
				//cout << "Checking " << next << endl;
				if(node[next].s < 0){
					//cout << "Can go to " << next << endl;
					node[next].s = node[p].s + 1;
					move.push(next);
				}
			}
			move.pop();
		}
	}

	for(int i = 1; i <= n; i++)
		cout << i << " " << node[i].s << endl;
}