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>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define all(x) (x).begin(), (x).end()
using i32 = int32_t;
using i64 = int64_t;
using f32 = float;
using f64 = double;
using P   = pair<int, int>;

template <class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template <class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void dump_vec(const vector<T>& v) {
    auto len = v.size();
    rep(i, 0, len) {
        cout << v[i] << (i == (int)len - 1 ? "\n" : " ");
    }
}

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} FASTIO;

//---------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------

signed main() {
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    rep(i, 0, N) {
        int id, k;
        cin >> id >> k;
        id--;
        rep(j, 0, k) {
            int u;
            cin >> u;
            u--;
            G[id].push_back(u);
        }
    }

    const auto inf = 1 << 30;
    vector<int> dist(N, inf);
    dist[0] = 0;
    queue<P> q;
    q.push(make_pair(0, 0));
    while (!q.empty()) {
        auto pi = q.front();
        q.pop();
        auto u = pi.first, cost = pi.second;
        for (auto& to : G[u]) {
            if (chmin(dist[to], cost + 1)) {
                q.push(make_pair(to, dist[to]));
            }
        }
    }

    rep(i, 0, N) {
        cout << i + 1 << " " << (dist[i] == inf ? -1 : dist[i]) << "\n";
    }
}

C++: //
//
//  algoC.cpp - created by us162022
//
#include <iostream>
#include <queue>
using namespace std;
struct Node{
  int u,k,v[99];
  int distance;
};

int main(int argc, char *argv[])
{
  int n;
  Node node[100];
  queue<Node> que;

  cin >> n;

  for(int i=0; i<n; i++){
    cin >> node[i].u >> node[i].k;

    for(int j=0; j<node[i].k; j++)
      cin >> node[i].v[j];

    node[i].distance=-1;
  }

  node[0].distance = 0;
  que.push(node[0]);

  while(!que.empty()){
    Node fro = que.front();

    for(int i=0; i<fro.k; i++){
      int x = fro.v[i]-1;
  
      if(node[x].distance==-1)
	    node[x].distance = fro.distance+1;

      que.push(node[x]);
    }

    que.pop();
  }

  for(int i=0; i<n; i++)
    cout << node[i].u << " " << node[i].distance << endl;

}

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

int A[100][100];//隣接行列
int n;
int D[100];  //訪問状態を保持　距離で

void bfs(int s) {
	queue<int> Q;
	Q.push(s);
	D[s] = 0; 
	while (! Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int d=0; d<n; ++d) {
			if (A[cur][d] == 1 && D[d] == -1) {
				D[d] = D[cur]+1;
				Q.push(d);
			}
		}
	}
}

int main() {
	cin >> n;
	int p,q,r;
	for (int i=0; i<n; ++i) {
		cin >> p >> q;
		D[i] = -1; //到達不可能の時-1
		for (int i=0; i<q; ++i) {
			cin >> r;
			A[p-1][r-1] = 1;
		}
	}
	bfs(0);
	for (int i=0; i<n; ++i) {
		cout << i+1 << " " << D[i] << endl;
	}
    return 0;
}

C++: #include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int main(void)
{
	int i,u,j,n1,c1;
	int n,k[101],v[101][100],c[101];
	int q[100],nn[100],p1,p2;
	scanf("%d",&n);
	for(i=1;i<=n;i++)	{
		scanf("%d %d",&u,&k[i]);
		for(j=0;j<k[i];j++)	{
			scanf("%d",&v[i][j]);
		}
	}
	for(i=1;i<=n;i++)	c[i]=-1;
	c[1]=0;
	p1=0;	p2=0;
	q[p1]=1;	nn[p1]=0;
	p1++;
	c[1]=0;
	while(p1!=p2)	{
		n1=q[p2];	c1=nn[p2];
		p2++;
//		printf("n1=%d c1=%d\n",n1,c1);
		for(i=0;i<k[n1];i++)	{
//			printf("v=%d\n",v[n1][i]);
			if(c[v[n1][i]]==-1)	{
				q[p1]=v[n1][i];
				nn[p1]=c1+1;
//				printf("q=%d nn=%d\n",v[n1][i],c1+1);
				p1++;
				c[v[n1][i]]=c1+1;
			}
		}
	}
	for(i=1;i<=n;i++)	printf("%d %d\n",i,c[i]);
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#define whi 0
#define gra 1
#define bla 2
using namespace std;
int n,d[101],color[101];
bool g[101][101];
queue<int> num;
void dft(){
	int a,i,j;
	for(i=1;i<=n;i++){
	color[i]=whi;
		d[i]=-1;
	}
	num.push(1);
	d[1]=0;
	color[1]=gra;
	while(!num.empty()){
		a=num.front();
		num.pop();
		for(int v=1;v<=n;v++){
			if(g[a][v] && color[v]==whi){
				num.push(v);
				color[v]=gra;
				d[v]=d[a]+1;
			}
			color[a]=bla;
		}
	}	
}
main(){
	int u,k,v,i,j;
	cin>>n;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++)
		g[i][v]=false;
	}
	for(i=1;i<=n;i++){
	cin>>u>>k;
		for(j=1;j<=k;j++){
		cin>>v;
			g[u][v]=true;
		}
	}
	dft();
	for(i=1;i<=n;i++)
	cout<<i<<" "<<d[i]<<endl;
	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;
            cout <<v<<" "<<d[v];
            d[v] = d[u] + 1;
            cout <<"->"<< d[v] << endl;
            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, 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;
}