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 <algorithm>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <list>
#include <bitset>

using namespace std;
typedef long long ll;

// #define DEBUG

int main()
{
#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, u, k, v, t, x;
    cin >> n;
    int d[n];

    vector<int> c[n];

    for (int i = 0; i < n; i++)
    {
        d[i] = 1000;
        cin >> u >> k;
        for (int j = 0; j < k; j++)
        {
            cin >> v;
            c[i].push_back(v - 1);
        }
    }
    d[0] = 0;
    t = 0;
    vector<int> search_list;
    for (auto itr = c[0].begin(); itr != c[0].end(); itr++)
    {
        search_list.push_back(*itr);
    }
    while (!search_list.empty())
    {
        t++;
        vector<int> new_search_list;
        for (auto itr = search_list.begin(); itr != search_list.end(); itr++)
        {
            x = *itr;
            if (d[x] == 1000)
            {
                d[x] = t;
                for (auto itr2 = c[x].begin(); itr2 != c[x].end(); itr2++)
                {
                    new_search_list.push_back(*itr2);
                }
            }
        }
        search_list.clear();
        for (auto itr = new_search_list.begin(); itr != new_search_list.end(); itr++)
        {
            search_list.push_back(*itr);
        }
    }

    for (int i = 0; i < n; i++)
        cout << i + 1 << " " << ((d[i] == 1000) ? -1 : d[i]) << endl;
    return 0;
}

// getline(cin,s);
// a = strtok(s, ",");  ","で分解
// for (int i = 0; i < n; i++)

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

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

int d[MAX_NODE];
vector<int> adj[MAX_NODE];

int bfs(int n, int start){
	queue<int> que;
	for(int i=0;i<n;i++){
		d[i]=(i==start)?0:-1;
	}
	que.push(start);
	while(que.empty()==0){
		int from=que.front();
		que.pop();
		for(int i=0;i<adj[from].size();i++){
			int to=adj[from][i];
			if(d[to]==-1){
				d[to]=d[from]+1;
				que.push(to);
			}
		}
	}
}	

int main(){
	int n,u,k,v;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>u>>k;
		for(int j=0;j<k;j++){
			cin>>v;
			v--;
			adj[u-1].push_back(v);
		}
	}
	cout<<adj[0].size()<<endl;
	bfs(n,0);
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<d[i]<<endl;
	}
	return 0;
}

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

static const int MAX_NUM = 101;
static const int NO_VISIT  = 0;
static const int END_VISIT = 2;

void BFS(int *matrix, int num)
{
    int i;
    int state[MAX_NUM]    = {0};
    int distance[MAX_NUM];
    queue<int> Q;
    int target;
    int dist = 0;
    int *tmp;
    bool first;

    for (i = 1; i <= num; i++) {
        distance[i] = -1;
    }

    Q.push(1);
    state[1] = END_VISIT;
    distance[1] = 0;

    while (Q.empty() == false) {
        target = Q.front();
        Q.pop();
        tmp = matrix + target * MAX_NUM;
        first = true;
        for (i = 1; i <= num; i++) {
            if ((*(tmp + i) == 1) && (state[i] == NO_VISIT)) {
                distance[i] = distance[target] + 1;
                Q.push(i);
                state[i] = END_VISIT;
            }
        }
    }

    for (i = 1; i <= num; i++) {
        printf("%d %d\n", i, distance[i]);
    }

    return;
}

int main(void)
{
    int i, j;
    int num;
    int elem;
    int nume;
    int tmp;
    int matrix[MAX_NUM][MAX_NUM];

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        scanf("%d %d", &elem, &nume);
        for (j = 0; j < nume; j++) {
            scanf("%d", &tmp);
            matrix[elem][tmp] = 1;
        }
    }

    BFS(&matrix[0][0], num);

    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
using namespace std;

const int N = 100;
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] == 0) continue;
            if (d[v] != INF) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for(int i = 0; i < n; i++) {
        cout << i+1 << " "  << ( (d[i] == INF) ? (-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 INF=(1<<21);
int n, MA[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(MA[u][v]==0)
        continue;
      if(d[v] != INF)
        continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }
  for(int i=0; i<n; i++)
  {
    cout << i+1 << " " <<((d[i] == INF) ? (-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++)
      MA[i][j]=0;
  }

  for(int i=0; i<n; i++)
  {
    cin >> u >> k;
    u--;
    for(int j=0; j<k; j++)
    {
      cin >> v;
      v--;
      MA[u][v]=1;
    }
  }

  Bfs(0);

  return 0;
}