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>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 100;
struct Graph{
    struct Edge{
        int to, w;
        Edge(int v, int _w):to(v), w(_w){}
    };
    vector<Edge> edge;
    vector<vector<int> > vertex;
    int V, E;
    Graph(int _V):V(_V),E(0){
        edge.clear(); vertex.clear();
        vertex.resize(V);
    }
    void addEdge(int u, int v, int w){
        vertex[u].push_back(E++);
        edge.push_back(Edge(v, w));
    }
};
void SPFA(Graph& G, LL* d, int s){
    bool inq[MAXN]; int cnt[MAXN];
    memset(cnt, 0, sizeof(int)*G.V);
    memset(d, INF, sizeof(LL)*G.V);
    memset(inq, false, sizeof(bool)*G.V);
    d[s] = 0; inq[s] = true;
    queue<int> q; q.push(s);
    while (q.size()){
        int p = q.front(); q.pop(); inq[p] = false;
        cnt[p]++; if (cnt[p] > G.V){ d[s] = -1; break;}
        for (auto index : G.vertex[p]){
            auto &e = G.edge[index];
            if (d[p] + e.w < d[e.to]){
                d[e.to] = d[p] + e.w;
                if (!inq[e.to]) inq[e.to] = true, q.push(e.to);
            }
        }
    }
}
int main(){
    int n;
    cin >> n;
    Graph G(n);
    LL d[MAXN];
    for (int i = 0 ; i < n ; i++){
        int m;
        cin >> m >> m;
        while (m--){
            int v;
            cin >> v;
            G.addEdge(i, v-1, 1);
        }
    }
    SPFA(G, d, 0);
    for (int i = 0 ; i < n ; i++)
        cout << i+1 << ' ' << d[i] << '\n';
}

C++: #include<bits/stdc++.h>

using namespace std;
int V;
vector<int> G[102];
bool vis[102];
int d[102];
const int inf=0x3f3f3f;

void bfs(int s=1){
    d[s]=0;
    queue<int> que;
    que.push(s);
    while(!que.empty()){
        int t=que.front();
        que.pop();
        vis[t]=true;
        for(int i=0;i<G[t].size();i++){
            if(!vis[G[t][i]]){
                que.push(G[t][i]);
                d[G[t][i]]=min(d[G[t][i]],d[t]+1);
            }
        }
    }
}


int main(){
    cin>>V;
	int i;
	for(int i=1;i<=V;i++){
        d[i]=inf;
        vis[i]=false;
	}
    for(i=1;i<=V;i++){
        int id,k;
        cin>>id>>k;
        while(k--){
            int out;
            cin>>out;
            G[id].push_back(out);
        }
        sort(G[id].begin(),G[id].end());
    }
    bfs();
    for(int i=1;i<=V;i++){
        cout<<i<<" "<<(d[i]==inf?-1:d[i])<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

int main() {
  ll n;
  cin >> n;
  vector<set<ll>> v(n);
  for(ll i=0;i<n;i++) {
    ll f,k;
    cin >> f >> k;
    for(ll j=0;j<k;j++) {
      ll temp;
      cin >> temp;
      temp--;
      v[i].insert(temp);
    }
  }
  vector<ll> ans(n,-1);
  ans[0]=0;
  queue<ll> q;
  q.push(0);
  while(q.size()) {
    ll p=q.front();
    q.pop();
    for(ll t:v[p]) {
      if(ans[t]<0) {
        ans[t]=ans[p]+1;
        q.push(t);
      }
    }
  }
  for(ll i=0;i<n;i++) {
    cout << i+1 << " " << ans[i] << endl;
  }
}

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

using namespace std;

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

    vector<vector<int>> graph(n);

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

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

    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(int i = 0; i < graph[cur].size(); i++ ){
            int next = graph[cur][i];
            if(d[next] == -1){
                d[next] = d[cur]+1;
                q.push(next);
            }
        }
    }

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

    return 0;
}

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

using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int INF = 1 << 30;

int n, M[N][N];
int color[N], d[N];


void dfs(int s){
    queue<int> q;
    q.push(s);
    for (int i = 0; i < n; ++i){
        d[i] = INF;
    }
    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] != INF)
                continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (int i = 0; i < n; ++i){
        cout << i + 1 << " " << ((d[i] == INF) ? -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;
        for (int j = 0; j < k; ++j){
            cin >> v;
            M[u - 1][v - 1] = 1;
        }
    }

    dfs(0);

    return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 100000

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]==1){
        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;
}