Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

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

func (q *Queue) pop() Pair {
	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([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

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

	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<vector>
#include<stack>
#include<cstdio>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;
int n ;
vector<int> G[MAX];
int color [MAX];
void dfs (int r,int c)
{
  stack <int> S;
  S.push(r);
  color[r] = c;
  while(!S.empty())
  {
    int u = S.top();
    S.pop();
    for(int i = 0;i<G[u].size();i++)
        {
          int v = G[u][i];
          if(color[v] == NIL)
          {
            color[v] = c;
            S.push(v);
          }
        }
  }
}
void ass()
{
  int id = 1;
  for(int i = 0;i<n;i++)
    color[i] = NIL;
  for(int u = 0;u<n;u++)
  {
    if(color[u] == NIL)
    dfs(u,id++);
  }
}
int main ()
{
  int s,t,m,q;
  cin>>n>>m;
  for(int i = 0;i<m;i++)
    {
      cin>>s>>t;
      G[s].push_back(t);
      G[t].push_back(s);
    }
  ass();
  cin>>q;
  for(int i = 0;i<q;i++)
  {
    cin>>s>>t;
    if(color[s] == color[t])
    {
      cout<<"yes"<<endl;
    }
    else cout<<"no"<<endl;
  }
  return 0 ;
}

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

typedef struct{
  int A[1000];
  int ans;
  bool c;
}node;
node T[1000];
int Q[100000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    for(int i=2;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	}
	Q[tail++]=i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    num=Q[head++];
    //  cout<<head<<" "<<tail<<endl;
  }

}

int main(){
  int num;
  int son;
  int z;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    for(int j=0;j<son;j++){
      cin>>z;
      T[num].A[z]=1;
      }
  }
  Q[0]=1;
  function(1);
  T[1].ans=0;
  for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
  }

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


  return 0;
}

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

struct node {
	std::vector<int> child;
	int distance;
};

int main() {
	int node_num;

	std::cin >> node_num;

	std::vector<node> n(node_num);

	for (int i = 0; i < node_num; i++) {
		int num, to, node_val;
		std::cin >> node_val >> num;
		for (int j = 0; j < num; j++) {
			std::cin >> to;
			n[node_val - 1].child.push_back(to);
		}
		n[i].distance = -1;
	}

	//???????????????????????¢0
	n[0].distance = 0;
	std::queue<int> q;
	int dist = 1;
	q.push(1);
	while(q.size() != 0){
		int num = q.front();
		q.pop();
		for (std::vector<int>::iterator it = n[num - 1].child.begin(); it != n[num - 1].child.end(); it++) {
			if (n[*it - 1].distance > n[num - 1].distance + 1|| n[*it - 1].distance == -1) {
				n[*it - 1].distance = n[num - 1].distance + 1;
				q.push(*it);
			}
		}
	}

	for (int i = 0; i < node_num; i++) {
		std::cout << i + 1 << " " << n[i].distance << std::endl;
	}
	return 0;
}

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

int main() {
    int N;
    cin>>N;
    vector<vector<int> > graph(N);
    int u, k, tmp;
    for(int i=0;i<N;++i){
        cin>>u>>k;
        for(int j=0;j<k;++j){
            cin>>tmp;
            tmp--;
            graph[i].push_back(tmp);
        }
    }
    queue<int> que;
    queue<int> passque;
    que.push(0);
    passque.push(0);
    int node, pass;
    vector<int> ans(N);
    for(int i=0;i<N;++i){
        ans[i] = -1;
    }
    map<int, int> past;
    past[0]++;
    while(!que.empty()){
        node = que.front();que.pop();
        pass = passque.front();passque.pop();
        ans[node] = pass;
        for(int i=0;i<graph[node].size();++i){
            if(past[graph[node][i]]==0){
                que.push(graph[node][i]);
                passque.push(pass + 1);
            }
            past[graph[node][i]]++;
        }
    }
    for(int i=0;i<N;++i){
        cout<<i+1<<" "<<ans[i]<<endl;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef long long ll;
#define range(i,a,b) for(int i=a;i<b;++i)
#define rep(i,n) range(i,0,n)
#define INF 10000000000 //10^10
#define PI 3.141592
#define pb push_back

int main(){
    //内部処理は0indexで行う

    //入力
    int n;
    cin >> n;
    vector<vint> adj(n);
    rep(i,n){
        int u,k;
        cin >> u >> k;
        rep(j,k){
            int v;
            cin >> v;
            adj[u-1].pb(v-1);
        }
    }

    //計算
    vint d(n,-1);
    d[0]=0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int cur=Q.front(); Q.pop();
        rep(i,adj[cur].size()){
            int dst=adj[cur][i];
            if(d[dst]==-1){
                d[dst]=d[cur]+1;
                Q.push(dst);
            }
        }
    }

    //出力
    rep(i,n){
        cout << i+1 << ' ' << d[i] << endl;
    }
}

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

static const int N=100;
static const int INF=(1<<21);

int n,g[N][N];
int d[N];

void bfs(int s) {
    queue<int> q;
    q.push(s);
    for(int i=0;i<n;i++) d[i]=INF;
    d[s]=0;
    int u;
    while(!q.empty()) {
        u=q.front();q.pop();
        for(int v=0;v<n;v++) {
            if(g[u][v]==0) continue;
            if (d[v] != INF) continue;
            d[v]=d[u]+1;
            q.push(v); 
        }
    }
    for(int i=0;i<n;i++) {
        cout<<i+1<<" "<<((d[i]==INF) ? (-1) : d[i] )<<endl;
    }
}

int main(void) {
    int u,v,k;
    cin>>n;
    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) g[i][j]=0;
    }
    for(int i=0;i<n;i++) {
            cin>>u>>k;
            u--;
        for(int j=0;j<k;j++) {
            int v;cin>>v;
            v--;
            g[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}