Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

const int INF = -1;

int G[110][110] = {};
int d[110] = {};

void bfs(int s, int n)
{
  int v;
  queue<int> que;

  for(int i = 1;i <= n;i++) d[i] = INF; //init

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

    for(int i = 1;i <= n;i++){
      if(G[v][i] != 1) continue;
      if(d[i] != INF) continue;

      que.push(i);
      d[i] = d[v]+1;
    }
  }
}

int main()
{
  int n;
  int i,j;
  int u,k,v;

  scanf("%d", &n);

  for(i = 0;i < n;i++){
    scanf("%d%d", &u, &k);

    for(j = 0;j < k;j++){
      scanf("%d", &v);
      G[u][v] = 1;
    }
  }

  /*for(i = 1;i <= n;i++)
    if(d[i] == 0) bfs(i, n);
  */
  bfs(1, n);
  
  for(i = 1;i <= n;i++)
    printf("%d %d\n", i, d[i]);

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <cstdlib>

const int MAX_N = 100;

struct Node {
  int v;
  int d;
  bool visited;
};

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

  int n = 0;
  std::cin >> n;

  std::vector<std::vector<int> *> *graph = new std::vector<std::vector<int> *>(n + 1);
  std::vector<struct Node *> *nodes = new std::vector<struct Node *>(n + 1);
  
  for (int i = 1; i <= n; i++) {
    int v = 0;
    std::cin >> v;

    int k = 0;
    std::cin >> k;

    std::vector<int> *neighbors = new std::vector<int>(k);

    for (int i = 0; i < k; i++) {
      int a = 0;
      std::cin >> a;

      neighbors->at(i) = a;
    }

    graph->at(i) = neighbors;

    struct Node *node = (struct Node *) std::malloc(sizeof(struct Node));

    node->v = i;
    node->d = -1;
    node->visited = false;
    
    nodes->at(i) = node;
  }

  int count = 0;
  int start = 1;

  //  nodes->at(start)->d = 1;

  std::list<int> *stack = new std::list<int>();

  stack->push_back(nodes->at(start)->v);

  while (!stack->empty()) {
    std::vector<int> *newNodes = new std::vector<int>();

    while (!stack->empty()) {    
      int v = stack->front();
      stack->pop_front();

      nodes->at(v)->d = count;
      nodes->at(v)->visited = true;
      
      std::vector<int> *neighbors = graph->at(v);

      for (std::vector<int>::iterator iterator = neighbors->begin(); iterator != neighbors->end(); ++iterator) {
	struct Node *node = nodes->at(*iterator);

	if (!node->visited) {
	  newNodes->push_back(node->v);
	}
      }
    }

    for (std::vector<int>::iterator iterator = newNodes->begin(); iterator != newNodes->end(); ++iterator) {
      stack->push_front(*iterator);
    }

    count++;
  }

  for (std::vector<struct Node *>::iterator iterator = nodes->begin() + 1; iterator != nodes->end(); ++iterator) {
    struct Node *node = *iterator;
        
    std::cout << node->v << " " << node->d << std::endl;
  }
  
  return 0;
}

C++: //push(),pop():C++ではstackと統一されいる
//front():先頭の要素が何かを返す
//back():末尾の要素が何であるかを返す
//empty():キューが空であるかどうかを調べる
//size():要素数を調べる
#include<stdio.h>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
static const int INFTY = (1<<21);

int G[MAX][MAX],color[MAX],d[MAX],num;

void bfs(int s){
  int u;
  queue<int> Q; //Qを使うことでエンキュー、デキューの操作が容易になる
  for(int i=1; i<=num; i++) {
    d[i] = INFTY;
  }
  Q.push(s);
  d[s] = 0;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();
    for(int v=1; v<=num; v++){
      if(G[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}


int main(){
  int i,j,u,k,v;

  scanf("%d",&num);
  for(i=1; i<=num; i++){
    color[i] = WHITE;
    for(j=1; j<=num; j++){
      G[i][j] = 0;
    }
  }
  for(i=1; i<=num; i++){
    scanf("%d%d",&u,&k);
    for(j=1; j<=k; j++){
      scanf("%d",&v);
      G[u][v] = 1;
    }
  }

  bfs(1);

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

  return 0;
}

C++: #include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;
#define MAX 105
vector<int> V[MAX];

int d[MAX];

void search(int v, int t) {
	if (d[v] == -1 || d[v] > t) d[v] = t;
	else return;

	for (int i = 0; i < V[v].size(); i++) {
		search(V[v][i], t + 1);
	}
}

int main() {


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

		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int s;
			cin >> s;
			V[u].push_back(s);
		}
	}

	search(1, 0);

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

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
int n;
int v,k,ttm;
vector<int> graph[105];
int dis[105];//到其他点的距离
int vis[105];//纪律顶点是否被访问
void bfs(int u)
{
    queue<int> qu;
    qu.push(u);
    dis[u]=0;
    vis[u]=1;
    while(!qu.empty())
    {
        int v=qu.front();qu.pop();
        for(int i:graph[v])
        {
            if(dis[i]||i==1){continue;}
            qu.push(i);
            dis[i]=dis[v]+1;
        }
    }
}
int main()
{
    clr(vis,0);
    cin>>n;
    for(int i=0;i<n;++i)
    {
        cin>>v>>k;
        while(k--)
        {
            cin>>ttm;
            graph[v].push_back(ttm);//连接的点
        }
    }
    bfs(1);
     cout<<1<<" "<<dis[1]<<endl;
    for(int i=2;i<=n;++i)
    {
         cout<<i<<" "<<(dis[i]?dis[i]:-1)<<endl;
    }
    return 0;
}

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

#define MAX 105
#define INFTY 10000000

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

void bfs(int s){
    //最短距離を初期化
    for(int u=1; u<=n; u++){
        d[u] = INFTY;
    }
    queue<int> Q;
    d[s] = 0;
    Q.push(s);

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

    //出力
    for(int u=1; u<=n; u++){
        if(d[u] == INFTY) printf("%d -1\n", u);
        else printf("%d %d\n", u, d[u]);
    }
}

//グラフの幅優先探索(stackを用いる)
int main(){
    cin >> n;
    //隣接リスト表現→隣接行列表現への変換
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            M[i][j] = 0;
        }
    }
    for(int i=1; i<=n; i++){
        int u, k;
        cin >> u >> k;
        for(int j=1; j<=k; j++){
            int v;
            cin >> v;
            M[u][v] = 1;
        }
    }

    bfs(1);

    return 0;
}