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++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_A
// Graph I - Breadth First Search

#define SUBMIT

#include <utility>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <limits>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <cstring>
#include <climits>
#include <memory>
#include <queue>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

const ui64 MAX_N = 100;

enum class color_type {
    white,
    gray
};

const int NIL = -1;

int adj[MAX_N][MAX_N];
color_type color[MAX_N];
int dist[MAX_N];
int n;
queue<int> q;

void bfs(int u) {
    q.push(u);

    while (!q.empty()) {
        u = q.front();
        q.pop();

        for (int v = 0; v < n; ++v) {
            if (adj[u][v] == 1 && color[v] == color_type::white) {
                q.push(v);
                color[v] = color_type::gray;
                dist[v] = dist[u] + 1;
            }
        }
    }
}

void start_bfs() {
    while (!q.empty()) q.pop();

    dist[0] = 0;
    color[0] = color_type::gray;
    bfs(0);
}

int main() {
    memset(adj, 0, sizeof(adj));
    memset(dist, NIL, sizeof(dist));
    cin >> n;

    int u, degree;
    for (int i = 0; i < n; ++i) {
        cin >> u >> degree;
        u--; // 0-origin

        int v;
        for (int j = 0; j < degree; ++j) {
            cin >> v;
            v--; //0-origin
            adj[u][v] = 1;
        }
    }

    start_bfs();
    for (int v = 0; v < n; ++v) cout << v + 1 << " " << dist[v] << endl;
    return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef pair<int,int> PII;
typedef long long LL;

#define d(x)  cout<<#x<<" = "<<(x)<<endl;
#define c cout<<"#"<<endl;
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define REACH(i,c) for(typeof((c).rbegin()) i=(c).rbegin(); i!=(c).rend(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c,n) sort(c,c+n)
#define VSORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define DFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define DREP(i,n) DFOR(i,n,0)
#define INF 2000000000

const double EPS = 1e-10;
const double PI  = acos(-1.0);
////////////////////////////////////////////////
int n,a[101][101],d[101],p[101];
QI q;
int main()
{
	cin>>n;
	FOR(i,1,n+1)
	{
		int k,m;cin>>k>>m;
		a[k][0]=m;
		FOR(j,1,m+1)
		{	
			int x;cin>>x;
			a[k][j]=x;
		}
	}
	REP(i,101)p[i]=1;
	for(q.push(1);SZ(q)!=0;)
	{
		int u=q.front();
		q.pop();
		for(;p[u]<=a[u][0];)
		{
			int v=a[u][p[u]];p[u]++;
			if(v!=1&&d[v]==0)
			{
				d[v]=d[u]+1;
				q.push(v);
			}
		}
	}
	FOR(i,1,n+1)
	{
		cout<<i<<" "<<d[i]<<endl;
	}
	return 0;
}

C++: #include <queue>
#include <map>
#include <cstdio>

using namespace std;

int main(){
	int t=0,T,i,j,k;
	map<int,vector<int> >a;
	for(scanf("%d",&T); t<T; t++)for(scanf("%d%d",&i,&k); k--; a[i].push_back(j))scanf("%d",&j);
	map<int,int>m;
	queue<int>q;
	for(q.push(1),m[1]=1;!q.empty();){
		k = q.front();
		q.pop();
		for (i = 0; i < a[k].size(); i++){
		    if(!m[a[k][i]])m[a[k][i]]=m[k]+1,q.push(a[k][i]);
		}
	}
	for(i=1;i<=T;i++){
	    printf("%d %d\n",i,m[i]-1);
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
int map[101][101];
int all = 0;
int search_2[101];
int n;
void bfs(int k, int m) {
	search_2[k] = m;
	queue<int>q;
	for (int i = 2; i <= n; i++) {
		if (map[k][i] && (search_2[i] == 0 || search_2[i] > m + 1)) {
			q.push(i);
		}
	}
	while (!q.empty())
	{
		bfs(q.front(), m + 1);
		q.pop();
	}
}
int main(void) {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		int m;
		cin >> m;
		for (int j = 0; j < m; j++) {
			int b;
			cin >> b;
			map[a][b] = 1;
		}
	}
	bfs(1, 0);
	cout << 1 << " " << 0 << endl;
	for (int i = 2; i <= n; i++) {
		cout << i << " ";
		if (search_2[i] == 0) cout << -1 << endl;
		else cout << search_2[i] << endl;
	}
}

C++: ///
// File:  alds1_11_c.cpp
// Author: ymiyamoto
//
// Created on Wed Oct 18 00:49:05 2017
//

#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
static uint32_t graph[100][100];
static vector<int32_t> d(100, -1);
static queue<uint32_t> q;

static void bfs(uint32_t n)
{
  q.push(0);
  d[0] = 0;

  while (!q.empty()) {
    uint32_t index = q.front();
    q.pop();

    for (uint32_t i = 0; i < n; i++) {
      if (d[i] == -1 && graph[index][i] == 1) {
        q.push(i);
        d[i] = d[index] + 1;
      }
    }
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t id, k;
    cin >> id >> k;
    for (uint32_t j = 0; j < k; j++) {
      uint32_t val;
      cin >> val;
      graph[id - 1][val - 1] = 1;
    }
  }

  bfs(n);

  for (uint32_t i = 0; i < n; i++) {
    if (i != 0) cout << " ";
    cout << d[i];
  }
  cout << endl;

  return 0;
}