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

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

	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 <queue>
#define MAX_V 101
#define INF 10000000
using namespace std;

bool edge[MAX_V][MAX_V];
int dist[MAX_V];

void bfs(int start, int n) {
  queue<int> que;
  que.push(start);
  dist[start] = 0;
  int current;
  while (!que.empty()) {
    current = que.front();
    que.pop();
    for (int i = 1; i <= n; i++) {
      if (edge[current][i] && dist[i] == INF) {
	dist[i] = dist[current] + 1;
	que.push(i);
      }
    }
  }
}

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

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      edge[i][j] = false;
    }
  }
  for (int j = 0; j < n; j++) {
    int u, k;
    cin >> u >> k;
    for (int i = 0; i < k; i++) {
      int v;
      cin >> v;
      edge[u][v] = true;
    }
  }

  for (int i = 1; i <= n; i++) {
    dist[i] = INF;
  }

  bfs(1, n);

  for (int i = 1; i <= n; i++) {
    cout << i << ' ';
    if (dist[i] == INF) cout << -1 << endl;
    else cout << dist[i] << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
    int n; cin >> n;
    vector< vector<int> > g(n, vector<int>());
    for (int i = 0; i < n; i++)
    {
        int u, k; cin >> u >> k; u--;
        for (int j = 0; j < k; j++)
        {
            int v; cin >> v; v--;
            g[i].push_back(v);
        } 
    }
    vector<bool> seen(n, false);
    vector<int> d(n, -1); d[0] = 0;
    queue<int> q;
    q.push(0);
    int depth = 0;
    while (!q.empty())
    {
        depth++;
        queue<int> qt;
        while (!q.empty())
        {
            qt.push(q.front()); 
            q.pop();
        }
        while (!qt.empty())
        {
            int x = qt.front(); qt.pop();
            seen[x] = true;
            for (int i = 0; i < g[x].size(); i++)
            {
                if(d[g[x][i]] == -1) d[g[x][i]] = depth;
                if (seen[g[x][i]] == false) q.push(g[x][i]);
            }
        }
    }
    for (int i = 0; i < n; i++)
        cout << i + 1 << " " << d[i] << endl;
}

C++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_11_C: Breadth First Search

#include <algorithm>
#include <cstdio>
#include <queue>

using uint = unsigned int;

class Graph {
  const uint n_;
  uint* const g_;
  int* const depth_;
  
public:
  Graph(uint n) : n_(n),
                  g_(new uint[(n_*n_)]),
                  depth_(new int[n])
  {
    std::fill(g_, g_+(n_*n_), 0u);
  }

  ~Graph()
  {
    delete[] g_;
    delete[] depth_;
  }
  
  uint* operator [](uint i) const { return g_+(n_*i); }

  int* depth() const { return depth_; }

  void bfs()
  {
    init_depth();
    std::queue<uint> q;
    auto i = 0u;
    q.push(i);
    depth_[i] = 0;
    auto last_visit = i;
    while (!q.empty()) {
      i = q.front();
      for (auto j = last_visit+1; j < n_; ++j) {
        if ((*this)[i][j] && depth_[j] == -1) {
          q.push(j);
          depth_[j] = depth_[i]+1;
        }
      }
      q.pop();
      last_visit = i;
    }
  }

private:
  void init_depth()
  {
    std::fill(depth_, depth_+(n_), -1);
  }
};

int main()
{
  uint n;
  std::scanf("%u", &n);

  Graph g(n);

  uint u, k, v;
  for (auto i = 0u; i < n; ++i) {
    std::scanf("%u %u", &u, &k);
    for (auto j = 0u; j < k; ++j) {
      std::scanf("%u", &v);
      g[u-1][v-1] = 1;
    }
  }

  g.bfs();
  auto depth = g.depth();
  for (auto i = 0u; i < n; ++i) {
    std::printf("%u %d\n", i+1, depth[i]);
  }
}

// eof

C++: // ALDS1_11_C.cpp
// Graph I - BFS
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct Vertex
{
    int id = 0;
    vector<int> adjList;
};
int tt = 0;
void bfs(vector<Vertex> &V, vector<int> &dist, int i)
{
    queue<Vertex> mq;
    mq.push(V[i]);
    dist[i] = 0;
    while(!mq.empty()) {
        Vertex tmpV = mq.front();
        mq.pop();
        for (int j = 0; j < tmpV.adjList.size(); j++) {
            int next = tmpV.adjList[j];
            if (dist[next] == -1) {
                mq.push(V[next]);
                dist[next] = dist[tmpV.id] + 1;
            }
        }
    }
}

int main()
{
    int n;
    cin >> n;
    vector<Vertex> V(n);
    vector<int> dist(n, -1);
    int id;
    int num;
    for (int i = 0; i < n; i++) {
        cin >> id >> num;
        id--;
        V[id].id = id;
        int tmp;
        for (int j = 0; j < num; j++) {
            cin >> tmp;
            tmp--;
            V[id].adjList.push_back(tmp);
        }
    }
    bfs(V, dist, 0);
    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << dist[i] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <utility>
#include <ctime>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define vector_rsort(x) sort(all(x));reverse(all(x))
#define array_rsort(x,n) sort(x,x+n);reverse(x,x+n);
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))

#define Yes cout<<"Yes"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define NO cout<<"NO"<<endl
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int n,u,k,v;
int g[105][105];

void init(){
  rep(i,105){
    rep(j,105){
      g[i][j]=INF;
    }
  }
}

void wf(){
  rep(k,105){
    rep(i,105){
      rep(j,105){
	g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
      }
    }
  }
}

int main(){
  init();
  cin>>n;
  rep(i,n){
    cin>>u>>k;
    rep(i,k){
      cin>>v;
      g[u][v]=1;
    }
  }
  wf();
  cout<<1<<" "<<0<<endl;
  for(int i=2;i<=n;i++){
    if(g[1][i]==INF)cout<<i<<" "<<-1<<endl;
    else cout<<i<<" "<<g[1][i]<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define N 100
#define INFTY (1<<21)

using namespace std;

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++) {
		cin >> u >> k;
		u--;
		for (int j =0; j<k;j++) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	bfs(0);
	return 0;
}