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 <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[106][106];
int A[106],f[106],w,h,n,sx,sy,gx,gy,e,g[106];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=1;i<=100;i++){
		for(j=1;j<=100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=1;i<=100;i++){		
		A[i]=0;				
		f[i]=-1;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
	//		printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	f[1]=0;
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			
			g[i]=g[d]+1;
			f[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	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;
}

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

#define MAX 100
int n;
int M[MAX][MAX];
int dist[MAX];

void bfs(int);

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

void bfs(int s){
  queue<int> q;
  int u,v;
  int i;
  q.push(s);
  for(i = 0; i < n; i++){
    dist[i] = INFINITY;
  }
  dist[s] = 0;
  while( !q.empty() ){
    u = q.front();
    q.pop();
    //for(i = 0; i < n; i++){
      for(v = 0; v < n; v++){
        if(M[u][v] == 0){
          continue;
        }
        if( dist[v] != INFINITY){
          continue;
        }
        dist[v] = dist[u] + 1;
        q.push(v);
      }
  }
  for(i = 0; i < n; i++){
    cout << i+1 << " " << ( (dist[i] == INFINITY) ? (-1) : dist[i] ) << endl;
    //if( d[i] == INFINITY)
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define eol '\n'
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  
  queue<int> que;
  vector< vector<int> > list(n+1);
  vector<int> d(n+1, -1);

  for (int i = 0; i < n; i++) {
    int u,k;
    cin >> u >> k;
    
    vector<int> row(k);
    for (int j = 0; j < k; j++) {
      cin >> row[j];
    }
    list[u] = row;
  }

  d[1] = 0;
  que.push(1);
  while(que.size()) {
    int v = que.front();
    que.pop();

    int dis = d[v];

    for (int i = 0; i < list[v].size(); i++) {
      int next = list[v][i];
      if (d[next] == -1) {
        d[next] = dis+1;
        que.push(next);
      }
    }
  }

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

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
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);
  rep(i, n) d[i] = INF;
  d[s] = 0;
  int u;
  while(!q.empty()) {
    u = q.front();
    q.pop();
    rep(v, n) {
      if (!M[u][v]) continue;
      if (d[v] != INF) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  rep(i, n) {
    cout << i+1 << " " << ((d[i] == INF) ? (-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 <bits/stdc++.h>
using namespace std;

typedef long long ll;


int d[200];
bool table[200][200];

void init(){
    for(int i=0;i<200;i++){
        d[i]=-1;
        for(int j=0;j<200;j++){
            table[i][j]=false;
        }
    }
}

void bfs(int itr, int dis, int n){
    d[itr] = dis;
    for(int i=0;i<n;i++){
        if(table[itr][i]&&(d[i]<0||d[i]>dis+1)){
            bfs(i, dis+1,n);
        }
    }
}

void solve(int n){
    bfs(0,0,n);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

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

    solve(n);

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

    return 0;
}

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

int main() {
	int a[101][101] = {{0}};
	int u, k, v, km;
	km = 0;
	while ( cin >> u, k ) {
		for ( int i = 1; i <= k; i++ ) {
			cin >> v;
			a[u][v] = 1;
		}
		km++;
	}
	int n0;
	int b[km + 1] = {- 1};
	queue<int> q;
	q.push( 1 );
	b[1] = 0;
	int cou = 0;
	for ( int i = 1; i <= km; i++ ) {
		n0 = q.front();
		q.pop();
		cou++;
		for ( int j = 1; j <= km + 1; j++ ) {
			if ( a[n0][j] == 1 && b[j] == - 1 ) {
				q.push( j );
				b[j] = cou;
			}
			if ( q.empty ) break;
		}
	}
	for ( int i = 1; i <= km; i++ )
		cout << i << " " << b[i] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
int N,mas[101][101],v[101],cnt=0,st[101];
queue<int> q;

void bfs(int u){
    v[u]++;
    //st[u]=cnt;
    q.push(u);
    while(!q.empty()){
        int n=q.front();
        q.pop();
        for(int i=1;i<N+1;i++){
            if(mas[n][i] && !v[i]){
                v[i]++;
                st[i]=st[n]+1;
                q.push(i);
            }
        }
    }
}

int main(void){
    // Your code here!
    int u,k,vx;
    cin>>N;
    
    for(int i=1;i<N+1;i++){
        cin>>u>>k;
        for(int j=1;j<k+1;j++){
            cin>>vx;
            mas[u][vx]++;
        }
    }
    
    bfs(1);
    for(int i=1;i<N+1;i++){
        if(i>1&& !st[i]) cout<<i<<" "<<"-1"<<endl;       
        else cout<<i<<" "<<st[i]<<endl;  
    } 
}