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
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

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

	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 <iostream>
#include <queue>
using namespace std;
struct id{
    int c[101];
    int p=0;
    int d=-1;
    int u=0;
};
int n,t=0;
id a[101];
queue<int> q;
void bps(int e){
    q.push(e);
    while(!q.empty()){
        int f=q.front();
        for(int i=1;i<=n;i++){
            if(a[f].c[i]==1&&a[i].u==0){
                a[i].d=a[f].d+1;
                q.push(i);
                a[i].u++;
            }
        }
        q.pop();
        a[f].u++;
    }
    
}

int main(){
    int x,y,z;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        for(int j=1;j<=y;j++){
            cin>>z;
            a[x].c[z]=1;
            a[z].p=x;
        }
    }
    a[1].d+=1;
    bps(1);
    for(int i=1;i<=n;i++){
        cout<<i<<" "<<a[i].d<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#define INFTY -10000000
using namespace std;



int a[124][124] = {{}};
int d[124] = {};
int n;
void bfs(int k){
  queue <int> q;
  q.push(k);
  int u;


  while(!q.empty()){
    u = q.front(); q.pop();
    for(int v = 0;v < n;v++){
      if(a[u][v] == 0) continue;
      if(d[v] == INFTY) {
        d[v] = d[u] + 1;
        cout << v << " " << d[v] << endl;
      }
      q.push(v);
    }

  }
}

int main(){

  int u,k,x;
  cin >> n;

d[0] = 0;
for(int i = 1; i < n;i++) d[i] = INFTY;

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

  bfs(0);

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

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

int main(){

  bool adj[100][100]={};//初期化を忘れない
  int d[100]={};
  int n,u,v,k;

  cin >> n;
  for (int i=0; i<n; i++){
    cin >> u >> k;
    for (int j=0; j<k; j++){
      cin >> v;
      adj[u-1][v-1] = true;
    }
  }
  
  queue<int> q;
  bool pushed[100]={};
  q.push(1);
  pushed[1-1] = true;
  d[0] = 0;

  while(q.empty() == false){
    u = q.front();
    q.pop();
    for (int i=0; i<n; i++){
      if (adj[u-1][i]){
	if (pushed[i] != true){
	  d[i] = d[u-1] + 1;
	  q.push(i+1);
	  pushed[i] = true;
	}
      }
    }
  }
  
  cout << 1 << " " << 0 << endl;
  for (int i=1; i<n; i++){
    if (d[i] == 0)
      cout << i+1 << " " << -1 << endl;
    else
      cout << i+1 << " " << d[i] << endl;      
  }

}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

bool was_output = false;
template<class t>
void output(t a){
	if(was_output)cout << " ";
	cout << a;
	was_output = true;
}
void outendl(){
	was_output = false;
	cout << endl;
}

vvector<int> edges;
vector<int> found_time;

void bfs(int start){
	found_time[start] = 0;
	queue<pii> que;
	que.emplace(start, 0);
	while(que.size()){
		pii d = que.front();
		que.pop();
		foreach(i, edges[d.first]){
			if(found_time[i]>=0)continue;
			found_time[i]=d.second+1;
			que.emplace(i, d.second+1);
		}
	}
}

int main(){
	int n;
	cin >> n;
	edges.resize(n);
	found_time.resize(n, -1);
	rep(i, n){
		int t;
		int time;
		cin >> t >> time;
		--t;
		rep(j, time){
			int to;
			cin >> to;
			--to;
			edges[t].push_back(to);
		}
	}
	bfs(0);
	rep(i, n){
		output(i+1);
		output(found_time[i]);
		outendl();
	}
	return 0;
}

C++: //1933-
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define INT(x) int x; scanf("%d",&x)
#define INPUT(x) scanf("%d",&x)
#define REP1(x,n) for(int x = 0; x < n; x++)
#define REP2(x,s,e) for(int x = s; x <= e; x++)
#define BR printf("\n")
#define INF 2000000000
#define FIND 1
#define END 2

vector<int> V[100];
priority_queue<pair<int,int> > PQ;//distance,id
int d[100];
int StateNode[100] = {};

void BFS(pair<int,int> p){
    if(d[p.second] <= -p.first) {
        PQ.pop();
        if(!PQ.empty()) BFS(PQ.top());
        return;
    }
    d[p.second] = -p.first;
    PQ.pop();
    
    REP1(k,V[p.second].size()){
        if (StateNode[V[p.second][k]] == 0 || (StateNode[V[p.second][k]] == FIND && -p.first+1)) {
            PQ.push(make_pair(p.first-1,V[p.second][k]));
            StateNode[V[p.second][k]] = FIND;
        }
    }
    
    if(!PQ.empty()) BFS(PQ.top());
}

int main(){
    INT(N);
    REP1(i,N){
        int from,n;
        scanf("%d %d",&from,&n);
        REP1(j,n){
            INT(to);
            V[i].push_back(to-1);
        }
        d[i] = INF;
    }
    
    PQ.push(make_pair(0,0));
    BFS(PQ.top());
    
    REP1(i,N){
        if (d[i] != INF) {
            printf("%d %d\n",i+1,d[i]);
        }else{
            printf("%d %d\n",i+1,-1);
        }
    }
    
    return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <bitset>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

struct dd{
    int id,f;
    dd(){
        id=0;
        f=-1;
    }
    dd(int a,int b){
        id=a;
        f=b;
    }
};
bool data[101][101]={};

void f1(dd*,int,int&,int);

int main()
{
    int N,M,num1,num2;
    dd ddata[101]={};
    cin>>N;
    int val[101]={};
    for(int i=0;i<N;i++){
        cin>>num1;
        cin>>M;
        val[num1]=-1;
        for(int j=0;j<M;j++){
            cin>>num2;
            data[num1][num2]=true;
        }
    }
    int time=1;
    queue<dd> que;
    bool vd[101]={};
    que.push(dd(1,0));
    vd[1]=true;
    for(dd ddata=que.front();!que.empty();que.pop(),ddata=que.front()){
        val[ddata.id]=ddata.f;
        for(int i=1;i<=N;i++){
            if(data[ddata.id][i]&&!vd[i]){
                vd[i]=true;
                que.push(dd(i,ddata.f+1));
            }
        }
    }
    for(int i=1;i<=N;i++){
        cout<<i<<' '<<val[i];
        cout<<endl;
    }
}