Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: #include<bits/stdc++.h>

using namespace std;

#define rep(a, b) for(int i = a; i <= b; i++)
#define mst(a) memset(a, 0, sizeof(a))

const int MAX = 1e2 + 10;


bool mtx[MAX][MAX];
int vis[MAX];
int n;

queue<int> q;

void init(void)
{
	while(!q.empty())
		q.pop();
	mst(vis);
	return;
}

bool check(int x, int y)
{
	if(mtx[x][y] && !vis[y])
		return true;
	return false;
}

void GetAns(void)
{
	rep(2, n)
	{
		if(vis[i] == 0)
			vis[i] = -1;
	}
	return;
}

void Bfs(void)
{
	int cnt = 1;
	q.push(1);
	while(!q.empty())
	{
		int cur = q.front();
		q.pop();
		rep(1, n)
		{
			if(check(cur, i))
			{
				q.push(i);
				vis[i] = vis[cur] + 1;
				cnt++;
				if(cnt == n)
					return;
			}
		}
	}
	return;
}

int main()
{
	while(~scanf("%d", &n))
	{
		init();
		rep(1, n)
		{
			int row;
			scanf("%d", &row);
			int num;
			scanf("%d", &num);
			while(num--)
			{
				int tmp;
				scanf("%d", &tmp);
				mtx[row][tmp] = true;
			}
		}
		
		Bfs();

		GetAns();
		
		rep(1, n)
			printf("%d %d\n", i, vis[i]);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
bool a[100][100];
int D[100];
void bfs( int src) {
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while( ! Q.empty() ) {
		int cur = Q.front();
		Q.pop();
		for(int dst = 0; dst < n; dst++ ) {
			if( D[dst] < 1 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cerr << i+1 <<  " " << D[i] <<  endl;
	}
}

int main() {
	cin >> n;
	for ( int i = 0;i < n;i++) {
		D[i] = -1;
		for(int j= 0; j < n;j++) {
			a[i][j] = 0;
		}
	}
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
	bfs(0);
}

C++: // AOJ
// ALDS1_11_C Breadth First Search
// https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
#define rep(i, n) for(int i = 0; i < (n); i++)

int main()
{
    int n; cin >> n;
    vvi g(n, vi(n, 0));
    rep(i, n) {
        int u, k; cin >> u >> k; u--;
        rep(j, k) {
            int v; cin >> v; v--;
            g[u][v] = 1;
        }
    }

    vi ans(n, -1);
    queue<int> q;
    q.push(0);
    ans[0] = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        rep(i, n) {
            if (g[u][i] == 1 && ans[i] == -1) {
                q.push(i);
                ans[i] = ans[u]+1;
            }
        }
    }

    rep(i, n) cout << i+1 << " " << ans[i] << endl;
}

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

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

int n, M[N][N];
int d[N];

void dfs(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 <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, v, k;
    cin >> n;

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            M[i][i] = 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);
}

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

struct StDir
{
  int m_nBgn;
  int m_nEnd;

  StDir(int nBgn, int nEnd) : m_nBgn(nBgn), m_nEnd(nEnd)
  { }
};

void fnInput(queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  int nxSiz;
  cin >> nxSiz;

  rvnDst.resize(nxSiz + 1, 0);
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      StDir oDir(nx, ny);
      rqoBFSrch.push(oDir);
    }
  }
}

void fnBreadthFirstSearch(queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  while (rqoBFSrch.size())
  {
    StDir oDir = rqoBFSrch.front(); rqoBFSrch.pop();

    int nDst = rvnDst[oDir.m_nBgn] + 1;
    if (!rvnDst[oDir.m_nEnd])
      rvnDst[oDir.m_nEnd] = nDst;
    else
      rvnDst[oDir.m_nEnd] = min(rvnDst[oDir.m_nBgn], nDst);
  }
}

void fnResult(const vector<int>& cnrvnDst)
{
  for (int i = 1; i < cnrvnDst.size(); ++i)
    cout << i <<  " " << cnrvnDst[i] << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  queue<StDir> qoBFSrch;
  vector<int>  vnDst;

  fnInput(qoBFSrch, vnDst);
  fnBreadthFirstSearch(qoBFSrch, vnDst);
  fnResult(vnDst);

  return 0;
}

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

class node {
public:
	int d = -1, k, v[100];
	bool t = false;
};

node V[101];
int E[101], e1 = 0, e2 = 0;

void Serch(int u) {
	for (int i = 0; i < V[u].k; i++) {
		if (V[V[u].v[i]].t == false) {
			E[e1] = V[u].v[i]; e1++;
			V[V[u].v[i]].t = true;
			V[V[u].v[i]].d = V[u].d+1;
		}
	}
	if (e2 < e1) { e2++; Serch(E[e2]); }
}

int main() {
	int n, u;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u; cin >> V[u].k;
		for (int j = 0; j < V[u].k; j++) {
			cin >> V[u].v[j];
		}
	}

	for (int i = 1; i <= n; i++) {
		E[0] = 1; V[1].t = true; V[1].d = 0; e1++;
		Serch(E[e2]);
	}

	for (int i = 1; i <= n; i++) cout << i << " " << V[i].d << endl;
	return 0;
}