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 <iostream>
#include <queue>
using namespace std;
#define INF 9999999

int graph[101][101];
int dis[101]; //距離を格納
bool check[101];//訪問済みチェック
int main(){
  int n;
  cin>>n;
  fill(dis,dis+101,INF);
  dis[1]=0;
  check[1]=true;
  for(int i=0;i<n;i++){
    int u,k,v;
    cin>>u>>k;
    for(int j=1;j<=k;j++){
      graph[u][0]=k;
      cin>>graph[u][j];
    }
  }
  queue<int> qu;
  qu.push(1);
  while(!qu.empty()){
    int num=qu.front(); qu.pop();
    for(int j=1;j<=graph[num][0];j++){
      int temp=graph[num][j];
      if(check[temp]){//訪問済みならば
        continue;
      }else{
        check[temp]=true;
      }
      dis[temp]=dis[num]+1;
      qu.push(temp);
    }
  }
  //表示部
  for(int i=1;i<=n;i++){
    cout<<i<<" ";
    if(dis[i]>=INF)cout<<-1<<"\n";
    else  cout<<dis[i]<<"\n";
  }
}

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

int D[101]; 
int edges[101][101];

void bfs(int src,int n)
{
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while(!Q.empty())
	{
		int cur=Q.front();
		Q.pop();
		for(int dst=1;dst<=n;dst++)
		{
			int flag=0;
			for(int i=0;i<n;i++)
			{
				if(edges[cur][i]==dst)
				{
					flag=1;
					break;
				}
			}
			if(D[dst]==-1&&flag)
			{
				D[dst]=D[cur]+1;
				Q.push(dst);
			}
		}
	}
}

int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++)
	{
		int u,k;
		cin >> u >> k;
		for(int j=0;j<k;j++)
		   cin >> edges[u][j];
	}
	for(int i=1;i<=n;i++)
	   D[i]=-1;
	bfs(1,n);
	for(int i=1;i<=n;i++)
	   cout << i << ' ' << D[i] << endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <cstdint>

using namespace std;

enum COLOR {
  WHITE,
  GRAY,
  BLACK,
};

static const int INFTY = INT_MAX;
static const int N = 100;

static int M[N][N] = {};
static vector<COLOR> color(N, WHITE);
static vector<int> d(N, INFTY);

static void bfs(int n, int s) {
  color[s] = GRAY;
  d[s] = 0;

  queue<int> Q;
  Q.push(s);

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int v = 0; v < n; v++) {
      if (M[u][v] && color[v] == WHITE) {
          color[v] = GRAY;
          d[v] = d[u] + 1;
          Q.push(v);
      }
      color[u] = BLACK;
    }
  }
}

int main() {
  int n;
  cin >> n;

  int number, count, value;
  for (int i = 0; i < n; i++) {
    cin >> number >> count;
    number--;
    for (int j = 0; j < count; j++) {
     cin >> value;
     value--;
     M[number][value] = 1; 
    }
  }
  
  bfs(n, 0);
  
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? -1 : d[i]) << endl; 
  }

  return 0;
}

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

#define INF 100000000
int n;
int Graph[100][100] = {};

int color[100] = {}, d[100];

void bfs(int x){
  int u;
  queue<int> Q; //queueを生成
  Q.push(x); //始点をキューに積む
  for(int i = 0 ; i < n ; i++){
    d[i] = INF;
  }

  d[x] = 0; //始点を0にする
  
  while(!Q.empty()){
    u = Q.front(); //取り出して削除
    Q.pop();
    for(int v = 0 ; v < n ; v++){
      if(Graph[u][v] == 0) continue;
      if(d[v] != INF) continue;
      d[v] = d[u] + 1;
      Q.push(v); //次点を積む
    }
  }

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


int main(){
  int u, k, v; //u = 頂点番号 k = 出次数 v = u に隣接する頂点番号

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

  bfs(0);
     
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>


class Graph {
  public:
    Graph(int n) : n(n) {
        adj_arr = std::vector<std::vector<int>>(n);
        depth = std::vector<int>(n, 0);
    }

    void breadth_search(int i, int d) {
        std::queue<int> queue;
        depth[i] = d;
        
        for (auto && v : adj_arr[i]) {
            if (depth[v - 1] == 0 || depth[v - 1] > d) {
                queue.push(v - 1);
            }
        }

        while (!queue.empty()) {
            int v = queue.front();
            queue.pop();
            breadth_search(v, d + 1);
        }
    }

    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i + 1 << " " << depth[i] << std::endl;
        }
    }

  public:
    std::vector<std::vector<int>> adj_arr;
    std::vector<int> depth;
    
  private:
    int n;
};


int main () {
    int n;
    std::cin >> n;

    Graph graph(n);

    for (int i = 0; i < n; i++) {
        int u, k;
        std::cin >> u >> k;
        std::vector<int> arr(k);
        
        for (int j = 0; j < k; j++) {
            std::cin >> arr[j];
        }
        graph.adj_arr[u - 1] = arr;
    }

    graph.breadth_search(0, 0);
    graph.print();
}

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

using namespace std;

int main(){
        vector<int> v[1005];
        bool used[1005];
        int dis[1005];
        memset(used, 0, sizeof(used));
        memset(dis, -1, sizeof(dis));
        int N ; cin >> N;
        for(int j = 0; j < N; j++){
            int id, t, tmp; cin >> id >> t;
            for (int i = 0; i < t; i++){
                cin >> tmp;
                v[id].push_back(tmp);
                //v[tmp].push_back(id);
            }
        }
        queue<int> q;
        q.push(1);
        used[1] = 1;
        dis[1] = 0;
        while(!q.empty()){
            int k = q.front();
            q.pop();
            for (int x : v[k]){
                if(!used[x]){
                    q.push(x);
                    dis[x] = dis[k] + 1;
                    used[x] = 1;
                }
            }
        }
        for (int i = 1; i <= N; i++)
            cout << i << " " << dis[i] << '\n';

}

C++: #include <iostream>
#include <vector>
#include <queue>

using Graph = std::vector<std::vector<int>>;

int main(){
    int n;
    std::cin >> n;

    Graph g(n);
    for(int i = 0; i < n; ++i){
        int u, k;
        std::cin >> u >> k;
        --u;
        for(int j = 0; j < k; ++j){
            int v;
            std::cin >> v;
            --v;
            g.at(u).push_back(v);            
            //g.at(v).push_back(u); 
        }
    }

    std::vector<int> dist(n, -1);
    std::queue<int> queue;
    dist.at(0) = 0;
    queue.push(0);

    while(!queue.empty()){
        int v = queue.front();
        queue.pop();

        for(auto nextV : g.at(v)){
//            if(-1 != dist.at(nextV)){
//                continue;
//            }
//            queue.push(nextV);
//            dist.at(nextV) = dist.at(v) + 1;
            if(-1 == dist.at(nextV)){
                queue.push(nextV);
                dist.at(nextV) = dist.at(v) + 1;
            }
        }
    }

    for(int i = 0; i < n; ++i){
        std::cout << i + 1 << " " << dist.at(i) << std::endl;
    }

    return 0;
}