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 <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> G[110], d(110);

int main() {
    int n; cin >> n;
    for (int i = 0; i < n; i++) {
        int u, k; cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v; cin >> v;
            G[u-1].push_back(v-1);
        }
    }

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

    queue<int> que;
    que.push(0);
    d[0] = 0;
    while (!que.empty()) {
        int u = que.front(); que.pop();
        for (int v: G[u]) {
            if (d[v] == -1) {
                que.push(v);
                d[v] = d[u] + 1;
            }
        }
    }

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

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define reptr(i,n) for(int (i)=(n);(i)>=0;--(i))
#define All(x) (x).begin(),(x).end()
const ll MOD=1e9+7;
using namespace std;

vector<int> ans;
int state[105][105]={};
int n;
void bfs(int s){
  queue<int> q;
  q.push(s);
  while(!q.empty()){
    int u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      //隣接していない
      if(state[u][v]==false) continue;
      //既に探索されている
      if(ans[v]!=INT_MAX) continue;
      //未探索の場合
      ans[v] = ans[u]+1;
      q.push(v);
    }
  }

  rep(i,n){
    if(ans[i]==INT_MAX) ans[i] = -1;
    cout << i+1 << " " << ans[i] << endl;
  }
}

int main(){
  cin >> n;
  ans.resize(n,INT_MAX);
  ans[0]=0;
  rep(i,n){
    int u,k;
    cin >> u >> k;
    u--;
    rep(j,k){
      int v;
      cin >> v;
      v--;
      state[u][v]=1;
    }
  }
  bfs(0);
}

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

int n;

int V[100][100];
int D[100];
int d = 0;

std::queue<int> Q;

void bfs(int r) {
    Q.push(r);
    D[r] = 0;
    while (Q.size()) {
        r = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++) {
            if ((D[i] == 0) && (V[r][i] == 1)) {
                Q.push(i);
                D[i] = D[r] + 1;
            }
        }
    }
}


int main() {
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        int id, e_num;
        std::cin >> id >> e_num;
        for (int j = 0; j < e_num; j++) {
            int t;
            std::cin >> t;
            V[id - 1][t - 1] = 1;
        }
    }

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

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

using namespace std;
using Graph = vector<vector<int>>;

int main() {

    int n; cin >> n;
    int u, k;

    Graph G(n+1);
    for ( int i = 1; i <= n; ++i ) {
        cin >> u >> k;
        for ( int j = 1; j <= k; ++j ) {
            int a;
            cin >> a;
            G[i].push_back(a);
            //G[b].push_back(a);
        }
    }

    /*for ( int i = 1; i <= n; ++i ) {
        for ( int ax : G[i] ) {
            cout << ax ;
        }
        cout << endl;
    }*/

    vector<int> dist(n+1,-1);
    queue<int> que;

    dist[1] = 0;
    que.push(1);

    while ( !que.empty() ) {
        int v = que.front();
        que.pop();

        for ( int nv : G[v] ) {
            if ( dist[nv] != -1 ) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

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

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using Graph = vector<vector<ll>>;
constexpr ll MOD = 1e9+7;
constexpr ll INF = 1ll<<60;

int main(int argc, char **argv)
{
	ll n; cin >> n;
	Graph G(n);
	for (ll i = 0; i < n; ++i)
	{
		ll a, k; cin >> a >> k;
		--a;
		for (ll j = 0; j < k; ++j)
		{
			ll b;
			cin >> b;
			G[a].push_back(--b);
		}
	}

	vector<ll> d(n, -1);
	d[0] = 0;
	queue<ll> que;
	que.push(0);

	while (!que.empty())
	{
		ll now = que.front(); que.pop();

		for (auto &nv : G[now])
		{
			if (d[nv] != -1) continue;
			d[nv] = d[now] + 1;
			que.push(nv);
		}
	}

	for (ll i = 0; i < n; ++i)
		std::cout << i + 1 << " " << d[i] << std::endl;
}

C++: //
//
//  algoC.cpp - created by us162022
//
#include <iostream>
#include <queue>
using namespace std;
struct Node{
  int u,k,v[99];
  int distance;
};

int main(int argc, char *argv[])
{
  int n;
  Node node[100];
  queue<Node> que;

  cin >> n;

  for(int i=0; i<n; i++){
    cin >> node[i].u >> node[i].k;

    for(int j=0; j<node[i].k; j++)
      cin >> node[i].v[j];

    node[i].distance=-1;
  }

  node[0].distance = 0;
  que.push(node[0]);

  while(!que.empty()){
    Node fro = que.front();

    for(int i=0; i<fro.k; i++){
      int x = fro.v[i]-1;
  
      if(node[x].distance==-1){
	    node[x].distance = fro.distance+1;

        que.push(node[x]);
        }
    }

    que.pop();
  }

  for(int i=0; i<n; i++)
    cout << node[i].u << " " << node[i].distance << endl;

}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
using namespace std;

/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long  ll;

#define fi first
#define se second

using Graph = vector<vector<int>>;


int main() {
    int n;
    cin>>n;
    Graph G(n);
    for(int i=0;i<n;i++){
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++){
            int x;
            cin>>x;
            G[u-1].push_back(x-1);
        }
    }

    queue<int>que;
    vector<int>dist(n, -1);
    dist[0]=0;

    que.push(0);

    while(!que.empty()){
        int v = que.front();
        que.pop();

        for(int nv : G[v]){
            if(dist[nv]!=-1)continue;
            que.push(nv);
            dist[nv]=dist[v]+1;
        }
    }

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



    return 0;
}