Go: package main

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

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

C++: #include <iostream>
#define INF (1<<25)
#define MAX 1001
using namespace std;

template <typename T>
class queue{
  private:
    T array[MAX];
    int head, tail, n;
  public:
    queue(int n){
      head=tail=0;
      this->n = n;
    }

    void push(T value){
      if (head == (tail+1)%n)
        return ;
      array[tail] = value;
      tail = (tail+1)%n;
    }

    bool empty(){
      if (head == tail)
        return true;
      return false;
    }

    void pop(void){
      if (head == tail) return;
      head = (head+1)%n;
    }

    T front(void){
      return array[head];
    }
};
int graph[MAX][MAX];
bool isDiscoverd[100];
int dist[100];
int dim[MAX];

void bfs(int i){
  queue<int> searchQueue(MAX);
  searchQueue.push(i);
  while(!searchQueue.empty()){
    auto current = searchQueue.front();
    int k = dim[current];
    searchQueue.pop();
    isDiscoverd[current] = true;
    for (auto ite=graph[current] ; ite != graph[current]+k ; ++ite){
      dist[*ite] = min(dist[*ite], dist[current]+1);
      if (!isDiscoverd[*ite]) searchQueue.push(*ite);
    }
  }
}

int main(){
  int n;
  cin >> n;
  for (int i=0;i<n;++i){
    int id;
    cin >> id;
    cin >> dim[id];
    for(int j=0;j<dim[id];++j){
      int temp;
      cin >> temp;
      graph[id][j] = temp;
      isDiscoverd[id]=false;
    }
  }

  dist[1] = 0;
  for (int i=2;i<=n;++i)
    dist[i] = INF;

  bfs(1);

  for (int i=1;i<=n;i++){
    cout << i << " " << (dist[i]>=INF?-1:dist[i]) <<endl;
  }

  return 0;
}

C++: 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <queue>

using namespace std;

const int MAX_N = 100;
int n;
int suji[MAX_N];
int	num[MAX_N];
int adj[MAX_N][MAX_N];
int ans[MAX_N][MAX_N];
int D[MAX_N];

void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;

	while(!Q.empty()){
		int cur = Q.front();
		Q.pop();

		for(int i = 1; i < n; i++){
			if(ans[cur][i] == 1 && D[i] == 0){
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}
}


int main(void){

	cin >> n;
	for(int i = 0; i < n; i++){
		
		cin >> suji[i] >> num[i];

		
		for(int j = 0; j < num[i]; j++){
			cin >> adj[i][j];
			adj[i][j] -= 1;
		}
	}

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

	for(int i = 0; i < n; i++){
		for(int j = 0; j < num[i]; j++){
			ans[i][adj[i][j]] = 1;
		}
	}

	bfs(0);

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

C++: #include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define N 105 // number of nodes
vector<int> neighbors[N]; //start from 1

//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
void bfs(int start, int key,int d){
	if(start == key){
		//if(d < dist[key])
			dist[key] = d;
		return;
	} 
	for(int i = 0; i< neighbors[start].size(); i++){
		bfs(neighbors[start].at(i), key,d+1);
	}
}

int main()
{
    int n;
    scanf("%d",&n);
    memset(dist, 105, sizeof(dist));
    for(int i = 0; i<n; i++){
        int node;
        scanf("%d",&node);
        int num;
        scanf("%d",&num);
        for(int j = 0; j<num; j++){
        	int temp;
        	scanf("%d",&temp);
        	neighbors[node].push_back(temp);
		}
    }
    for(int i = 1; i<=n; i++){
    	bfs(1,i,0);
	}
    for(int i = 1; i<=n; i++){
    	printf("%d %d\n",i, dist[i]);
	}
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <assert.h>
#define MAX_V 100
using namespace std;

int adjacencyMatrix[MAX_V][MAX_V] = {};
int d[MAX_V];
int V;

void bfs(int start)
{
    queue<int> Q;
    Q.push(start);

    for (int i = 0; i < V; ++i)
        d[i] = -1;

    d[start] = 0;

    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        for (int i = 0; i < V; ++i) {
            if (d[i] != -1) continue;
            if (adjacencyMatrix[u][i] == 1) {
                d[i] = d[u] + 1;
                Q.push(i);
            }
        }
    }
    return;
}

int main()
{
    cin >> V;

    for (int i = 0; i < V; ++i) {
        int u, k;
        cin >> u >> k;
        assert(u = i + 1);
        for (int j = 0; j < k; ++j) {
            int tmp;
            cin >> tmp;
            adjacencyMatrix[i][tmp - 1] = 1;
        }
    }

    bfs(0);

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

    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
// 0オリジンなことに注意！！！！

#define MAX 100
#define INF 101 // 距離の最大値+2

int n, M[MAX][MAX];
int d[MAX]; // distance from 1

void bfs(int start){
    int i, j, u;
    queue<int> q;
    
    for (i = 0; i < n; i++) {
        d[i] = INF;
    }
    
    q.push(start);
    d[start] = 0;
    while( !q.empty() ) {
        u = q.front();
        q.pop();
        for (j = 0; j < n; j++) { // 取り出した頂点から繋がっている頂点のうち未探索なものをqueueに入れる
            if(M[u][j] == 1 && d[j] == INF){
                d[j] = d[u] + 1;
                q.push(j);
            }
        }
    }
    
    for (i = 0; i < n; i++) {
        printf("%d %d\n", i+1, d[i] == INF ? -1 : d[i]);
    }
}


int main(){
    int i, j, m, a;
    
    scanf("%d", &n);
    // init
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }
    // read
    for(i = 0; i < n; i++){
        scanf("%d %d", &m, &m); // First m is dammy
        for(j = 0; j < m; j++){
            scanf("%d", &a);
            M[i][a-1] = 1;
        }
    }
#ifdef D
    // どういう風にグラフが格納されてるか見る
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            printf("%d ", M[i][j]);
        }
        printf("\n");
    }
#endif
    
    bfs(0);
    
    return 0;
}