Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include <cstdio>
#include <queue>
using namespace std;
#define INF (1<<21)

int n, G[100][100], dist[100], visited[100];


int main(void) {
    int u, k, v, i, j;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++) G[i][j] = 0;

    for (i = 0; i < n; i++) {
        scanf("%d %d", &u, &k);
        u--;
        for (j = 0; j < k; j++) {
            scanf("%d", &v);
            v--;
            G[u][v] = 1;
        }
    }
    
    for (i = 1; i < n; i++) {
        dist[i] = INF;
        visited[i] = 0;
    }
    dist[0] = 0;
    visited[0] = 1;

    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        u = q.front();
        q.pop();
        for (i = 0; i < n; i++) {
            if (G[u][i] == 0 || visited[i] == 1) continue;
            q.push(i);
            visited[i] = 1;
            dist[i] = dist[u] + 1;
        }
    }

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

    return 0;
}

C++: //Breadth First Search
#include <iostream>
#include <queue>

using namespace std;

static const int N = 100;

int d[N];

void bfs(vector<vector<int>> &graph, int u) {
    queue<int> q;
    q.push(u);
    d[u] = 0;

    while(!q.empty()) {
        int v = q.front();
        q.pop();
        for (auto w: graph[v]) {
            if(d[w] != -1) continue;
            q.push(w);
            d[w] = d[v] + 1;
        }
    }
}

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

    bfs(graph, 0);

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

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

#define N 100

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

queue<int> Q;

void bfs(int x){
    //初期化
    rep(i,n){
        d[i] = -1;
    }

    d[x] = 0;
    Q.push(x);

    while(!Q.empty()){
        int v = Q.front(); Q.pop();
        for(int i = 0; i < n; i++){
            if(M[v][i] == 1){
                if(d[i] == -1){
                    Q.push(i);
                    d[i] = d[v] + 1;
                }else{
                    d[i] = min(d[i], d[v] + 1);
                }
                
            }
        }
    }
}


int main(){
    cin >> n;

    rep(i,N)rep(j,N) M[i][j] = 0;

    int u, k, v;
    rep(i,n){
        cin >> u >> k;
        u--; //0オリジンに変換
        rep(j,k){
            cin >> v;
            v--; //0オリジンに変換
            M[u][v] = 1;
        }
    }

    bfs(0);

    rep(i,n){
        cout << i + 1 << " " << d[i] << endl;
    }

    return 0;
    
}

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

using namespace std;
int main(){
  int u, i, id, k, j;
  cin >> u;
  unordered_map<int, vector<int> > mymap;
  queue<vector<int> > myqueue;
  for (i = 0; i < u; ++i){
    cin >> id >> k;
    vector<int> points;
    for (j = 0; j < k; ++j){
      int a;
      cin >> a;
      points.push_back(a);
    }
    mymap[id] = points;
    if(id == 1){
      myqueue.push(points);
    }
  }
  
  bool visited[u+1];
  for (j = 1; j <= u; ++j){
    visited[j] = false;
  }
  visited[1] = true;

  int dist[u+1];
  for (j = 1; j <= u; ++j){
    dist[u] = -1;
  }
  dist[1] = 0;
  int d = 1;

  while(!myqueue.empty()){
    vector<int> combined;
    for (i = 0; i < myqueue.front().size(); ++i){
      if(!visited[myqueue.front()[i]]){
        dist[myqueue.front()[i]] = d;
        visited[myqueue.front()[i]] = true;
        if(combined.empty()){
          combined = mymap[myqueue.front()[i]];
        }else{
          combined.insert(combined.end(), mymap[myqueue.front()[i]].begin(), mymap[myqueue.front()[i]].end());
        }
      }
    }
    if(!combined.empty()){
      myqueue.push(combined);
    }
    myqueue.pop();
    d++;
  }

  for (j = 1; j <= u; ++j){
    cout << j << " " << dist[j] << endl;
  }

}

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

int main()
{
    queue<int> Q;
    int n;
    int a, b, c;
    cin >> n;
    int D[100], in[100][100];
    for (int i = 0; i < n; i++)
    {
        D[i] = -1;
        for (int j = 0; j < n; j++)
            in[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        for (int j = 0; j < b; j++)
        {
            cin >> c;
            in[c - 1][a - 1] = 1;
        }
    }
    Q.push(0);
    D[0] = 0;
    while (!Q.empty())
    {
        int cur = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++)
        {
            if (in[i][cur] == 1 && D[i] == -1) {
                D[i] = D[cur] + 1;
                Q.push(i);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << D[i] << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long long ll;
#define MAX_N 100
int d[MAX_N];

void init(){
	rep(i,MAX_N){
		d[i] = -1;
	}
}


int main()
{
	init();
	int n;
	cin>>n;
	vector<int> *node = new vector<int>[n];
	rep(i,n){
		int num,k;
		cin>>num>>k;
		rep(j,k){
			int num;
			cin>>num;
			num--;
			node[i].push_back(num);
		}
	}
	
	d[0] = 0;
	queue<int> que;
	que.push(0);
	while(que.empty() != true){
		int currentNode = que.front();
		que.pop();
		for(auto iter = node[currentNode].begin(); iter != node[currentNode].end(); iter++){
			if( d[*iter] == -1 || d[currentNode] + 1 < d[*iter]){
				d[*iter] = d[currentNode] + 1;
				que.push(*iter);
			}
		}
	}
	rep(i,n)cout<<i+1<<" "<<d[i]<<endl;
	return 0;
}

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

#define REP(i,n) for(int i=0;i<n;++i)
#define SET(a,c) memset(a,c,sizeof(a))

const int MAX_N = 100;
const int INF = 0x3F3F3F3F;

int n, d[MAX_N], G[MAX_N][MAX_N] = {0};

void bfs(int s) {
  int u;
  queue<int> q;
  q.push(s);
  SET(d, INF);
  d[s] = 0;
  while (!q.empty()) {
    u = q.front(); q.pop();
    REP(v, n) {
      if (!G[u][v]) continue;
      if (d[v] != INF) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  REP(i, n) cout << i + 1 << ' ' << ( (d[i] == INF) ? (-1) : d[i] ) << endl;
}

int main() {
  int u, k, v;
  cin >> n;
  REP(i, n) {
    cin >> u >> k;
    REP(j, k) {
      cin >> v;
      G[u - 1][v - 1] = 1;
    }
  }
  bfs(0);
  return 0;
}