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 <iostream>
#include <climits>
#include <vector>
#include <queue>
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF INT_MAX

using namespace std;

void bfs(int s, int n, int *color, int *d, vector<vector<int>> Adj){
    queue<int> Q;
    for (int u = s+1; u <= n; u++){
        color[u] = WHITE;
        d[u] = INF;
    }
    color[s] = GRAY;
    d[s] = 0;
    Q.push(s);
    while (Q.size() != 0){
        int zero = 0;
        int u = Q.front();
        Q.pop();
        for (int w = 1; w < Adj[u].size(); w++){
            int v = Adj[u][w];
            if (v == 0){
                break;
            }else if (color[v] == WHITE){
                color[v] = GRAY;
                d[v] = d[u] + 1;
                Q.push(v);
                zero++;
            }
        }
        color[u] = BLACK;
    }
}

int main(int argc, char* argv[]) {
    int n, u, k, v;
    cin >> n;
    n++;
    int color[n], d[n], f[n];
    vector<vector<int>> Adj(n, vector<int>(n, 0));

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

    bfs(1, n, color, d, Adj);

    for (int i = 1; i < n; i++){
        if (d[i] == INF)
            printf("%d -1\n", i);
        else
            printf("%d %d\n", i, d[i]);
    }
    return 0;
}

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

map<int, vector<int>> A;
int color[101];
int D[101];
int d = 0;

void dfs(int u) {
    queue<int> Q;
    Q.push(u);
    color[u] = 1;
    int current;
    D[u] = d;
    while(!Q.empty()) {
        current = Q.front();
        Q.pop();
        vector<int>::iterator it = A[current].begin();
        while(it != A[current].end()) {
            if (color[*it] == 0) {
                Q.push(*it);
                color[*it] = 1;
                D[*it] = D[current] + 1;
            }
            it++;
        }
    }
}

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

    int u, k;
    for (int i = 1; i <= n; i++) {
        color[i] = 0;
        D[i] = -1;
        cin >> u >> k;
        int ad;
        for (int j = 0; j < k; j++) {
            cin >> ad;
            A[u].push_back(ad);
        }
    }

    dfs(1);

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

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <utility>

using vvi = std::vector<std::vector<int>>;

std::vector<int> DistanseFrom(const vvi& edges, int from)
{
    using namespace std;
    constexpr int nil = -1;

    int nextDist {};
    const int n = edges.size();
    queue<int> currentQ;
    queue<int> nextQ;
    vector<int> distances(n, nil);

    nextQ.push(from);
    distances[from] = nextDist;
    ++nextDist;

    while (!nextQ.empty())
    {
        currentQ = nextQ;
        nextQ = queue<int>();
        
        while (!currentQ.empty())
        {
            int c = currentQ.back();
            currentQ.pop();

            for (const auto& e : edges[c])
            {
                if (distances[e] == nil)
                {
                    nextQ.push(e);
                    distances[e] = nextDist;
                }
            }
        }
        
        ++nextDist;
    }

    return distances;
}

void solve()
{
    using namespace std;

    int n;
    cin >> n;

    //隣接リスト形式(1オリジン)
    vvi edges(n + 1);

    //読み込み
    for (int i = 1; i <= n; ++i)
    {
        int u, k;
        cin >> u >> k;

        edges[u] = vector<int>(k);

        for (int j = 0; j < k; ++j)
        {
            cin >> edges[u][j];
        }
    }

    auto dist = DistanseFrom(edges, 1);

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

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

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

int main()
{
    int n;
    cin >> n;
    int adj[n][n];
    int V[n];
    for (int i = 0; i < n; i++)
    {
        int u, k;
        cin >> u >> k;
        V[i] = k;
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            adj[u - 1][j] = v - 1;
        }
    }
    queue<int> q;
    int d[n];
    for (int i = 0; i < n; i++)
        d[i] = -1;
    d[0] = 0;
    q.push(0);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < V[u]; i++)
        {
            int v = adj[u][i];
            if (d[v] == -1)
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }

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

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

class Vertex {
public:
    int num;
    bool flag;                  // 以前訪れたかどうか
    vector<int> adj;            // 隣接リスト
};

int dist[101];
void bfs(map<int, Vertex>& graph, Vertex s) {
    queue<Vertex> que;

    dist[s.num] = 0;
    graph[s.num].flag = true;
    que.push(s);
    while (!que.empty()) {
        Vertex u = que.front(); que.pop();
        for (auto i = 0; i < u.adj.size(); i++) {
            Vertex v = graph[u.adj[i]];
            if (!v.flag) {
                graph[u.adj[i]].flag = true;
                dist[v.num] = dist[u.num]+1;
                que.push(v);
            }
        }
    }
}

int main(void) {
    int n;
    cin >> n;
    for (int i = 0; i < 101; i++) {
        dist[i] = -1;
    }

    map<int, Vertex> graph; // 頂点の番号->頂点
    for (int i = 0; i < n; i++) {
        Vertex v;
        int num, k;
        cin >> num >> k;
        v.flag = false;
        v.num = num;
        for (int j = 0; j < k; j++) {
            int tmp;
            cin >> tmp;
            v.adj.push_back(tmp);
        }
        graph[num] = v;
    }

    bfs(graph, graph[1]);
    for (int i = 1; i <= n; i++) {
        cout << i << " " << dist[i] << endl;
    }

    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[106][106];
int A[106],f[106],w,h,n,sx,sy,gx,gy,e,g[106];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=1;i<=100;i++){
		for(j=1;j<=100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<105;i++){		
		A[i]=0;				
		f[i]=-1;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
	//		printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	f[1]=0;
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, g[i]);	//
	}
	return 0;
}