Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

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

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int d[128][128],solve[128];
int main(){
      //input
      int n,m,row,t;
      queue<int> que;
      cin >> n;
      for(int i= 0;i<n;i++){
            cin >>row>>m;
            for(int j = 0;j<m;j++){
                  cin>>t;
                  d[row][t] = 1;
            }
      }
      //debug
      //for(int i = 0;i<=n;i++){
      //for(int j = 0;j<=n;j++)cout << d[i][j] << ' ';cout << endl;}

      //solve
      fill(solve,solve+n+2,-1);
      que.push(1);
      solve[1] = 0;
      while(!que.empty())
      {
            int p = que.front();
            que.pop();
            for(int i = 1;i<=n;i++)
            {
                  if(d[p][i] && solve[i]==-1)
                  {
                        que.push(i);
                        solve[i] = solve[p] + 1;
                  }
            }
      }
      for(int i = 1;i<=n;i++)
            cout << i << ' '<< solve[i]<<endl;


}

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 <iostream>
#include <queue>
#include <vector>
using namespace std;
int d[128][128],solve[128];
int main(){
      int n,m,row,t;
      queue<int> que;
      cin >> n;
      for(int i= 0;i<n;i++){
            cin >>row>>m;
            for(int j = 0;j<m;j++){
                  cin>>t;
                  d[row][t] = 1;
            }
      }
      fill(solve,solve+n+2,-1);
      que.push(1);
      solve[1] = 0;
      while(!que.empty()){
            int p = que.front();
            que.pop();
            for(int i = 1;i<=n;i++){
                  if(d[p][i] && solve[i]==-1){
                        que.push(i);
                        solve[i] = solve[p] + 1;
                  }
            }
      }
      for(int i = 1;i<=n;i++)cout << i << ' '<< solve[i]<<endl;
}

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

int n, u, k, v;
int a[100][100] = {{0}};
int b[100] = { - 1 };
queue<int> q;

int main() {
	cin >> n;
	for ( int i = 0; i < n; i++ ) {
		cin >> u >> k;
		for ( j = 0; j < k; j++ ) {
			cin >> v;
			a[u - 1][v - 1] = 1;
		}
	}
	b[0] = 0;
	q.push( 0 );
	while ( ! q.empty() ) {
		u = q.front();
		for ( int i = 0; i < n; i++ ) {
			if ( a[u][i] == 1 && b[i] == -1 ) {
				b[i] = b[u] + 1;
				q.push( i );
			}
		}
		q.pop();
	}
	for ( int i = 0; i < n; i++ )
		cout << ( i + 1 ) << " " << b[i] << endl;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int n, d[100], M[100][100];
queue<int>q;
void bfs(int u){
	q.push(u);
	while(!q.empty()){
		int v=q.front();
		q.pop();
		for(int i=0; i<n; i++){
			if(M[v][i]==1 && d[i]==-1){
				d[i]=d[v]+1;
				q.push(i);
			}
		}
	}
}
int main(){
	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++)
		d[i]=-1;
	d[0]=0;
	for(int i=0; i<n; i++){
		int id,k;
		cin>>id>>k;
		for(int j=0; j<k; j++){
			int child;
			cin>>child;
			M[id-1][child-1]=1;
		}
	}
	bfs(0);
	for(int i=0; i<n; i++)
		cout<<i+1<<" "<<d[i]<<endl;
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <math.h>
#include <vector>
#include <list>
#define inf 200
using namespace std;

int n;
list<int> adj[101];
int d[101];
bool f[101];
void BFS(int v)
{
    int m = 0;
    f[v] = false;
    while (!adj[v].empty())
    {
        d[adj[v].front()] = min(d[v] + 1, d[adj[v].front()]);
        adj[v].pop_front();
    }
    for (int i = 1; i <= n; i++)
    {
        if (f[i] && d[i] < d[m])
            m = i;
    }
    if (m > 0)
        BFS(m);
}

void print()
{
    for (int i = 1; i <= n; i++)
    {
        if (d[i] == inf)
            d[i] = -1;
        printf("%d %d\n", i, d[i]);
    }
}

int main()
{
    int u, k, v;
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> u >> k;
        while (k > 0)
        {
            cin >> v;
            adj[u].push_back(v);
            k--;
        }
    }
    for (int i = 0; i <= n; i++)
    {
        d[i] = inf;
        f[i] = true;
    }
    d[1] = 0;
    BFS(1);

    print();

    return 0;
}

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

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == u) return 0;
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		if (d_min == -1) d_min = next[i]->breadthFirstSearch(id) + 1;
		else d_min = min(next[i]->breadthFirstSearch(id) + 1, d_min);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);

static const int N = 100;
static const int INF = (1<<21);

int d[N]; //距離で訪問状態を管理する
int n, M[N][N];

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

  REP(i,n) d[i] = INF;

  d[s] = 0;
  int u;
  while(!q.empty()){
    u = q.front(); q.pop();
    REP(v,n){
      //つながっていなかったら，飛ばす
      if(M[u][v] == 0) continue;
      //既に訪れていたら，飛ばす
      if(d[v] != INF) continue;

      d[v] = d[u] + 1;
      q.push(v);
    }
  }

  REP(i,n){
    cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i] ) << endl;
  }




}
int main(){
  int u, k, v;
  cin >> n;
  REP(i,n){
    REP(j,n) M[i][j] = 0;
  }

  REP(i,n){
    cin >> u >> k;
    --u;
    REP(i,k){
      cin >> v;
      --v;
      M[u][v] = 1;
    }
  }

  bfs(0);
  return 0;
}