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;
int map[101][101];
int all = 0;
int search_2[101];
int n;
void bfs(int k, int m) {
	search_2[k] = m;
	queue<int>q;
	for (int i = 2; i <= n; i++) {
		if (map[k][i] && (search_2[i] == 0 || search_2[i] > m + 1)) {
			q.push(i);
		}
	}
	while (!q.empty())
	{
		bfs(q.front(), m + 1);
		q.pop();
	}
}
int main(void) {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		int m;
		cin >> m;
		for (int j = 0; j < m; j++) {
			int b;
			cin >> b;
			map[a][b] = 1;
		}
	}
	bfs(1, 0);
	cout << 1 << " " << 0 << endl;
	for (int i = 2; i <= n; i++) {
		cout << i << " ";
		if (search_2[i] == 0) cout << -1 << endl;
		else cout << search_2[i] << endl;
	}
}

C++: ///
// File:  alds1_11_c.cpp
// Author: ymiyamoto
//
// Created on Wed Oct 18 00:49:05 2017
//

#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
static uint32_t graph[100][100];
static vector<int32_t> d(100, -1);
static queue<uint32_t> q;

static void bfs(uint32_t n)
{
  q.push(0);
  d[0] = 0;

  while (!q.empty()) {
    uint32_t index = q.front();
    q.pop();

    for (uint32_t i = 0; i < n; i++) {
      if (d[i] == -1 && graph[index][i] == 1) {
        q.push(i);
        d[i] = d[index] + 1;
      }
    }
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t id, k;
    cin >> id >> k;
    for (uint32_t j = 0; j < k; j++) {
      uint32_t val;
      cin >> val;
      graph[id - 1][val - 1] = 1;
    }
  }

  bfs(n);

  for (uint32_t i = 0; i < n; i++) {
    if (i != 0) cout << " ";
    cout << d[i];
  }
  cout << endl;

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#define N 101
using namespace std;

typedef struct{
  int q[10000];
  int head;
  int tail;
}Queue;

//queue
Queue que;
int g[N][N]; 
int n;
int d[N];
bool us[N];
void Enque(int data){
  que.q[que.tail]=data;
  que.tail++;
}

int Deque(void){
  int data;
  data=que.q[que.head];
  que.head++;
  return data;
}

bool isEmpty(){
  if(que.tail-que.head==0)return true;
  else return false;
}

//bfs
void bfs(int s){
  int u,v;
  Enque(s);
  while(true){

    if(isEmpty()==true) {
      break;
    }
    u=Deque();
    //  cout << "u = " << u << endl;
      for(int i=1;i<= n;i++){
 
      if(us[i]==false && g[u][i] == 1){
	v=i;
	//	cout << "i - " << i <<endl;
	us[i]=true;
	d[v]=d[u]+1;
	Enque(v);
      }
    }
  }
}

int main(){

  int n2,data,u;

 //load n
  cin >>n;

  //initialize
  que.tail=0;
  que.head=0;
  for(int i=0;i<N;i++){
    us[i]=false;
    d[i]=-1;
    for(int j=0;j<N;j++){
      g[i][j]=0;
    }
  }

  //load data
  for(int i=0;i<n;i++){
    scanf("%d%d",&u,&n2);
    for(int j=0;j<n2;j++){
      scanf("%d",&data);
      g[u][data]=1;
    }
  }
  //execute

  d[1]=0;
  us[1]=true;
  bfs(1);


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

  return 0;
}

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

#define MAX_NODE 100
#define INF 1000000000

using namespace std;

int d[MAX_NODE];
vector<int> adj[MAX_NODE];

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

	for (int i = 0; i < n; i++)
		d[i] = (i == start ? 0 : INF);

	que.push(start);
	while(!que.empty()) {
		int from = que.front();
		que.pop();

		for (int i = 0; i < adj[from].size(); i++) {
			int to = adj[from][i];
			if (d[to] == INF) {
				d[to] = d[from] + 1;
				que.push(to);
			}
		}
	}
}

int main() {
	int n;

	cin >> n;

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		u--;
		while (k--) {
			int v;
			cin >> v;
			v--;
			adj[u].push_back(v);
		}
	}

	bfs(n, 0);

	for (int i = 0; i < n; i++)
		cout << i + 1 << " " << d[i] << endl;

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<stack>
#include<queue>
using namespace std;

int n;
int m[100][100], d[100], f[100];
int t = 0;
char c[100];
queue<int> q;

void bfs(int u){
  q.push(u);
  c[u] = 'g';
  d[u] = 0;

  while(q.empty() == 0){
    u = q.front();
    q.pop();
    for(int i = 0; i < n; i++){
      if(m[u][i] == 1 && c[i] == 'w'){
        q.push(i);
        c[i] = 'g';
        d[i] = d[u] + 1;
      }

    }
    c[u] = 'b';
  }

}


int main(){

  cin >> n;

  for(int i = 0; i < n; i++){
    c[i] = 'w';
    d[i] = 100;
    for(int j = 0; j < n; j++){
      m[i][j] = 0;

    }
  }

  for(int i = 0; i < n; i++){
    int id, k, x;
    cin >> id >> k;
    for(int j = 0; j < k; j++){
      cin >> x;
      m[i][x - 1] = 1;
    }
  }

  bfs(0);

  for(int i = 0; i < n; i++){
    cout << i + 1 << " " <<((c[i] == 'w') ? -1 : d[i]) << endl;
  }

  return 0;
}

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

const int Max = 100;
const int white = 0;
const int black = 1;
int M[Max][Max];
int d[Max], nt[Max], color[Max];
int n;
queue<int> Q;

int next(int u){
    for(int v=nt[u]; v<n; v++){
        nt[u] = v + 1;
        if(M[u][v]) return v;
    }
    return -1;
}

void bfs_visit(int u){
    for(int i=0; i<n; i++){
        nt[i] = 0;
        d[u] = 0;
    }

    color[u] = black;
    Q.push(u);
    while(!Q.empty()){
        int u = Q.front();
        int v = next(u);
        if(v != -1){
            if(color[v] == white){
                color[v] = black;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        } else {
            Q.pop();
        }
    }
}

int main(){
    int u, k, v;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        color[i] = white;
    }
    for(int i=0; i<n; i++){
        cin >> u >> k;
        u--;
        for(int i=0; i<k; i++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs_visit(0);

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

C++: //16D8103013E Sen Shunkou
#include<iostream>
#include<queue>
int n;
int cnt=0;

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

    int cnt=0;
    int A[100][100];
    int dis_min[n];
    bool visited[n];

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            A[i][j]=0;
        }
    }

    for(int i=0;i<n;i++){
        std::cin>>A[i][0];
        std::cin>>A[i][1];
        for(int j=0;j<A[i][1];j++){
            std::cin>>A[i][j+2];
        }
    }

    for(int i=0;i<n;i++){
        dis_min[i]=0;
    }

    //initialization
    std::queue<int> que;
    que.push(0);
    //std::cout<<"\n"<<que.front()<<"\n";
    //std::cout<<que.size()<<"\n\n";

    while(!que.empty()){
        int q=que.size();
        for(int k=0;k<q;k++){
            //std::cout<<"quesize"<<que.size()<<"\n";
            int i=que.front();
            que.pop();
            visited[i]=true;
            dis_min[i]=cnt;
            //std::cout<<i<<"cnt"<<cnt<<"\n";
            if(A[i][1]!=0){
                for(int j=0;j<A[i][1];j++){
                    if(visited[A[i][j+2]-1]!=true){
                        visited[A[i][j+2]-1]=true;
                        que.push(A[i][j+2]-1);
                        //std::cout<<A[i][j+2]-1<<"\n";
                    }
                }
            }
        }
        cnt++;
        //std::cout<<"cnt="<<cnt<<"\n";
    }

    //std::cout<<"===========\n";
    for(int i=0;i<n;i++){
        std::cout<<i+1<<" "<<dis_min[i]<<"\n";
    }
    return 0;
}