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 <cmath>
#include <queue>
#include <iostream>
using namespace std;

int G[110][110] = {0}; /*G????????????????????????????????????????????????, ??????1?????¨??????????????????*/

int n;
int D[110]; /*D???????????????????¨?????????°????????????????????????????????????*/
void bfs(int src){
  
  // cerr << "bfs root = " << src << endl;
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while(! Q.empty()) {
    int cur = Q.front();
    Q.pop();
    // cerr << "visiting " << cur << ' ' << D[cur] << endl;
    for(int i=1; i<=n; i++){
      if(G[cur][i]==1 && D[i]==0){
	D[i] = D[cur]+1;
	Q.push(i);
      }
    }
  }

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

int main(){

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

  bfs(1);
}

C++: #include<algorithm>
#include<bitset>
#include<iostream>
#include<iomanip>
#include<set>
#include<string>
#include<math.h>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;

class zahyo{
	public:
	int x;
	int y;
	zahyo(){
		x=0;
		y=0;
	}
	zahyo(int x_,int y_){
		x=x_;
		y=y_;
	}
	void print(){
		cout<<x<<" "<<y;
	}
	zahyo operator+(zahyo add){
		add.x=this->x+add.x;
		add.y=this->y+add.y;
		return add;
	}
	zahyo operator-(zahyo add){
		add.x=this->x-add.x;
		add.y=this->y-add.y;
		return add;
	}
	bool operator==(const zahyo add) const{
		if(add.x==this->x and add.y==this->y){return true;}
		else{return false;}
	}
	bool operator!=(const zahyo add) const{
		if(add.x==this->x or add.y==this->y){return false;}
		else{return true;}
	}
	bool operator<(const zahyo add) const{
		if(this->x<add.x){return true;}
		else if(this->x==add.x and this->y<add.y){return true;}
		else{return false;}
	}
	bool operator>(const zahyo add) const{
		if(this->x>add.x){return true;}
		else if(this->x==add.x and this->y>add.y){return true;}
		else{return false;}
	}
	void input(){
		cin>>x>>y;
	}
};


int main(){
	int glaph[100][100]={};
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int k;
		cin>>k>>k;
		for(int j=0;j<k;j++){
			int v;
			cin>>v;
			v--;
			glaph[i][v]=1;
		}
	}
	queue<int> widesearch;
	int ans[100]={};
	int counter=1;
	ans[0]=1;
	widesearch.push(0);
	while(widesearch.size()>0){
		counter++;
		int size=widesearch.size();
		for(int i=0;i<size;i++){
			int from=widesearch.front();
			widesearch.pop();
			for(int j=0;j<n;j++){
				if(glaph[from][j]==1 and ans[j]==0){
					ans[j]=counter;
					widesearch.push(j);
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<ans[i]-1<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int N = 101;
const int CRL = -1;
int adj[N][N] = {0};
int d[N]={0};
int kai[N];

int main()
{
    int n;
    memset(d, CRL, sizeof(d));
    d[1]=0;
    queue <int> q;
    q.push(1);
    cin >> n;
    for(int i=1;i<=n;i++){
    	int k;
    	int l;
    	cin >> l >> k;
    	kai[i]=l;
    	for(int j=1;j<k+1;j++){
    		int m;
    		cin >> m;
    		adj[i][j] = m;
    	}
    }
    while(q.size()){
    	int k = q.front();
    	q.pop();
    	for(int i=1;i<kai[i]+1;i++){
    		if(d[adj[k][i]]==CRL){
    			d[adj[k][i]] = d[k]+1;
    			q.push(adj[k][i]);
    		}
    	}
    }
    for(int i=2;i<N;i++)
    	if(d[i]==0)d[i]=-1;
    for(int i=1;i<=n;i++){
    	cout << i << " " << d[i] << endl;
    }
    return 0;
}

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<queue>
#include<math.h>
#include<cmath>
#include<bitset>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<iostream>
#include<utility>
using namespace std;
int main(){
  vector<vector<int> > g(110);
  int INF=(1<<29);
  int x,n,i,j,v,k,u,ans[110];
  queue<int> q;
  scanf("%d\n",&n);
  for(i=0;i<n;i++){
    scanf("%d %d",&u,&k);
    u--;
    for(j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      g[u].push_back(v);
    }
  }
  for(i=0;i<n;i++){
    ans[i]=INF;
  }
  q.push(0);
  ans[0]=0;
  while(!q.empty()){
    x=q.front();
    q.pop();
    for(i=0;i<g[x].size();i++){
      if(ans[g[x][i]]>ans[x]+1){
        ans[g[x][i]]=ans[x]+1;
        q.push(g[x][i]);
      }
    }
  }
  for(i=0;i<n;i++){
    if(ans[i]==INF){
      ans[i]=-1;
    }
  }
  for(i=0;i<n;i++){
    printf("%d %d\n",i+1,ans[i]);
  }
  return 0;
}

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

class vertex{
public:
  vector<vertex*> to;
  bool visited;
  int num;
  int dis;

  vertex(){
    visited=false;
    dis = -1;
  }

  void bfs(int dis){
    this->dis=dis;
    visited=true;
    queue<vertex*> qv; qv.push(this);

    while(!qv.empty()){
      vertex* now = qv.front();  qv.pop();

      for(int i=0; i<now->to.size(); i++){
        if(!now->to[i]->visited){
          qv.push(now->to[i]);
          now->to[i]->visited = true;
          now->to[i]->dis = now->dis+1;
        }
      }
    }
  }

  void print(){
    cout << num << " " << dis << endl;
  }
};

int main(){
  int n; cin >> n;
  vertex graph[n+1];

  int v, k, to;
  for(int i=0; i<n; i++){
    cin >> v >> k;
    graph[v].num = v;
    for(int j=0; j<k; j++){
      cin >> to;
      graph[v].to.push_back(&graph[to]);
    }
  }

  graph[1].bfs(0);

  for(int v=1; v<=n; v++){
    graph[v].print();
  }

  return 0;
}

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

using namespace std;

const int INF = 100000000;

int main(){
	int n;
	cin >> n;
	vector<vector<int>> node(n+1);
	for(int i=0;i<n;i++){
		int u,k,tmp;
		cin >> u >> k;
		for(int j=0;j<k;j++){
			cin >> tmp;
			node[u].push_back(tmp);
		}
	}
	
	//search
	vector<int> res(n+1,INF);
	queue<int> que;
	queue<int> copied;
	queue<int> zeros;
	que.push(1);
	
	int count = 0;
	for(int i=0;i<n-1;i++){
		while(que.size()){
			int k = que.front(); que.pop();
			//cout << "-> " << k << endl;
			if(res[k] >= INF) res[k] = count;
			for(int v:node[k]){
				copied.push(v);
			}
		}
		count++;
		que = copied;
		copied = zeros;
	}

	//output
	for(int i=1;i<=n;i++){
		if(res[i] >= INF) res[i] = -1;
		cout << i << " " << res[i] << endl;
	}

	return 0;
}