Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

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

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

using namespace std;
static const int INF = 1e9;
static const int SIZE = 101;
typedef pair<int, int> P;

int d[SIZE];
vector<int> G[SIZE];

void solve()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; ++j)
		{
			int v;
			cin >> v;
			G[u].push_back(v);
		}
	}
	for (int i = 0; i < SIZE; ++i)
	{
		d[i] = INF;
	}
	d[1] = 0;
	queue<P> que;
	que.push(P(0, 1));
	while (!que.empty())
	{
		P p = que.front();
		que.pop();
		int v = p.second;
		for (int i = 0; i < G[v].size(); ++i)
		{
			int to = G[v][i];
			if (d[to] > p.first + 1)
			{
				d[to] = p.first + 1;
				que.push(P(d[to], to));
			}
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		cout << i << " " << d[i] << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <array>
#include <algorithm>
#include <stack>
#include <queue>

const int nmax = 105;
const int NIL = -1;
int n;
// state of vertex
// 0: not visited
// 1: visited
// 2: complete search from the vertex
std::array<int, nmax> color, d;

std::array<std::array<int, nmax>, nmax> adj;

void bfs(int v)
{
  std::queue<int> q;
  q.push(v);
  d[v] = 0;

  while (!q.empty())
    {
      v = q.front();
      for (int i=0; i<n; ++i)
        {
          if (adj[v][i] == 1 && d[i] == NIL)
            {
              q.push(i);
              d[i] = d[v] + 1;
            }
        }
      q.pop();

    }
}
int main ()
{
  std::cin >> n;

  color.fill(0);
  std::for_each(adj.begin(), adj.end(), [](auto x){x.fill(0);});
  for (int i=0; i<n; ++i)
    {
      int u, k;
      std::cin >> u >> k;
      for (int j=0; j<k; ++j)
        {
          int v;
          std::cin >> v;
          adj[u-1][v-1] = 1;
        }
    }

  d.fill(NIL);
  bfs(0);

  for (int i=0; i<n; ++i)
    {
      std::cout << i+1 << ' ' << d[i] << std::endl;
    }
}

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

const int N = 100;
const int WHITE = 0;
const int BLACK = 1;

int n, adj_mat[N][N];
int d[N];
int distance;
bool color[N];

int Next(int node_id)
{
    for (int i = 0; i < n; i++) {
        if (adj_mat[node_id][i] == 1 && color[i] == WHITE) {
            color[i] = BLACK;
            return i;
        }
    }
    return -1;
}

void BFS()
{
    int node_id = 0;
    std::queue<int> q;
    q.push(node_id);
    d[node_id] = 0;
    color[node_id] = BLACK;

    while (!q.empty()) {
        int target = q.front();
        q.pop();
        while (1) {
            int next = Next(target);
            if (next < 0) break;
            q.push(next);
            d[next] = d[target] + 1;
        }
    }
}

void Init(int n)
{
    distance = 0;
    for (int i = 0; i < n; i++) {
        d[i] = 0;
        color[i] = WHITE;
        for (int j = 0; j < n; j++) {
            adj_mat[i][j] = 0;
        }
    }
}

int main(int argc, char *argv[])
{
    scanf("%d", &n);

    Init(n);    

    // make adj matrix
    for (int i = 0; i < n; i++) {
        int node_id, adj_num;;
        scanf("%d", &node_id);
        scanf("%d", &adj_num);
        node_id--;
        for (int j = 0; j < adj_num; j++) {
            int adj_id;
            scanf("%d", &adj_id);
            adj_id--;
            adj_mat[node_id][adj_id] = 1;
        }
    }

    // depth search
    BFS();
    for (int i = 0; i < n; i++) {
        if (color[i] == WHITE) d[i] = -1;
    }

    // dump adj matrix
    for (int i = 0; i < n; i++) {
        fprintf(stdout, "%d %d\n", i+1, d[i]);
    }

    return 0;
}

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

int main(){
  queue<int> q;
  int n,k,u,max=0;
  int g[101]={},f[101];
  int d[101][101]={};
  int di;
  cin>>n;
  for(int i=0;i<101;i++) f[i]=-1;
  f[1]=0;
  g[1]=1;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    if(max<k) max=k;
    for(int j=0;j<k;j++){
      cin>>di;
      if(g[di]==0){
	g[di]=1;
	d[u][j]=di;
      }
    }
  }

  q.push(1);

  int cnt=1,fg;
  int a;
  while(!q.empty()){
    fg=0;
 a=q.front();
    for(int i=0;i<=max;i++){

      if( d[a][i]>0){
	fg=1;
	q.push(d[a][i]);
	f[d[a][i]]=cnt;
      }
    }
      q.pop();
    if(fg==1) cnt++;
  }
  for(int i=1;i<=n;i++) cout<<i<<" "<<f[i]<<endl;
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N = 100;
static const int INF = (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] = INF;
	}
	d[s] = 0;
	int u;
	while (!q.empty()) {
		u = q.front();
		q.pop();
		for (int v = 0; v < n; v++) {
			if (M[u][v] == 1 && d[v] == INF) {
				d[v] = d[u] + 1;
				q.push(v);
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i+1 << " " << d[i] << endl;
	}
}
int main(int argc, char *argv[]) {
	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 <string>
#include <vector>
#include <queue>

using namespace std;

template<typename ISTREAM>
void readGraphList(ISTREAM& cin, vector< vector<int> >& graphList) {
	size_t n;
	cin >> n;
	graphList = vector< vector<int> >(n);
	for (size_t i = 0; i != n; ++i) {
		size_t u;
		size_t k;
		cin >> u;
		cin >> k;
		vector<int>& v = graphList.at(u - 1);
		v.resize(k);
		for (size_t j = 0; j != k; ++j) {
			cin >> v.at(j);
			v.at(j)--;
		}
	}
}
#if 1
void widthSearch(const vector<vector<int>>& graphList) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	vector<int> distanceList(n, -1);
	distanceList.front() = 0;
	nodeQueue.push(0);
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		const int distanceOfNextNode = distanceList.at(id) + 1;
		const vector<int>& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			int& nextNodeDistance = distanceList.at(adjID);
			if (nextNodeDistance != -1)continue;
			nodeQueue.push(adjID);
			nextNodeDistance = distanceOfNextNode;
		}
		nodeQueue.pop();
	}
	for (int i = 0; i != n; ++i) {
		cout << i + 1 << " " << distanceList.at(i) << endl;
	}
}
#endif

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphList(cin, graphList);
	widthSearch(graphList);
}

int main()
{
	execute(cin);

	return 0;
}