Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)
const INF = 1<<31

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	arr := make([][]int, n)
	for i := range arr {
		arr[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			arr[i][j] = INF
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		x, k := input[0]-1, input[1]
		for j := 0; j < k; j++ {
			y := input[j+2]-1
			arr[x][y] = 1
		}
	}
	for k := 0; k < n; k++ {
		arr[k][k] = 0
		for i := 0; i < n; i++ {
			if arr[i][k] >= INF {
				continue
			}
			for j := 0; j < n; j++ {
				if arr[k][j] >= INF {
					continue
				}
				arr[i][j] = min(arr[i][j], arr[i][k]+arr[k][j])
			}
		}
	}
	scanner.Scan()
	p, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < p; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		s, d, v := input[0]-1, input[1]-1, input[2]
		if arr[s][d] < v {
			fmt.Println(arr[s][d]+1)
		} else {
			fmt.Println("NA")
		}
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include <cstdio>
#include <algorithm>

using namespace std;

#define INF (1<<29)
#define MAX 100

int main(){
  int n,v;
  int G[MAX][MAX];

  for(int i = 0 ; i < MAX ; i++){
    for(int j = 0 ; j < MAX ; j++){
      G[i][j] = INF;
    }
  }

  scanf("%d" ,&n);

  for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < n ; j++){
      G[i][j] = INF;
    }
  }
  
  int r,k,t;
  for(int i = 0 ; i < n ; i++){
    scanf("%d %d" ,&r ,&k); r--;
    while(k--){
      scanf("%d" ,&t); t--;
      G[r][t] = 1;
    }
  }

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

  int a,b,c;
  scanf("%d" ,&v);
  while(v--){
    scanf("%d %d %d" ,&a ,&b ,&c);
    a--,b--;
    if(G[a][b] == INF || G[a][b] + 1 > c){
      puts("NA");
    }else{
      printf("%d\n" ,G[a][b] + 1);
    }
  }

  return 0;
}

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  const int INF = 1 << 30;
  int n, dis[110][110];
  cin >> n;
  fill((int*)dis, (int*)(dis + 110), INF);
  for (int i = 1; i <= n; i++) {
    dis[i][i] = 0;
  }
  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      dis[r][t] = 1;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (dis[i][k] == INF || dis[k][j] == INF) continue;
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }
  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    if (dis[s][d] + 1 <= v) cout << dis[s][d] + 1 << endl;
    else cout << "NA" << endl;
  }

  return 0;
}

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

using namespace std;

typedef pair<int , int> pii;
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
const int INF = 1<<30;

int n ;
vector<int> way[120];

int mini(int s,int d,int v);

int main(void){
  cin>>n;
  rep(i,n){
    int r,k;
    cin>>r>>k;
    while(k--){
      int t;
      cin>>t;
      way[r].push_back(t);
    }
  }
  int p;
  cin>>p;
  
  while(p--){
    int s,d,v;
    cin>>s>>d>>v;
    int cost = mini(s,d,v);
    if(cost == INF) cout<< "NA" << endl;
    else cout<< cost << endl;
  }
  return 0;
  
  
}
int mini(int s, int d, int v)
{
    int diff[128];
    queue<pii> q;
     
    fill(diff, diff + n + 1, INF);
    q.push(pii(s, 1));
    diff[s] = 1;
    while (!q.empty()){
        pii p = q.front(); q.pop();
         
        if (diff[p.first] < p.second) continue;
        for (int i = 0; i < way[p.first].size(); i++){
            int tmp = way[p.first][i];
             
            if (diff[tmp] > diff[p.first] + 1){
                diff[tmp] = diff[p.first] + 1;
                q.push(pii(tmp, diff[tmp]));
            }
        }
    }
     
    if (diff[d] == INF || diff[d] > v) return INF;
    else return diff[d];
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = (1<<21);

class state{
public:
  int d,n;
  state(int d=0,int n=0):d(d),n(n){}
};

int main(){
  int n;
  while(cin >> n){
    bool d[100][100];
    for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j] = false;
    for(int i=0;i<n;i++){
      int a,b;
      cin >> a >> b;
      a--;
      for(int j=0;j<b;j++){
	int c;
	cin >> c;
	c--;
	d[a][c] = true;
      }
    }

    int m;
    cin >> m;
    while(m--){
      int a,b,c;
      queue<state> Q;
      bool f[100];
      for(int i=0;i<n;i++) f[i] = false;
      cin >> a >> b >> c;
      a--;
      b--;

      state u(a,1);
      int ans = INF;
      Q.push(u);

      while(!Q.empty()){
	u = Q.front();
	Q.pop();
	f[u.d] = true;

	if(u.d == b){
	  if(u.n <= c) ans = u.n;
	  break;
	}

	for(int i=0;i<n;i++){
	  if(!f[i] && d[u.d][i]){
	    state v = u;
	    v.d = i;
	    v.n++;
	    Q.push(v);
	  }
	}
      }

      if(ans == INF) cout << "NA" << endl;
      else cout << ans << endl;

    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define S second
#define f first
#define P pair<int,int>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
main(){
  int n,p,m,t,s;
  cin>>n;
  vector<int>a[n+1];
  r(i,n){cin>>p>>m;
    while(m--)cin>>t,a[p].push_back(t);
  }
  cin>>m;
  while(m--){
    cin>>s>>t>>p;p--;
    queue<P>q;
    q.push(make_pair(s,0));
    while(!q.empty()){
      P w=q.front();q.pop();
      if(w.f==t){cout<<w.S+1<<endl;goto L;}
      r(i,a[w.f].size()){
        if(w.S+1<=p)q.push(make_pair(a[w.f][i],w.S+1));
      }
    }
    cout<<"NA"<<endl;
    if(0)L:n++;
  }
}

C++: import java.util.*;

public class Main
{
    static final int inf = 99999;
    int[][] a;

    Main(){
	Scanner sc = new Scanner(System.in);
	int n, m, s, t;

	while(sc.hasNext()){
	    n = sc.nextInt();
	    a = new int[n+1][n+1];

	    for(int i = 0; i <= n; ++i)
		for(int j = i+1; j <= n; ++j)
		    a[i][j] = a[j][i] = inf;

	    for(int i = 0; i < n; ++i){
		s = sc.nextInt();
		m = sc.nextInt();
		for(int j = 0; j < m; ++j){
		    t = sc.nextInt();
		    a[s][t] = 1;
		}
	    }

	    for(int k = 1; k <= n; ++k)
		for(int i = 1; i <= n; ++i)
		    for(int j = 1; j <= n; ++j)
			a[i][j] = Math.min(a[i][j], a[i][k]+a[k][j]);

	    n = sc.nextInt();
	    for(int i = 0; i < n; ++i){
		s = sc.nextInt();
		t = sc.nextInt();
		m = sc.nextInt();
		if(a[s][t] < u)
		    System.out.println(Integer.toString(a[s][t]+1));
		else
		    System.out.println("NA");
	    }
	}
    }

    public static void main(String[] args){
	new Main();
    }
}

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

vector <int> ro[101],dis;
queue <int> Q;
int n,s,d,v;
int r,kk;

void reset() {
  dis.resize(n);
  for(int i=0;i<n;i++) 
    dis[i] = 100000000;
}


int bfs() {
  Q.push(s);
  dis[s] = 1;
  int t,a;
  while(!Q.empty()) {
    t = Q.front();
    Q.pop();
   
    for(int i=0;i<ro[t].size();i++) {
      a = ro[t][i];
      if(dis[a] > dis[t]+1 ){
	dis[a] = dis[t]+1;
	if(dis[t]+1 < v && a != d) Q.push(a);
      }
    }
  }
  return dis[d];
}

int main() {
  
  cin >> n;
  int a;
  for(int i=0;i<n;i++) {
    cin >> r >> kk;
    ro[r-1].resize(kk);
    for(int j=0;j<kk;j++) {
      cin >> a;
      ro[r-1][j] = a - 1;
    }
  }
  
  int p;
  int ans;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;
    s--;
    d--;
    reset();
    ans = bfs();
    if(ans <= v) cout << ans << endl;
    else cout <<"NA" << endl;
    
  }
  
  return 0;
  

}