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;
const int N = 101;
const int CRL = -1;
int adj[N][N] = {0};
int d[N]={0};
int kai[N];

int main()
{
    int n;
    queue <int> q;
    q.push(1);
    cin >> n;
    for(int i=1;i<=n;i++){
    	int k;
    	int l;
    	cin >> l >> k;
    	kai[i]=l;
    	for(int j=1;j<k+1;j++){
    		int m;
    		cin >> m;
    		adj[i][j] = m;
    	}
    }
    while(q.size()){
    	int k = q.front();
    	q.pop();
    	for(int i=1;i<kai[i]+1;i++){
    		if(d[adj[k][i]]==0){
    			d[adj[k][i]] = d[k]+1;
    			q.push(adj[k][i]);
    		}
    	}
    }
    for(int i=1;i<=n;i++){
    	cout << i << " " << d[i] << endl;
    }
    return 0;
}

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,v,k;
    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>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;


void bfs(v2& a, v2& b){
	queue<int> q;
	q.push(a[0][0]);
	while(!q.empty()){
		int x = q.front()-1;
		int t = b[x][1];
		q.pop();
		rep(i, a[x][1]){
			int y = a[x][2+i]-1;
			if(a[y][0] != -1 && a[y][0] != -2){
				b[y][1] = t+1;
				a[y][0] = -1;
			}
			if(a[y][0] != -2) q.push(y+1);
		}
		a[x][0] = -2;
	}
}

int main(){
	int n;
	cin >> n;
	v2 a(n, v1(0));
	v2 b(n, v1(2, 0));
	rep(i, n){
		int m;
		cin >>m;
		a[i].push_back(m);
		b[i][0] = m;
		int x;
		cin >> x;
		a.at(i).push_back(x);
		rep(j, x){
			int y;
			cin >> y;
			a.at(i).push_back(y);
		}
	}
	bfs(a, b);
	for(int i = 1;i<n; i++){
		if(b[i][1] == 0) b[i][1] = -1;
	}
	rep(i, n){
		rep(j, 2){
			cout << b[i][j];
			if(j != 1) cout << " ";
			else cout << endl;
		}
	}
}

C++: #include<stdio.h>
#include<queue>
#define MAX 101
using namespace std;

int G[MAX][MAX], d[MAX];
queue<int> Q;

void adjacency_matrices(int n) {
    int i, j, u, m, k;

    for (i=1; i<=n; i++) {
        scanf("%d", &u);
        scanf("%d", &m);
        for (j=1; j<=m; j++) {
            scanf("%d", &k);
            G[u][k] = 1;
        }
    }
}

void breadth_first_search(int n, int i, int dist) {
    int k;
    d[i] = dist++;
    for (int j=1; j<=n; j++) {
        if (G[i][j]==1 && d[j]==-1) {
            Q.push(j);
            d[j] = dist;
        }
    }

    while(!Q.empty()) {
        dist++;
        int length = Q.size();
        for (int j=1; j<=length; j++) {
            k = Q.front(); Q.pop();
            for (int m=1; m<=n; m++) {
                if (G[k][m]==1 && d[m]==-1) {
                    Q.push(m);
                    d[m] = dist;
                }
            }
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);

    for (int i=1; i<=n; i++) {
        d[i]=-1;
        for (int j=1; j<=n; j++) {
            G[i][j] = 0;
        }
    }

    adjacency_matrices(n);

    breadth_first_search(n, 1, 0);

    for (int i=1; i<=n; i++) {
        printf("%d %d\n", i, d[i]);
    }
}

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

using Graph = vector<int>;
vector<Graph>g;

int main() {
    int n;
    cin >> n;
    g.resize(n);
    vector<int>s(n, -1);
    for (int i=0; i < n; i++) {
        int num, count;
        cin >> num >> count;
        for (int j=0; j < count; j++) {
            int t;
            cin >> t;
            g[num-1].emplace_back(t-1);
        }
    }

    queue<int> que;
    que.push(0);
    s[0] = 0;
    while (!que.empty()) {
        int tyouten = que.front();
        que.pop();
        Graph gu = g[tyouten];
        for (int i=0; i < gu.size(); i++) {
            int num = gu[i];
            if (s[num] == -1) {
                s[num] = s[tyouten] + 1;
                que.push(num);
            }
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <numeric>
#include <queue>

using namespace std;

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

    vector<vector<int32_t>> A(n+1);

    for (int32_t i = 0; i < n; i++) {
        int32_t u, k;
        cin >> u >> k;

        for (int32_t j = 0; j < k; j++) {
            int32_t v;
            cin >> v;

            A[u].push_back(v);
        }
    }

    vector<bool> visited(n+1);
    fill(visited.begin(), visited.end(), false);
    vector<int32_t> d(n+1);
    fill(d.begin(), d.end(), -1);
    queue<int32_t> tovisit;
    visited[1] = true;
    d[1] = 0;
    tovisit.push(1);

    while (!tovisit.empty()) {
        int32_t u = tovisit.front();
        tovisit.pop();

        for (int32_t v : A[u]) {
            if (!visited[v]) {
                visited[v] = true;
                d[v] = d[u] + 1;
                tovisit.push(v);
            }
        }
    }

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

    return 0;
}