Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

using namespace std;

int main() {
  int n; cin >> n;

  vector<vector<int>> G(n, vector<int>());

  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--;
      G[u].push_back(v);
    }
  }

  vector<int> dist(n, 1000);
  dist[0] = 0;

  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < n; j++) {
      for (auto k: G[j]) {
        dist[k] = min(dist[k], dist[j] + 1);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i + 1 << ' ';
    if (dist[i] == 1000) {
      cout << -1 << endl;
    } else {
      cout << dist[i] << endl;
    }
  }
}

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

using namespace std;
static const int N = 100;
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, k, v;

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

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

  bfs(0);

  return 0;
}

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

int n = 0;
int u = 0;
int k[101];
int v[101][101];
int Short[101] = { 0 };
//into n = 0;
int p = 0;
int fptr = 0;
int lptr = 0;
int x1[10000];
int y1[10000];
void Enqueue(int y, int x)
{
	x1[lptr] = x;
	y1[lptr] = y;
	lptr = (lptr + 1) % n;
}
void bfs(int l) {
	int i;
	while (fptr!=lptr) {
		l=x1[fptr];
		p=y1[fptr];
		fptr = (fptr + 1) % n;
		if(Short[l]!=-1){
			continue;
		}
		for(i=0;i<k[l];i++){
			if(v[l][i]){
				Enqueue(p+1,v[l][i]);
			}
		}
		//Enqueue(v[l][in]);
		Short[l]=++p;
	}
}
int main()
{
	int i;
	int j;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> u;
		cin >> k[u];
		for (j = 0; j < k[u]; j++) {
			cin >> v[u][j];
		}
	}
	Enqueue(-1,1);
	for(i=0;i<n+1;i++){
		Short[i]=-1;
	}
	bfs(0);
	cout << "\n";
	
	for (i = 1; i < n + 1; i++) {
		cout << i << " " << Short[i] << endl;
	}
}

C++: #include <algorithm>
#include <iostream>
#include <list>
#include <vector>

class TNode
{
	std::list<TNode*> FNexts;
	int  FDistance;
public:
	TNode()
	: FDistance(-1)
	{
	}
	
	int GetDistance()const {return FDistance;}
	
	void Push(TNode* New)
	{
		FNexts.push_back(New);
	}
	
	void Visit(int Distance)
	{
		if(FDistance >= 0 && Distance > FDistance) return;
		FDistance = Distance;
		for(std::list<TNode*>::iterator It = FNexts.begin(); It != FNexts.end(); ++It){
			(*It)->Visit(Distance + 1);
		}
	}
};

class TGraph
{
	std::vector<TNode*> FNodes;
public:
	TGraph(int Count)
	: FNodes(Count + 1)
	{
		for(int i = 1; i < Count + 1; ++i){
			FNodes[i] = new TNode();
		}
	}
	
	~TGraph()
	{
		for(unsigned int i = 1; i < FNodes.size(); ++i){
			delete FNodes[i];
		}
	}
	
	void InputNode()
	{
		int Index;
		std::cin >> Index;
		int BranchCount;
		std::cin >> BranchCount;
		for(int i = 0; i < BranchCount; ++i){
			int Target;
			std::cin >> Target;
			FNodes[Index]->Push(FNodes[Target]);
		}
	}
	
	void BreathFirstSearch()
	{
		FNodes[1]->Visit(0);
	}
	
	int Distance(int Target)const
	{
		return FNodes[Target]->GetDistance();
	}
};

int main()
{
	int Count;
	std::cin >> Count;
	TGraph Graph(Count);
	for(int i = 0; i < Count; ++i){
		Graph.InputNode();
	}
	Graph.BreathFirstSearch();
	
	for(int i = 0; i < Count; ++i){
		std::cout << i + 1 << " " << Graph.Distance(i + 1) << std::endl;
	}
	return 0;
}

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

int main() {
	int a[101][101] = {{0}};
	int n, u, k, v, km;
	km = 0;
	cin >> n;
	for ( int i = 0; i < n; i++ ) {
		cin >> u, k;
		for ( int j = 1; j <= k; j++ ) {
			cin >> v;
			a[u][v] = 1;
		}
		km++;
	}
	int n0;
	int b[101] = {- 1};
	queue<int> q;
	q.push( 1 );
	b[1] = 0;
	for ( int i = 1; i <= km; i++ ) {
		n0 = q.front();
		q.pop();
		for ( int j = 1; j <= km + 1; j++ ) {
			if ( a[n0][j] == 1 && b[j] == - 1 ) {
				q.push( j );
				b[j] = b[n0] + 1;
			}
			if ( q.empty() ) break;
		}
	}
	for ( int i = 1; i <= km; i++ )
		cout << i << " " << b[i] << endl;
}

C++: //Breadth First Search (?????????) Algorithm
#include<iostream>
#include<queue>
#define WH 0
#define GR 1
#define BL 2
#define INF 2000000
void bfs(int);
void init(void);
int n, *color, **M, *d;
std::queue<int> Q;
int main(){
  std::cin>>n;
  color=new int[n], d=new int[n], M=new int*[n];
  for(int i=0, u, k;i<n;i++){
    M[i]=new int[n];
    color[i]=WH, d[i]=INF;
    for(int j=0;j<n;j++) M[i][j]=0;
    std::cin>>u>>k;
    for(int j=0, v;j<k;j++){
      std::cin>>v;
      M[u-1][v-1]=1;
    }
  }
  bfs(0);
  for(int i=0;i<n;i++) std::cout<<i+1<<" "<<(d[i]!=INF?d[i]:-1)<<std::endl;
  delete color;
  delete d;
  for(int i=0;i<n;i++) delete M[i];
  delete M;
  return 0;
}

void bfs(int s){
  color[s]=GR, d[s]=0;
  Q.push(s);
  while(!Q.empty()){
    int u=Q.front();
    Q.pop();
    for(int i=0;i<n;i++){
      if(M[u][i]&&color[i]==WH){
        color[i]=GR, d[i]=d[u]+1;
        Q.push(i);
      }
    }
    color[u]=BL;
  }
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



int main(){
    int n;
    cin >> n;
    V<V<int>> g(n);
    rep(i,n) {
        int a;
        cin >> a;
        a--;
        int k;
        cin >> k;
        rep(j,k) {
            int b;
            cin >> b;
            b--;
            g[a].push_back(b);
        }
    }

    V<int> d(n,-1);
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while(sz(q)) {
        int v = q.front(); q.pop();
        for(int nv : g[v]) {
            if(d[nv] != -1) continue;
            d[nv] = d[v] + 1;
            q.push(nv);
        }
    }

    rep(i,n) {
        printf("%d %d\n", i+1, d[i]);
    }
}