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 <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const int INF = 1e9;
const int MOD = 1e9+7;
using Graph = vector<vector<ll>>;
template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;}

// 0 false, 1 true 
// string to int : -48
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

int main() {
    std::cout << std::fixed << std::setprecision(15);
    ll n;
    cin >> n;
    vector<vector<ll>> g(n);
    rep(i, n){
        ll num;
        cin >> num;
        num--;
        ll edgenum;
        cin >> edgenum;
        rep(j,edgenum){
            ll next;
            cin >> next;
            next--;
            g[num].push_back(next);
        }
    }

    queue<ll> que;
    que.push(0);
    vector<ll> ans(n, -1);
    ans[0] = 0;
    while(!que.empty()){
        ll now = que.front();
        ll dist = ans[now];
        que.pop();
        for(ll i = 0; i < g[now].size(); i++){
            if(ans[g[now][i]] != -1){
                continue;
            }

            ans[g[now][i]] = ans[now] + 1;
            que.push(g[now][i]);

        }
    }

    rep(i,g.size()){
        cout << i+1 << " " << ans[i] << endl;
    }



}

C++: #include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ll long long
#define ui unsigned int
#define ull unsigned long long

int main(void)
{
    int n;
    while (cin >> n)
    {
        int adj[105][105]={{0}};
        int tmpn = n;
        while (tmpn--)
        {
            int u, k;
            cin >> u >> k;
            for (int i = 0 ; i < k ; i++)
            {
                int tmp;
                cin >> tmp;
                adj[u-1][tmp-1] = 1;
            }
        }

        int dis[105]={0};
        queue< pair<int, int> > g;
        g.push( make_pair(1,0) );
        while (!g.empty())
        {
            for (int i = 0 ; i < n ; i++)
            {
                if (adj[g.front().first-1][i] == 1 && dis[i] == 0)
                {
                    g.push( make_pair(i+1, g.front().second+1) );
                    dis[i] = g.front().second+1;
                }
            }
            g.pop();
        }

        for (int i = 0 ; i < n ; i++)
        {
            cout << i+1 << " " << dis[i] << endl;
        }
    }

    return 0;
}

C++: //push(),pop():C++ではstackと統一されいる
//front():先頭の要素が何かを返す
//back():末尾の要素が何であるかを返す
//empty():キューが空であるかどうかを調べる
//size():要素数を調べる
#include <iostream>
#include <queue>
using namespace std;
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
static const int INFTY = (1<<21);

int G[MAX][MAX],color[MAX],d[MAX],num;

void bfs(int s){
  int u;
  queue<int> Q; //Qを使うことでエンキュー、デキューの操作が容易になる
  for(int i=1; i<=num; i++) {
    d[i] = INFTY;
  }
  Q.push(s);
  d[s] = 0;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();
    for(int v=1; v<=num; v++){
      if(G[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}


int main(){
  int i,j,u,k,v;

  scanf("%d",&num);
  for(i=1; i<=num; i++){
    color[i] = WHITE;
    for(j=1; j<=num; j++){
      G[i][j] = 0;
    }
  }
  for(i=1; i<=num; i++){
    scanf("%d%d",&u,&k);
    for(j=1; j<=k; j++){
      scanf("%d",&v);
      G[u][v] = 1;
    }
  }

  bfs(1);

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

  return 0;
}

C++: #include <cstdio>

void search(int (*A)[101], int *dis, int n){
    int hoge;
    int time=0;
    for(int k=1;k<n;k++){
        hoge = 1;
        for(int i=1;i<=n;i++){
            if(dis[i]==time){
                for(int j=1;j<=n;j++){
                    if(A[i][j]&&dis[j]<0){
                        dis[j]=time+1;
                        hoge=0;
                    }
                }
            }
        }
        time++;
        if(hoge)return;
    }
}

int main(){
    int i, j, n, u, k, v;
    scanf("%d",&n);
    int dis[n+1];
    dis[1]=0;
    for(i=2;i<=n;i++){
        dis[i]=-1;
    }
    int A[101][101];
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            A[i][j]=0;
        }
    }
    for(i=1;i<=n;i++){
        scanf("%d %d",&u,&k);
        for(j=0;j<k;j++){
            scanf("%d",&v);
            A[u][v]=1;
        }
    }
    search(A,dis,n);
    for(i=1;i<=n;i++)printf("%d %d\n",i,dis[i]);
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

double distanceAB(double xa, double ya, double xb, double yb);
//void trace(int A[], int N);

/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/


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);
	REP(n) 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);
		}
	}
	REP(n)
	{
		cout << i + 1 << " " << ((d[i] == INFTY) ? (-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(j, k)
		{
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}
	
	bfs(0);
	
	return 0;
}


/*
void trace(int A[], int N)
{
	REP(N)
	{
		if (i > 0) cout << " ";
		cout << A[i];
	}
	cout << endl;
}
*/


double distanceAB(double xa, double ya, double xb, double yb)
{
	return sqrt((xb-xa)*(xb-xa) + (yb-ya)*(yb-ya));
}

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


//使用队列实现广度优先搜索

using namespace std;

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

/**
 * M 邻接矩阵
 * queue 记录下一个待访问顶点的队列
 * 将七点s到各顶点的最短距离记录在d[i]中
 */

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?d[i]:(-1))<<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;
}