Go: package main

/*
グラフの表現 隣接行列による表現
*/

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

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;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vi> g(n);
  REP(i, n) {
    int u, k;
    cin >> u >> k;
    u--;
    REP(i, k) {
      int v;
      cin >> v;
      v--;
      g[u].push_back(v);
    }
  }

  queue<pair<int, int>> q;
  vi ans(n, -1);
  q.push(make_pair(0, 0));
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    if (ans[p.first] != -1)
      continue;
    ans[p.first] = p.second;
    for (auto v : g[p.first]) {
      q.push(make_pair(v, ans[p.first] + 1));
    }
  }

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

  return 0;
}