Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>
using namespace std;
int dx[8] = {1,0,-1,0,1,-1,-1,1};//右、上、左、下、右上、左上、左下、右下のつもり
int dy[8] = {0,1,0,-1,1,1,-1,-1};//実際は色々反転している。どうせ4か8方向調べるから関係ないとしている。
bool seen[60][60]={true};
int main(void){
    int n;cin >> n;
    vector<vector<int> > x(n+1);
    for(int i=0;i<n;i++){
	int a,b; cin >> a >> b;
	for(int j=0;j<b;j++){
	    int c;cin >> c;
	    x[a].push_back(c);
	}
    }
    
    vector<int> dist(n+1,-1);
    queue<int> que;
    dist[1]=0;que.push(1);
    while(!que.empty()){
	int v = que.front();que.pop();
	for(int i=0;i<x[v].size();i++){
	    if(dist[x[v][i]]!=-1) continue;
	    dist[x[v][i]] = dist[v] + 1; 
	    que.push(x[v][i]);
	}
    }
    for(int i=1;i<=n;i++){
	cout << i << " " << dist[i] << endl;
    }
    return 0;
}

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

int D[100];
int a[100][100];

int main() {
  for (int i = 0; i < 100; i++){
    for (int j = 0; j < 100; j++){
	  a[i][j] = 0;
	  }
	 }
	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;
		a[u][v] = 1;
		}
	  }
queue<int> Q; //整数を管理するキューの定義
  for (int i = 1; i < n+1; i++){
   D[i] = -1;
   }
  int src = 1;
  Q.push(src);
  D[src] = 0; //出発点
  while (! Q.empty()) {
    int cur = Q.front(); //先頭要素を取り出す
	Q.pop();
	for(int dst = 1 ; dst <= n; dst++) { //各行き先dstに対して
	if((a[cur][dst] == 1 && D[dst] == -1)) { //curからdstに辺があり、dstが未訪問なら
	      D[dst] = D[cur] + 1; //
		    Q.push(dst); //dstを訪問先に入れる
			}
		  }
		}
	for (int i=1; i < n+1; i++){
	  cout << i << " " << D[i] << endl;
	  }
	}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define M (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;


int g[105][105];
int dist[105]={-1};
bool visit[105];

int main(void){
    int n;
    cin>>n;
    for(int i=0;i<105;i++)dist[i]=-1;
    for(int i=0;i<n;i++){
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++){
            int v;
            cin>>v;
            g[u][v]=1;
        }
    }
    queue<int>que;
    que.push(1);
    dist[1]=0;
    visit[1]=true;
    while(!que.empty()){
        int now = que.front();
        que.pop();
        for(int i=1;i<=n;i++){
            if(i==now)continue;
            if(visit[i])continue;
            if(g[now][i]){
                dist[i]=dist[now]+1;
                que.push(i);
                visit[i]=true;
            }
        }
    }
    for(int i=1;i<=n;i++)cout<<i<<" "<<dist[i]<<endl;
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define EPS (1e-7)
#define INF (1e15)
#define PI (acos(-1))
#define MOD 1000000007

#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define arrdump(n,a) for(int i=0;i<n;i++) {cout<<((i>0)? " ":"")<<a[i];}cout<<"\n";


typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=ja

int d[105];
vector<vector<int>> vv;

int main() {
    int n; cin >> n;
    REP(i, n) d[i] = -1;
    vv.resize(n);
    REP(i, n) {
        int u, k; cin >> u >> k;
        u--;
        REP(j, k) {
            int v; cin >> v;
            v--;
            vv[u].push_back(v);
        }
    }

    queue<int> q;
    q.push(0);
    d[0] = 0;

    while (!q.empty()) {
        int f = q.front(); q.pop();

        for (int t : vv[f]) {
            if (d[t] == -1 || d[t] > d[f] + 1) {
                d[t] = d[f] + 1;
                q.push(t);
            }
        }
    }

    REP(i, n) {
        cout << i+1 << " "<< d[i] << "\n";
    }
       

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
//original idea: stupid wanna check the node with many bfs search 
using namespace std;
#define N 105 // number of nodes
vector<int> neighbors[N]; //start from 1
queue<int> q;
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
int vis[N];
int res = 0;
void bfs(int d){
	if(q.empty()) return;
	//until q is empty
	while(!q.empty()){
		int temp = q.front();
		q.pop();
		if(dist[temp]!=-1){
			if(d<dist[temp]) dist[temp] = d;
			else {dist[temp] = dist[temp];}
		}else {
			dist[temp] = d;
		}
		for(int i = 0; i<neighbors[temp].size(); i++){
			d = dist[temp] +1;//next lever
		if(dist[neighbors[temp].at(i)] == -1){//not visited
			q.push(neighbors[temp].at(i));
			dist[neighbors[temp].at(i)] = d;
			}
		}
		
	}
	
}

int main()
{
    int n;
    scanf("%d",&n);
    memset(dist, -1, sizeof(dist));
    memset(vis, -1, 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);
        	//neighbors[temp].push_back(node);
		}
    }
	q.push(1);//begin from 1
    bfs(0);
    for(int i = 1; i<=n; i++){
    	printf("%d %d\n",i, dist[i]);
	}
    return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;

vector <int> adj[105];
int level[105];
int visited[105];

void bfs()
{
    for(int i = 0; i < 105; i++)
        level[i] = -1;

    queue <int> q;
    q.push(1);
    visited[1] = 1;
    level[1] = 0;

    while(!q.empty())
    {
        int u = q.front();
        q.pop();

        for(int i = 0; i < adj[u].size(); i++)
        {
            int v = adj[u][i];

            if(!visited[v]) {
                visited[v] = 1;
                level[v] = level[u] + 1;
                q.push(v);
            }
        }
    }
}

int main()
{
    int node, vertice, edge, vertex, u, v, i, j;

    cin >> node;

    for(i = 0; i < node; i++)
    {
        cin >> vertice >> edge;
        for(j = 0; j < edge; j++) {
            cin >> vertex;
            adj[vertice].push_back(vertex);
        }
    }

    bfs();

    for(i = 1; i <= node; i++) {
        cout << i << " " << level[i] << endl;
    }

    return 0;
}