Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
#include<queue>
using namespace std;

static const int N = 100;
static const int INF = (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] = 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(void) {
	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;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, G[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(G[u][v] == 0) continue;
      if(d[v] != 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++) G[i][j] = 0;
  }

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

  bfs(0);
  return 0;
}

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

using namespace std;

static const int N=100;
static const int INF=-1;
int M[N][N],n;
int d[N];

void bfs(int s){
  queue<int> q;
  q.push(s);

  for(int i=0;i<n;i++){
    d[i]=INF;
  }

  d[s]=0;

  while(!q.empty()){
    int 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);
    }
  }
}

int main(){
  cin>>n;

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

  int u,k,v;

  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);

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

C++: #include <iostream>
using namespace	std;
int n,g[100][100],f[100],c[100];
void bfs(int now,int far){
  f[now]=far;
  c[now]=1;
  far++;
  for(int i=0;i<n;i++) if(g[now][i]&&f[i]!=1) bfs(i,far);

}
int main(){

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    f[i]=-1;
    cin>>a>>b;
    for(int j=0;j<b;j++){
      cin>>tmp;
      g[a-1][tmp-1]=1;
    }
  }

  bfs(0,0);

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

  return 0;

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int a[100],d[100],f[100];
int inf=100000,t=0,n;
int v[100][100];
void bfs(int s){
    d[s]=0;
    a[s]=inf;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=0;i<n;i++){
            if(v[u][i]==1&&a[i]!=inf){
                a[i]=inf;
                q.push(i);
                d[i]=d[u]+1;
                //cout<<d[i]<<" "<<d[u]<<endl;
            }
        }
    }
}
int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    int u,k;
    cin>>u>>k;
    for(int j=0;j<k;j++){
      int tmp;
      cin>>tmp;
      v[u-1][tmp-1]=1;
    }
  }
  bfs(0);
  for(int i=0;i<n;i++)
      if(!a[i])d[i]=-1;
  for(int i=0;i<n;i++){
        cout<<i+1<<" "<<d[i]<<endl;
  }
}

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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

const int INF  = 1000001;

int main() {
    int n; cin>>n;
    
    vector<int> to[100];
    rep(i,n){
        int a,m;
        cin>>a>>m;
        --a;
        
        rep(j,m){
            int b;cin>>b;
            --b;
            to[a].push_back(b);
//            to[b].push_back(a);
//          cout<<a<<" "<<b<<endl;
        }
    }

    queue<int> q;
    vector<int> dist(n, INF);
    dist[0]=0;
    q.push(0);

    while(!q.empty()){
        int v = q.front(); q.pop();
      for(int u : to[v]){
        if (dist[u] != INF) continue;
            dist[u] = dist[v] + 1;
            q.push(u);
//          cout<<u<<" "<<dist[u]<<endl;
        }
    }
  
    rep(i,n){
        int res1 = i+1;
        int res2 = dist[i];
        if(i == 0)res2 = 0;
        if(res2 == INF) res2 = -1;
        cout<<res1<<" "<<res2<<endl;
    }
    
	return 0;
}

C++: #include<iostream>
#include<cstdlib>
using namespace std;
int main() {
	int n, u, k[101], data[101][100]; cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> u >> k[i];
		for (int j = 0; j<k[i]; ++j) cin >> data[u][j];
	}
	int queue[10000], head = 0, tail = 1, cost[101];
	for (int i = 2; i <= n; ++i) cost[i] = -1;
	queue[0] = 1; cost[1] = 0;
	while (head != tail) {
		int val = queue[head++];
		for (int i = 0; i < k[val]; ++i) {
			if (cost[data[val][i]] == -1) {
				queue[tail++] = data[val][i];
				cost[data[val][i]] = cost[val] + 1;
			}
		}
	}
	for (int i = 1; i <= n; ++i) cout << i << " " << cost[i] << endl;
	return 0;
}