Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repr(i, a, b) for (int i=a; i<(b); ++i)
#define reprev(i, n) for (int i=n-1; i>=0; --i)
#define reprrev(i, a, b) for (int i=b-1; i>=(a); --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    cout << fixed << setprecision(10);
    
    int n; cin >> n;
    vector<vector<int>> e(n);
    rep(i, n) {
        int id; cin >> id; id--;
        int ne; cin >> ne;
        rep(j, ne) {
            int tn; cin >> tn; tn--;
            e[id].emplace_back(tn);
        }
    }

    queue<int> q;
    vector<int> aa(n);
    vector<int> d(n);
    q.emplace(0);
    aa[0] = 1;

    while(!q.empty()) {
        int nn = q.front();
        q.pop();
        rep(i, e[nn].size()) {
            if (aa[e[nn][i]]) continue;
            q.emplace(e[nn][i]);
            d[e[nn][i]] = d[nn] + 1;
            aa[e[nn][i]] = 1;
        }
    }
    rep(i, n) {
        if (aa[i]) {
            cout << i+1 << " " << d[i] << endl;
        } else {
            cout << i+1 << " " << -1 << endl;
        }
        
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (1E9+7)
#define PI 3.1415926535897932384626433832795

std::ostream &operator<<(std::ostream &out, const vector<int> &tgt)
{
	string s;
	for (int i = 0; i<tgt.size(); i++) {
		s += (to_string(tgt[i]) + ((i != tgt.size() - 1) ? " " : ""));
	}
	out << s;
	return out;
}
struct node {
	vi to;
	int dist = -1;
	bool visited = false;
};

vector<node> g;
int t = 0;

void bfs(int n) {
	queue<pii> q;
	q.push({n,0});
	while (!q.empty()) {
		int dis = q.front().sc;
		int node = q.front().fs;
		q.pop();
		if (g[node].visited)continue;
		//cout << node + 1 << endl;
		g[node].dist = dis;
		g[node].visited = true;
		rep(i, g[node].to.size()) {
			if (!g[g[node].to[i]].visited)q.push({ g[node].to[i],dis + 1 });
		}
	}
}

int main(){
	int n,in,inn;
	cin >> n;
	g.resize(n);
	rep(i, n) {
		cin >> in;
		cin >> in;
		rep(j, in) {
			cin >> inn;
			g[i].to.push_back(inn-1);
		}
	}
	rep(i, n) {
		if (!g[i].visited) {
			bfs(0);
		}
	}
	rep(i, n) {
		cout <<i+1<<" "<< g[i].dist <<endl;
	}
}

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

struct node
{
	int distance;
	bool accessed;
	node(int d, bool a) : distance(d), accessed(a) {}
};

void bfs(vector<node> &nodes, vector<vector<bool>> &conn_mat)
{ //index = n-1
	nodes[0].accessed = true;
	nodes[0].distance = 0;
	queue<int> bfs_queue;
	bfs_queue.push(0);
	int dis = 0;
	while (!bfs_queue.empty())
	{
		int index = bfs_queue.front();
		int i = 0;
		bfs_queue.pop();
		for (auto connected : conn_mat[index])
		{
			if (connected && (!nodes[i].accessed))
			{
				bfs_queue.push(i);
				nodes[i].accessed = true;
				nodes[i].distance = nodes[index].distance + 1;
			}
			i++;
		}
	}
}

int main()
{

	int n_node, index, n_cn, cn;
	cin >> n_node;
	vector<node> nodes(n_node, node(-1, false));
	vector<bool> vertex(n_node, false);
	vector<vector<bool>> conn_mat(n_node, vertex);
	while (n_node--)
	{
		cin >> index >> n_cn;
		while (n_cn--)
		{
			cin >> cn;
			conn_mat[index - 1][cn - 1] = true;
		}
	}
	bfs(nodes, conn_mat);
	int i = 0;
	for (node nd : nodes)
	{
		i++;
		cout << i << ' ' << nd.distance << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
bool a[100][100];
int D[100];
void  bfs ( int src) {
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while( ! Q.empty() ) {
		int cur = Q.front();
		Q.pop();
		for(int dst = 0; dst < n; dst++ ) {
			if( D[dst] == 0 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cerr << i+1 <<  " " << D[i] <<  endl;
	}
}
int main() {
	cin >> n;
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			a[i][j] = 0;
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
		bfs(0);
}

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

int n;
vector<int> G[110];
int d[110];

void bfs(){
  queue<int> que;
  for(int i=0; i<n; i++){
    d[i] = -1;
  }
  d[0] = 0;
  que.push(0);
  while(!que.empty()){
    int v = que.front(); que.pop();
    for(int i=0; i<G[v].size(); i++){
      int nv = G[v][i];
      if(d[nv] < 0){
        que.push(nv);
        d[nv] = d[v] + 1;
      }
    }
  }
}

int main(){
  cin >> n;
  for(int i=0; i<n; i++){
    int u, k; cin >> u >> k;
    for(int i=0; i<k; i++){
      int c; cin >> c;
      G[u-1].push_back(c-1);
    }
  }
  bfs();
  for(int i=0; i<n; i++){
    cout << i+1 << " " << d[i] << endl;
  }
}

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

int n;
char c[101];
int g[101][101];
int t = 0;
int visit[101];
int finish[101];
int d[101];
int dist = 0;

void bfs(int start) {
    t++;
    queue<int> q;
    q.push(start);
    c[start] = 'g';
    visit[start] = t;
    while(!q.empty()) {
        t ++;
        int now = q.front();
        dist ++;
        for (int i = 1; i <= n ; ++i) {
            if(g[now][i] && c[i] == 'w') {
                q.push(i);
                c[i] = 'g';
                visit[i] = t;
                d[i] = d[now] + 1;
            }
        }
        q.pop();
        c[now] = 'b';
        finish[now] = t;
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n ; ++i) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; ++j) {
            int v;
            cin >> v;
            g[u][v] = 1;
        }
    }

    for (int l = 0; l < n; ++l) {
        c[l+1] = 'w';
    }

    for (int i1 = 1; i1 <=n ; ++i1) {
        d[i1] = -1;
    }
    for (int m = 1; m <= n ; ++m) {
        d[1] = 0;
        if(c[m] == 'w') bfs(m);
        break;

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

    return 0;
}