Go: package main

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

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

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

C++: #include<iostream>
#include<queue>
#define rep(i, n) for (int i=0; i<(n); i++)
using namespace std;

static const int N=100;
static const int INF=(1<<12);

int n;
bool M[N][N];
int d[N]; //距離で状態を管理するためcolor不要

void bfs(int s) {
  queue<int> que;
  que.push(s);
  rep(i, n) d[i]=INF;
  d[s]=0;

  while (!que.empty()) {
    int u=que.front();
    que.pop();
    rep(v, n) {
      if (!M[u][v]) continue;
      if (d[v]!=INF) continue;
      d[v]=d[u]+1;
      que.push(v);
    }
  }
}

int main() {
  cin>>n;
  rep(i, n) {
    rep(j, n) {
      M[i][j]=false;
    }
  }
  rep(i, n) {
    int u,k; cin>>u>>k;
    u--;
    rep(i, k) {
      int v; cin>>v;
      v--;
      M[u][v]=true;
    }
  }

  bfs(0);
  rep(i, n) {
      if (d[i]==INF) cout<<i+1<<" "<<-1<<endl;
      else cout<<i+1<<" "<<d[i]<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
//#define INF 1e18
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;

vector<int> E[SIZE];
int d[SIZE];

void bfs(int s){
  queue<int> que;
  que.push(s);
  fill(d,d+SIZE,INF);
  d[s] = 0; 
  while(!que.empty()){
    int v = que.front();que.pop();
    for(int to : E[v]) if(d[to] == INF){
      d[to] = d[v]+1;
      que.push(to);
    }
  }
}

int main(){
  cin >> n;
  rep(i,0,n){
    int u,k;
    cin >> u >> k;
    rep(i,0,k){
      int v;
      cin >> v;
      E[u].push_back(v);
    }
  }
  bfs(1);
  rep(i,1,n+1){
    cout << i << " ";
    if(d[i] == INF)cout << -1 << endl;
    else cout << d[i] << endl;
  }

  return 0;
}