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 <queue>
#include <cstdlib>
int INF = 1e9;
int main(){
    long long n;
    std::cin >> n;
    std::vector<int> G[n];
    std::vector<int> used(n, 0);
    for(int i = 0; i < n; i++){
        int a, k;
        std::cin >> a >> k;
        a--;
        for(int j = 0; j < k; j++){
            int b;
            std::cin >> b;
            b--;
            G[a].push_back(b);
        }
    }
    std::vector<int> d(n, INF);
    std::queue<int> q;
    q.push(0);
    d[0] =0;
    while(!q.empty()){
        int t = q.front();
        q.pop();
        used[t] = 1;
        for(int i: G[t]){
            if(used[i] == 0){
                q.push(i);
                d[i] = std::min(d[i], d[t] + 1);
            }
        }
    }
    for(int i = 0; i < n; i++) if(d[i] == INF) d[i] = -1;
    for(int i = 0; i < n; i++) std::cout << i + 1 << ' ' << d[i] <<  std::endl;
}

C++: #include<iostream>
using namespace std;
int n, u,k[101], v[101][101], out[101];

void hitung(int a, int b){
	int i;
	out[a]=b;
	for(i=1; i<=k[a]; i++){
		if(out[v[a][i]]==-1 || out[v[a][i]]>b+1){
			hitung(v[a][i],	b+1);
		}
	}
}

int main(){
	int i,j;
    cin>>n;
    for(i=1; i<=n; i++){
        cin>>u>>k[i]; //getchar();
		out[i]=-1;
        for(j=1; j<=k[i]; j++){
            cin>>v[i][j];// getchar();
        }
    }
    
    hitung(1,0);
	for(i=1;i<=n;i++){
		cout<<i<<" "<<out[i]<<endl;
	}


    //getchar();
    return 0;
}

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

const int maxn=(1<<21);
int G[110][110];///构造图
int d[110];///该顶点到顶点1的距离
bool vis[110];
int n;

void BFS(){
    queue<int> q;
    q.push(1);
    d[1]=0;
    vis[1]=true;
    while(!q.empty()){
        int top=q.front();
        q.pop();
        //vis[top]=true;
        for(int i=1;i<=n;i++){
            if(G[top][i]&&vis[i]==false){
       //     if(G[top][i]&&d[i]==maxn){
                d[i]=d[top]+1;
                vis[i]=true;
                q.push(i);
            }
        }
    }
}


int main(){
    scanf("%d",&n);
    memset(G,0,sizeof(G));
    memset(vis,0,sizeof(vis));
    fill(d,d+110,maxn);

  //  memset(d,maxn,sizeof(d));
    for(int i=0;i<n;i++){
        int u,k;
        scanf("%d%d",&u,&k);
        for(int j=0;j<k;j++){
            int v;
            scanf("%d",&v);
            G[u][v]=1;
        }
    }

    BFS();
    for(int i=1;i<=n;i++){
        printf("%d %d\n",i,(d[i]==maxn)?(-1):d[i]);
    }
    return 0;
}

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

using namespace std;

int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    fill(D, D + N, -1);
    D[src] = 0;
    while (! Q.empty()){
        int cur = Q.front();
        Q.pop();
        cerr << cur+1 << " " << D[cur] << endl;
        for (int k = 0; k < N; k++){
            if (G[cur][k] == 1 && D[k] <= D[cur]){
                D[k] = D[cur]+1;
                Q.push(k);
            }
        }
    }
}

int main(){
	scanf("%d", &N);
		int id, c;
		for (int i = 0; i < N; i++){
			scanf("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
			scanf("%d", V + id);
			for (int j = 0; j < V[id]; j++){
				scanf("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
				G[id][c] = 1;
			}
		}//データの読み込み完了。
    bfs(0);
}

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

int G[110][110] = {0};
int n;
int D[110] = {0};
void bfs(int src){
 queue<int> Q;
 Q.push(src);
 D[src] = 0;
 while(! Q.empty()){
  int cur = Q.front();
  Q.pop();
  for(int i=1; i<=n; i++){
   if(G[cur][i]==1 && D[i]==0){
    D[i] = D[cur]+1;
    Q.push(i);
   }
  }
 }

 for(int i=1; i<=n; i++){
  if(i!=1 && D[i]==0){
   D[i]=-1;
  }
  cout << i << " " << D[i] << endl;
 }
}

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

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

static const int N = 100;
static const int no = -1;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[N][N] = {{}}, d[N] = {}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for(int i = 1; i <= n; i++) d[i] = no;
  d[v] = 0;
  while(!q.empty()) {
    u = q.front(); q.pop();
    for(int i = 1; i <= n; i++) {
      if((graph[u][i] == 0) || (d[i] != no)) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
	ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int dx[8] = {1,0,-1,0,1,-1,-1,1};//右、上、左、下、右上、左上、左下、右下のつもり
int dy[8] = {0,1,0,-1,1,1,-1,-1};//実際は色々反転している。どうせ4か8方向調べるから関係ないとしている。
bool seen[60][60]={true};
int main(void){
    int n;cin >> n;
    vector<vector<int> > x(n+1);
    for(int i=0;i<n;i++){
	int a,b; cin >> a >> b;
	for(int j=0;j<b;j++){
	    int c;cin >> c;
	    x[a].push_back(c);
	}
    }
    
    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 i=0;i<x[v].size();i++){
	    if(dist[x[v][i]]!=-1) continue;
	    dist[x[v][i]] = dist[v] + 1; 
	    que.push(x[v][i]);
	}
    }
    for(int i=1;i<=n;i++){
	cout << i << " " << dist[i] << endl;
    }
    return 0;
}