Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

const (
	N   = 100
	INF = 1 << 21
)

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

C++: #include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>

using namespace std;
int ans[100][100] = {};
int now = 0, n;

int main() {
    int u, k, tmp;
    cin >> n;
    vector<int> dist(n, -1);
    queue<int> que;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            cin >> tmp;
            ans[u - 1][tmp - 1] = 1;
        }
    }
/*
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; ++j) {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
*/
    dist[0] = 0;
    que.push(0);
    while (!que.empty()) {
        int v = que.front();
        //cout << v;
        que.pop();

        for (int i = 0; i < n; ++i) {
            int nv = ans[v][i];
            if (dist[i] == -1 and nv != 0) {
                dist[i] = dist[v] + 1;
                que.push(i);
            }
        }
        //cout << endl;
    }
    for (int i = 0; i < n; ++i) {
        cout << i + 1 << " " << dist[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];  // 距離で訪問状態（color）を管理する

void bfs(int s) {
  queue<int> q;  // 標準ライブラリの queue を使用
  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;
int vis[111],G[111][111],cn,n,d[111];
int  bfs(int i){
  	queue<int>q;
  	q.push(i);
  	vis[i]=1;
	while(!q.empty()){
		int temp=q.front();
		vis[temp]=1;
		q.pop();
		for(int j=1;j<=n;j++){
			if(vis[j]==0&&G[temp][j]!=0){
				d[j]=d[temp]+1;
				vis[j]=1;
				q.push(j);
			}
		}
	}  
}

int main(){
    scanf("%d",&n);
	for(int i=1;i<=n;i++){	
	int v2,v1,k;
		scanf("%d%d",&v1,&k);
		for(int j=0;j<k;j++){
			scanf("%d",&v2);
			G[v1][v2]=1;
		}
}
    bfs(1);
    for(int i=1;i<=n;i++) {
    	if(i==1)
    	printf("%d %d\n",i,d[i]);
    	else if(d[i]==0){
    			printf("%d %d\n",i,-1);
		} 
		else printf("%d %d\n",i,d[i]);
	}
	 
	 return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<n;i++)

using namespace std;


int main()
{
	int n;
	cin>>n;
	
	bool adj[n+10][n+10]={};
	
	for(int i=1;i<n+1;++i){//1index
		
		int u;
		int k;
		cin>>u>>k;
		
		for(int j=1;j<k+1;++j){//1index
			int v;
			cin>>v;
			adj[i][v]=true;
		}
	}
	
	int d[n+10];
	for(int i=1;i<n+1;++i){
		d[i]=-1;
	}
	
	d[1]=0;
	
	queue<int> que;
	que.push(1);
	while(!que.empty()){
		int now=que.front();
		que.pop();
		
		int a=d[now];
		
		for(int j=1;j<n+1;++j){
			
			if((adj[now][j]) and (d[j]==-1)){
				d[j]=a+1;
			que.push(j);
			
			}
			
		}
	}
	
	for(int i=1;i<n+1;++i){
		cout<<i<<" "<<d[i]<<endl;
	}
	
}

C++: #include <iostream>
#include <queue>
using namespace std;
    queue<int> Q;
int u[1000][1000];
int d[1000],color[1000];
// white 0 gray 1 black 2
void BFS(int s,int n){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            color[i]=0;
            d[i]=-1;
        }
    }
    color[s]=1;
    d[s]=0;
    Q.push(s);
    while(Q.size()!=0){
        int tmp=Q.front();
        Q.pop();
        for(int j=1;j<=n;j++){
            if(color[j]==0 && u[tmp][j]==1){
                color[j]=1;
                d[j]=d[tmp]+1;
                Q.push(j);
            }
        }
        color[tmp]=2;
    }
}
int main(){
    int n,l,k,m;
    cin>>n;
    u[100][100]={};
    for(int i=0;i<n;i++){
        cin>>l>>k;
        for(int j=1;j<=k;j++){
            cin>>m;        
            u[l][m]=1;
        }
    }
    BFS(1,n);
    for(int i=1;i<=n;i++){
        cout<<i<<" "<<d[i]<<endl;
    }
    return 0;
}

C++: // Ki
// 所要時間 25分 (1WA)
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define REP(i, d, n) for(int i=(d); i<(n); ++i)
#define all(v) v.begin(), v.end()

int main() {

    int n;
    cin >> n;
    vector<vector<int> > graph(n);
    rep(i, n){
        int u, k;
        cin >> u >> k;
        --u;
        rep(j, k){
            int v;
            cin >> v;
            graph[u].push_back(--v);
        }
    }

    queue<int> q;
    vector<int> dis(n, -1);
    q.push(0);
    dis[0] = 0;
    while(!q.empty()){
        int v = q.front();
        q.pop();
        rep(i, graph[v].size()){
            int u = graph[v][i];
            if(dis[u] == -1){
                dis[u] = dis[v] + 1;
                q.push(u);
            }
        }
    }

    rep(i, n){
        cout << i+1 << " " << dis[i] << endl;
    }

    return 0;
}

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

static const int MAX_NUM = 101;

void BFS(int matrix[][MAX_NUM], int num, int target, int result[])
{
    queue<int> Q;
    int  distance = 0;
    char state[MAX_NUM] = {0};

    Q.push(target);
    result[target] = distance;
    state[target] = 1;

    while (Q.empty() == false) {
        int index = Q.front();
        Q.pop();

        for (int i = 1; i < num; i++) {
            if ((matrix[index][i] == 1) && (state[i] == 0)) {
                Q.push(i);
                state[i]  = 1;
                result[i] = result[index] + 1;
            }
        }
    }

    return;
}

int main(void)
{
    int num;
    int index, elemNum, tmp;
    int matrix[MAX_NUM][MAX_NUM] = {0};
    int result[MAX_NUM];

    scanf("%d", &num);
    for (int i = 1; i < num + 1; i++) {
        result[i] = -1;
        scanf("%d %d", &index, &elemNum);
        for (int j = 0; j < elemNum; j++) {
            scanf("%d", &tmp);
            matrix[index][tmp] = 1;
        }
    }

    BFS(matrix, num, 1, result);

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

    return 0;
}