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 <stdio.h>
#include <queue>
using namespace std;
struct bfs{
    int n;
    int l;
};
int main(){
    int n,a,m,temp,ans[101];
    bool graph[101][101];
    bfs t,b;
    queue<bfs> q;
    scanf("%d",&n);
    for(int i=0;i<101;i++){
        for(int j=0;j<101;j++){
            graph[i][j]=false;
        }
        ans[i]=-1;
    }
    for(int i=0;i<n;i++){
        scanf("%d%d",&a,&m);
        for(int j=0;j<m;j++){
            scanf("%d",&temp);
            graph[a][temp]=true;
        }
    }
    t.n=1;
    t.l=0;
    q.push(t);
    while(!q.empty()){
        t=q.front();
        q.pop();
        if(ans[t.n]==-1)ans[t.n]=t.l;
        else continue;
        t.l++;
        b=t;
        for(int i=1;i<=n;i++){
            if(graph[t.n][i]){
                b.n=i;
                q.push(b);
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d %d\n",i,ans[i]);
    }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <string.h>
#include <limits.h>
using namespace std;
vector<int> v[101];
int d[101];
queue<int> q;
bool flag[101];
int main(){
	int n, u, k; scanf("%d", &n);
	for (int i = 0; i < n; ++i){
		scanf("%d %d", &u, &k);
		for (int j = 0; j < k; ++j){
			int tmp; scanf("%d", &tmp);
			v[u].push_back(tmp);
		}
	}
	memset(d, -1, sizeof(d));
	d[1] = 0;
	q.push(1); flag[1] = true;
	while (!q.empty()){
		int m = q.front(); q.pop();
		for (int i = 0; i < v[m].size(); ++i){
			int tmp = v[m][i];
			if (!flag[tmp]){
				flag[tmp] = true;
				d[tmp]=d[m]+1;
				q.push(tmp);
			}
		}
	}
	for (int i = 1; i <= n; ++i){
		cout << i << ' ' << d[i] << '\n';
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const int MAX = 105;

vector<int> to[MAX];
bool seen[MAX];
int d[MAX];

void bfs(int u) {
  queue<int> q;
  seen[u] = true;
  d[u] = 0;
  q.push(u);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v: to[u]) {
      if (seen[v]) continue;
      d[v] = d[u] + 1;
      seen[v] = true;
      q.push(v);
    }
  }
}

int main() {
  int n, u, k, v;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      to[u-1].push_back(v-1);
    }
  }

  rep(i, n) {
    seen[i] = false;
    d[i] = INF;
  }

  bfs(0);

  rep(i, n) {
    cout << i+1 << " ";
    if (seen[i]) cout << d[i] << endl;
    else cout << -1 << endl;
  }



  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 100;
struct Graph{
    struct Edge{
        int to, w;
        Edge(int v, int _w):to(v), w(_w){}
    };
    vector<Edge> edge;
    vector<vector<int> > vertex;
    int V, E;
    Graph(int _V):V(_V),E(0){
        edge.clear(); vertex.clear();
        vertex.resize(V);
    }
    void addEdge(int u, int v, int w){
        vertex[u].push_back(E++);
        edge.push_back(Edge(v, w));
    }
};
void SPFA(Graph& G, LL* d, int s){
    bool inq[MAXN]; int cnt[MAXN];
    memset(cnt, 0, sizeof(int)*G.V);
    memset(d, INF, sizeof(LL)*G.V);
    memset(inq, false, sizeof(bool)*G.V);
    d[s] = 0; inq[s] = true;
    queue<int> q; q.push(s);
    while (q.size()){
        int p = q.front(); q.pop(); inq[p] = false;
        cnt[p]++; if (cnt[p] > G.V){ d[s] = -1; break;}
        for (auto index : G.vertex[p]){
            auto &e = G.edge[index];
            if (d[p] + e.w < d[e.to]){
                d[e.to] = d[p] + e.w;
                if (!inq[e.to]) inq[e.to] = true, q.push(e.to);
            }
        }
    }
}
int main(){
    int n;
    cin >> n;
    Graph G(n);
    LL d[MAXN];
    for (int i = 0 ; i < n ; i++){
        int m;
        cin >> m >> m;
        while (m--){
            int v;
            cin >> v;
            G.addEdge(i, v-1, 1);
        }
    }
    SPFA(G, d, 0);
    for (int i = 0 ; i < n ; i++)
        cout << i+1 << ' ' << d[i] << '\n';
}

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

using namespace std;
int V;
vector<int> G[102];
bool vis[102];
int d[102];
const int inf=0x3f3f3f;

void bfs(int s=1){
    d[s]=0;
    queue<int> que;
    que.push(s);
    while(!que.empty()){
        int t=que.front();
        que.pop();
        vis[t]=true;
        for(int i=0;i<G[t].size();i++){
            if(!vis[G[t][i]]){
                que.push(G[t][i]);
                d[G[t][i]]=min(d[G[t][i]],d[t]+1);
            }
        }
    }
}


int main(){
    cin>>V;
	int i;
	for(int i=1;i<=V;i++){
        d[i]=inf;
        vis[i]=false;
	}
    for(i=1;i<=V;i++){
        int id,k;
        cin>>id>>k;
        while(k--){
            int out;
            cin>>out;
            G[id].push_back(out);
        }
        sort(G[id].begin(),G[id].end());
    }
    bfs();
    for(int i=1;i<=V;i++){
        cout<<i<<" "<<(d[i]==inf?-1:d[i])<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

int main() {
  ll n;
  cin >> n;
  vector<set<ll>> v(n);
  for(ll i=0;i<n;i++) {
    ll f,k;
    cin >> f >> k;
    for(ll j=0;j<k;j++) {
      ll temp;
      cin >> temp;
      temp--;
      v[i].insert(temp);
    }
  }
  vector<ll> ans(n,-1);
  ans[0]=0;
  queue<ll> q;
  q.push(0);
  while(q.size()) {
    ll p=q.front();
    q.pop();
    for(ll t:v[p]) {
      if(ans[t]<0) {
        ans[t]=ans[p]+1;
        q.push(t);
      }
    }
  }
  for(ll i=0;i<n;i++) {
    cout << i+1 << " " << ans[i] << endl;
  }
}

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

using namespace std;

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

    vector<vector<int>> graph(n);

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

    vector<int> d(n, -1);
    queue<int> q;
    q.push(0);
    d[0] = 0;

    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(int i = 0; i < graph[cur].size(); i++ ){
            int next = graph[cur][i];
            if(d[next] == -1){
                d[next] = d[cur]+1;
                q.push(next);
            }
        }
    }

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

    return 0;
}