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++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

int main(){
    //input
    int n;
    std::cin>>n;

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

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

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<std::endl;
    }
    return 0;
}

C++: #include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

#define NMAX 100
#define INF -1
#define NIL -1

int n;
int d[NMAX + 1]; // distance, 1-origin
vector<int> adj[NMAX + 1];
queue<int> Q;

int next_unvisited_adj(int u) {
    vector<int>::iterator it;
    for (it = adj[u].begin(); it != adj[u].end(); it++) {
        if (d[*it] == INF)
            return *it;
    }
    return NIL;
}

void bfs(int x) {
    d[x] = 0;
    Q.push(x);
    int u, v;
    while (!Q.empty()) {
        u = Q.front();
        Q.pop();
        while ((v = next_unvisited_adj(u)) != NIL) {
            d[v] = d[u] + 1;
            Q.push(v);
        }
    }
}

int main() {
    scanf("%d", &n);
    int u, k, v;
    for (int i = 1; i <= n; i++) {
        d[i] = INF;
        scanf("%d %d", &u, &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &v);
            adj[u].push_back(v);
        }
    }
    bfs(1);
    for (int i = 1; i <= n; i++)
        printf("%d %d\n", i, d[i]);
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX 105
#define INFTY -1

int n, G[MAX][MAX], d[MAX];

void bfs(int s) {
	queue<int> Q;
	Q.push(s);
	d[s] = 0;
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for (int v = 1; v <= n; v++) {
			if (G[u][v] == 0) continue;
			if (d[v] != INFTY) continue;
			d[v] = d[u] + 1;
			Q.push(v);
		}
	}
}

int main(void) {
	int i, j, u, k, v;
	for (i = 0; i < MAX; i++) {
		d[i] = INFTY;
	}
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> u >> k;
		for (j = 0; j < k; j++) {
			cin >> v;
			G[u][v] = 1;
		}
	}

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

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

#define MAX 100
static const int WHITE = 0; // not visit
static const int GRAY = 1; // visited
static const int BLACK = 2; // searched

int n;
int M[MAX][MAX] = { 0 };
int distance[MAX] = { 0 }; // ?????????????§??????????????????¢


void breadthFirstSearch() {
	queue<int> vertices;
	int v_states[MAX] = { WHITE };

	vertices.push(0); // ?§????
	while (!vertices.empty()) {
		int u = vertices.front();
		v_states[u] = GRAY;
		vertices.pop();
		for (int v = 0; v < n; v++) {
			if (M[u][v] == 1 && v_states[v] == WHITE) {
				::distance[v] = ::distance[u] + 1;
				vertices.push(v);
				v_states[v] = GRAY;
			}
		}
	}
	return;
}

int main() {
	cin >> n;

	for (int i = 0; i < n; i++) {
		int k, u;
		cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	breadthFirstSearch();
	for (int i = 0; i < n; i++) {
		cout << i + 1 << ' ' << ::distance[i] << endl;
	}

	return 0;

}

C++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
#define all(x) (x).begin(),(x).end()


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, e;
    cin >> n;
    vector<vector<int>> vec(n+1, vector<int>());
    for(int i = 0; i < n; i++){
      int u, k;
      cin >> u >> k;
      for(int j = 0; j < k; j++){
          cin >> e;
          vec[u].push_back(e);
      }
    }

    vector<int> cost(n+1, -1);
    queue<int> que;
    que.push(1);
    cost[1] = 0;
    int num;
    while(!que.empty()){
      num = que.front(); que.pop();
      for(int i=0; i<vec[num].size(); i++){
          int next = vec[num][i];
        if(cost[next] == -1){
          cost[next] = cost[num] + 1;
          que.push(next);
        }
      }
    }
    for(int i=1; i<=n; i++){
      cout << i << " " <<  cost[i] << ln;
    }
}

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[u] != 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;
}