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>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)

using namespace std;

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


int n, M[N][N], d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  REP(i, n) d[i] = INFTY;
  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] != INFTY ) continue;
      d[v] = d[u] + 1;
      q.push( v );
    }
  }
  REP(i, 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;
}

C++: # include <iostream>
# include <vector>
# include <cstdio>
# include <queue>
using namespace std;
struct info
{
	int d = -1;
	bool vis = false;
};
vector<int> a[105];
info *ans;
queue<int> solve;
void bfs()
{
	solve.push(1); 
	ans[1].vis = true; ans[1].d = 0;
	while(!solve.empty())
	{
		int u = solve.front(); solve.pop();
		for(int j = 0;j < a[u].size();j++)
		{
			if(!ans[a[u][j]].vis)
			{
				ans[a[u][j]].vis = true;
				ans[a[u][j]].d = ans[u].d + 1;
				solve.push(a[u][j]);
			}
		}
	}
	
}
int main(void)
{
	int n,cnt,k,u;
	scanf("%d",&n);
	ans = new info [n+1];
	for(int w = 0;w < n;w++)
	{
		scanf("%d %d",&u,&cnt);
		for(int i = 0;i < cnt;i++)
		{
			scanf("%d",&k);
			a[u].push_back(k);
		}
	}
	bfs();
	for(int i = 1;i <= n;i++) printf("%d %d\n",i,ans[i].d);
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#define MAX 101
using namespace std;

int n;
int a[MAX][MAX]={};
int d[MAX];

void track(int v,int depth){
  if(d[v]>depth||d[v]==-1) d[v]=depth;
  for(int i=1;i<=n;i++){
    if(a[v][i]==1){
      track(i,depth+1);
    }
  }
}

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

  for(int i=1;i<=n;i++){
    d[i]=-1;
  }

  track(1,0);

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

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 100 + 4;
const int INF = 1000000;
int M[MAXN][MAXN];
int d[MAXN];

int main()
{
    int N, u, k, m;
    memset ( M, 0, sizeof(M) );
    d[1] = 0;
    cin>>N;
    for ( int i = 0; i < N; i++ ) {
        cin>>u>>k;
        for ( int j = 0; j < k; j++ ){
            cin>>m;
            M[u][m] = 1;
        }
    }


    queue<int> q;
    q.push(1);
    for ( int i = 0; i <= N; i++ ) d[i] = INF;
    d[1] = 0;
    while ( !q.empty() ) {
        int u = q.front(); q.pop();
        for ( int v = 1; 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 = 1; i <= N; i++ ) {
        cout<<i<<' '<<( d[i] == INF ? -1:d[i] )<<endl;
    }
    return 0;
}

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

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

int main() {
	for ( int i = 0; i < 100; i++ )
		b[i] = - 1;
	cin >> n;
	for ( int i = 0; i < n; i++ ) {
		cin >> u >> k;
		for ( int 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 <string>
#include <stdio.h>
#include <queue>
using namespace std;

int d[100];
int f[100];
int map[100][100];
int dis;
int n;
queue<int> q;
void visit(int);
void searchQ(int);

int main(){
	dis=0;
	int i,j,k,u,v;
	for(i=0;i<100;i++) d[i]=-1; //?????¢??¨?????¢??\
	cin >> n;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			map[i][j]=0;
		}
	}
	for(i=0;i<n;i++){
		cin>>u>>k;
		for(j=0;j<k;j++){
			cin>>v;
			map[u-1][v-1]=1; //?????????????????????????????????????????????
		}
	}
	visit(0);
	for(i=0;i<n;i++) cout<< i+1 <<" "<<d[i]<<endl;
	return 0;
}

void visit(int a){
	q.push(a);
	d[a]=0;

	while(!q.empty()){
		int i=q.front();
		q.pop();
		searchQ(i);
		
	}
}

void searchQ(int a){//?????\????????¶?????????
	for(int i=0;i<n;i++){
		if(map[a][i]==1 && d[i]==-1){
			q.push(i);
			d[i]=d[a]+1;
		}
	}
}

C++: #include "taichitary.h"
#define V_MAX 100

int m[V_MAX][V_MAX];
int cnt[V_MAX];
int count=0;
int n;

void bfs(int u){

    queue<int> Q;
    Q.push(u);
    cnt[u] = 0;
    int v = Q.front();

    while(!Q.empty()){

        for(int i=0; i<n; i++){
            if(m[v][i] == 1 && cnt[i] == -1){
                Q.push(i);
                cnt[i] = cnt[v]+1;
            }
        }

        Q.pop();
        v = Q.front();
    }
}


int main(){

    cin >> n;

    int from, deg;

    for(int i=0; i<n; i++){
        cin >> from >> deg;
        from--;
        for(int j=0; j<deg; j++){
            int tmp;
            cin >> tmp;
            tmp--;
            m[from][tmp] = 1;
        }
    }

    for(int k=0; k<n; k++) cnt[k] = -1; //?????????

    bfs(0);

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


}

C++: from collections import deque
n = int(input())
A = [0]*(n+1)
D = [-1]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
d.append(1)
D[1] = 0
DP[1] = 1
while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i)
for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp)

C++: //Breadth First Search
#if 0
?????¨?????????30??????
??????????????¨??????????????¢?´¢????????¬????????¨??¢?????´????????????????£?
#endif
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n;
int D[100];
int matrix[100][100];
void bfs(int src)
{
    //cerr<<"bfs root = "<<src<<endl;
    queue<int> q;
    q.push(src);
    D[src]=0;
    while(!q.empty())
    {
        int cur=q.front();
        q.pop();
        //cerr<<"visiting "<<cur<<' '<<D[cur]<<endl;
        for(int dst=0;dst<n;dst++)
        {
            if(matrix[cur][dst]==1&&D[dst]==-1)
            {
                D[dst]=D[cur]+1;
                q.push(dst);
            }
        }
    }
};

int main()
{
    vector<int> l[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            int tmp;
            cin>>tmp;
            l[u].push_back(tmp);
        }
    }
    
    for(int i=0;i<100;i++)
    {
        D[i]=-1;
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            matrix[i][j]=0;
        }
    }
    
    for(int i=1;i<n+1;i++)
    {
        for(int j=0;j<l[i].size();j++)
        {
            matrix[i-1][l[i][j]-1]=1;
        }
    }
    
    bfs(0);
    
    for(int i=0;i<n;i++)
    {
        cout<<(i+1)<<' '<<D[i]<<endl;
    }
}