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++: #include<bits/stdc++.h>
using namespace std;
static const int N=100;
enum COLOR{WHITE,GRAY,BLACK};
static const int INFTY=(1<<21);

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

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    for(int i=0;i<n;i++)d[i]=INFTY;
    d[s]=0;
    int u;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int v=0;v<n;v++)
        {
            if(M[u][v]==0)continue;
            if(d[v]!=INFTY)continue;
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<i+1<<" "<<(d[i]==INFTY?-1:d[i])<<endl;
    }
}

int main()
{
    int u,v,k,i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)M[i][j]=0;
    }

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

    bfs(0);
 	return 0;
}

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

#define int long long
 	
const int N = 100 + 1;
vector< int > g[N];
int d[N]; // stores the depth of all nodes

void BFS(int u){
   queue<int> q;
   q.push(u);
   d[u] = 0;
   while (!q.empty()){
	   int x = q.front(); q.pop();
	   for(auto it : g[x]){
		     if (d[it] != -1) continue;
		     d[it] = d[x] + 1;
		     q.push(it);
		 }  
	 }
}

int32_t main(){
   int n, m, deg;
   cin >> n;
   for (int i = 0; i < n; i++){
	    cin >> m >> deg;
	    m--;
	    for(int i = 0; i < deg; i++){
		    int x;
		    cin >> x;
		    x--;
		    g[m].push_back(x);
	    }  
	 }
	for (int i = 0; i < n; i++) d[i] = -1;
    BFS(0);
    for (int i = 0; i < n; i++){
	   int idx = i + 1;
	   if (d[i] == -1) cout << idx << " " << -1 << endl;
	   else cout << idx << " " << d[i] << endl; 
	 }
}

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

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

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

int 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()==0){
		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,u,k,v;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>u>>k;
		for(int j=0;j<k;j++){
			cin>>v;
			v--;
			adj[u-1].push_back(v);
		}
	}
	cout<<adj[0].size()<<endl;
	bfs(n,0);
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<d[i]<<endl;
	}
	return 0;
}

C++: #include <queue>
#include <vector>
#include <iostream>
using namespace std;
int N;
int D[110];
int G[110][110];
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=0;i<N;i++){
			if(G[cur][i] && D[i]==0){
				D[i] = D[cur]+1;
				Q.push(i);
			}
		}
	}
	for(int i=0;i<N;i++){
		if(i!=src && D[i]==0) D[i]=-1;
	}
	D[src]=0;
}

int main(){
	while(cin >> N && N>0){
		vector<vector<int> > g(110,vector<int>(110,0));
		int u,k;
		for(int i=0;i<N;i++){
			cin >> u >> k;
			for(int j=0;j<k;j++){
				int a;
				cin >> a;
				g[u-1].push_back(a); 
			}
		}
		for(int i=0;i<N;i++){
			for(int j=0;j<g[i].size();j++){
				int f = g[i][j];
				G[i][f-1] = 1;
			}
		}
		bfs(0);
		for(int i=0;i<N;i++){
			cout << i+1 << " " << D[i] << endl;
		}
	}
}

#if 0
無限ループに陥らないようにするのに少し苦労した。プログラム上では番号を0から始めたのもよくないかもしれない。
#endif

C++: #include<stdio.h>
#include<queue>
#include<algorithm>

#define MAX 100

int G[MAX+1][MAX+1], d[MAX+1];
int n;
std::queue<int> Q;

void bfs() {
    d[1] = 0;

    Q.push(1);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 1; i <= n; ++i) {
            if (G[cur][i] == 0) continue;
            if (d[i] != -1) continue;

            Q.push(i);
            d[i] = d[cur] + 1;
        }
    }

}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        d[i] = -1;
        for (int j = 1; j <= n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < n; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        for (int j = 0; j < k; ++j) {
            int t;
            scanf("%d", &t);
            G[u][t] = 1;
        }
    }

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

}

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

using namespace std;

#define MAX 100

#define WHITE 0
#define GRAY 1
#define BLACK 2

int T;
int N;

int M[MAX][MAX];
int C[MAX],D[MAX];

void bfs(int s)
{
    queue<int> *q = new queue<int>;

    q->push(s);
    D[s] = 0;
    C[s] = GRAY;

    while(q->size() > 0)
    {
        int u = q->front();
        q->pop();

        for (int i = 0; i < N; i++)
        {
            if (M[u][i] && C[i] == WHITE)
            {
                C[u] = GRAY;
                D[i] = D[u] + 1;
                q->push(i);
            }
        }

        C[u] = BLACK;
    }
}

int main ()
{
    cin >> N;

    for (int i = 0; i < N; i++)
    {
        C[i] = WHITE;
        D[i] = -1;

        for (int j = 0; j < N; j++)
        {
            M[i][j] = 0;
        }
    }

    for (int i = 0; i < N; i++)
    {
        int u, k;
        cin >> u >> k;
        u--;

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

/*
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (j > 0)
                cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
*/
    bfs(0);

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

    return 0;
}