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 <queue>

using namespace std;
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] == 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++)
        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<queue>
#include<math.h>
#include<cmath>
#include<bitset>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<iostream>
#include<utility>
using namespace std;
int main(){
  vector<vector<int> > g(110);
  int INF=(1<<29);
  int x,n,i,j,v,k,u,ans[110];
  queue<int> q;
  scanf("%d\n",&n);
  for(i=0;i<n;i++){
    scanf("%d %d",&u,&k);
    u--;
    for(j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      g[u].push_back(v);
    }
  }
  for(i=0;i<n;i++){
    ans[i]=INF;
  }
  q.push(0);
  ans[0]=0;
  while(!q.empty()){
    x=q.front();
    q.pop();
    for(i=0;i<g[x].size();i++){
      if(ans[g[x][i]]>ans[x]+1){
        ans[g[x][i]]=ans[x]+1;
        q.push(g[x][i]);
      }
    }
  }
  for(i=0;i<n;i++){
    if(ans[i]==INF){
      ans[i]=-1;
    }
  }
  for(i=0;i<n;i++){
    printf("%d %d\n",i+1,ans[i]);
  }
  return 0;
}

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

class vertex{
public:
  vector<vertex*> to;
  bool visited;
  int num;
  int dis;

  vertex(){
    visited=false;
    dis = -1;
  }

  void bfs(int dis){
    this->dis=dis;
    visited=true;
    queue<vertex*> qv; qv.push(this);

    while(!qv.empty()){
      vertex* now = qv.front();  qv.pop();

      for(int i=0; i<now->to.size(); i++){
        if(!now->to[i]->visited){
          qv.push(now->to[i]);
          now->to[i]->visited = true;
          now->to[i]->dis = now->dis+1;
        }
      }
    }
  }

  void print(){
    cout << num << " " << dis << endl;
  }
};

int main(){
  int n; cin >> n;
  vertex graph[n+1];

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

  graph[1].bfs(0);

  for(int v=1; v<=n; v++){
    graph[v].print();
  }

  return 0;
}

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

using namespace std;

const int INF = 100000000;

int main(){
	int n;
	cin >> n;
	vector<vector<int>> node(n+1);
	for(int i=0;i<n;i++){
		int u,k,tmp;
		cin >> u >> k;
		for(int j=0;j<k;j++){
			cin >> tmp;
			node[u].push_back(tmp);
		}
	}
	
	//search
	vector<int> res(n+1,INF);
	queue<int> que;
	queue<int> copied;
	queue<int> zeros;
	que.push(1);
	
	int count = 0;
	for(int i=0;i<n-1;i++){
		while(que.size()){
			int k = que.front(); que.pop();
			//cout << "-> " << k << endl;
			if(res[k] >= INF) res[k] = count;
			for(int v:node[k]){
				copied.push(v);
			}
		}
		count++;
		que = copied;
		copied = zeros;
	}

	//output
	for(int i=1;i<=n;i++){
		if(res[i] >= INF) res[i] = -1;
		cout << i << " " << res[i] << endl;
	}

	return 0;
}

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

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

    // グラフ入力受取 (ここでは無向グラフを想定)
    Graph G(N);
    REP(i,N){
        int u,k; cin >> u >> k;
        REP(i,k){
            int vlist; cin >> vlist;
            G[u-1].push_back(vlist-1);
        }
    }

    // BFS のためのデータ構造
    vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[0] = 0;
    que.push(0); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (int nv : G[v]) {
            if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    // 結果出力 (各頂点の頂点 0 からの距離を見る)
    for (int v = 0; v < N; ++v) cout << v+1 << " " << dist[v] << endl;
}

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

vector< vector < int > > adjList;
vector< int > depths;

void bfs(void) {
  queue< int > currentLevel;
  queue< int > nextLevel;
  nextLevel.push(1);
  int depth = 0;
  while (nextLevel.size()) {
    while (nextLevel.size()) {
      currentLevel.push(nextLevel.front());
      nextLevel.pop();
    }
    while (currentLevel.size()) {
      if (depths[currentLevel.front()-1] == -1) {
	depths[currentLevel.front()-1] = depth;
	for (int i = 0; i < adjList[currentLevel.front()-1].size(); i++)
	  nextLevel.push(adjList[currentLevel.front()-1][i]);
      }
      currentLevel.pop();
    }
    depth++;
  }
}

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  adjList = vector< vector < int > > (n, vector< int > ());
  depths = vector< int > (n, -1);
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int holder;
      cin >> holder;
      adjList[i].push_back(holder);
    }
  }
  bfs();
  for (int i = 0; i < n; i++)
    cout << i+1 << ' ' << depths[i] << endl;
  return 0;
}