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 <iostream>
#include <queue>
#include <map>
#include <climits>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef pair<int,int> NODE;
#define pos first
#define depth second

map< int,vector<int> > info;
int bfs(int s,int d){
	queue<NODE> Q;
	map<int,bool>done;
	
	Q.push( make_pair(s,0) );
	while( !Q.empty() ){
		/* queueツづ個静ヲツ督ェツづ個ノツーツドツづーツ訪ツ姪「ツ催渉づ敖づ可つオツづowツ陛渉青板づ可禿シツづェツ、ツキツδーツつゥツづァツ偲ヲツづィツ渉慊つュ */
		NODE now = Q.front();
		Q.pop();
		done[now.pos] = true;

		if(now.pos == d){
			return now.depth;
		}

		for(int i=0;i<info[now.pos].size();i++){
			if( !done[info[now.pos][i]] ){
				Q.push( make_pair(info[now.pos][i], now.depth+1) );
			}
		}
	}
	return INT_MAX;
}

int main(){
	int n,r,k,t,p,s,d,v;
	
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> r >> k;
		for(int j=0;j<k;j++){
			cin >> t;
			info[r].push_back(t);
		}
	}	
	
	cin >> p;
	for(int i=0;i<p;i++){
		cin >> s >> d >> v;
		 
		if(v > bfs(s,d) )cout << bfs(s,d)+1 << endl;
		else cout << "NA" << endl;
	}
}

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

int md = 10000000;
vector <int> ro[100];
int s,d,v;
int s2,d2;
int kk[100],n;
bool flg[101] ;

void visit(int sn,int dis) {
  int x;
  
  flg[sn] = 1;
  
  if(sn == d2 && dis <=v){
    md = min(md,dis);
  }
  
  else if(dis <= v) {
    for(int i=0;i<ro[sn].size();i++) {
      for(int j=0;j<n;j++)
	if(ro[sn][i] == kk[j]) {
	  x = j;
	  break;
	}
      if(dis == 1){
	for(int i=0;i<=n;i++) flg[i] = 0;
	//	cout <<"reset"<<endl;
	flg[s2] = 1;
      }
      //       cout <<sn+1 << "to"<<x+1 << endl;
      if(flg[x] == 0) visit(x,dis+1);
    } 
  }
}
  
int main() {

  cin >> n;
  int a;
    
  for(int i=0;i<n;i++) {
    cin >> kk[i];
    cin >> a;
    ro[i].resize(a);
    for(int j=0;j<a;j++) cin >> ro[i][j];
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;

   
    for(int i=0;i<n;i++){
      if(kk[i] == s) s2 = i;
      if(kk[i] == d) d2 = i;
    }

    for(int i=0;i<=n;i++) flg[i] = 0;
    md = 10000000;
   
    visit(s2,1);
    if(md != 10000000) cout << md <<endl;
    else cout << "NA" <<endl;
  }
  
  return 0;
  
}

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

#define NONE INT_MAX>>1

vector< vector<int> > WarshallFloyd(vector< vector<int> >A)
{
  for(int k=0;k<A.size();k++)
    for(int i=0;i<A.size();i++)
      for(int j=0;j<A.size();j++)
        A[i][j]=min(A[i][j],A[i][k]+A[k][j]);
  return A;
}

int main()
{
  int n;
  int r,k,t;
  int p;
  int s,d,v;

  cin>>n;
  vector< vector<int> > m(n+1,vector<int>(n+1,NONE));
  for(int i=0;i<n;i++){
    cin>>r>>k;
    for(int j=0;j<k;j++){
      cin>>t;
      m[r][t]=1;
    }
  }
  m=WarshallFloyd(m);

  cin>>p;
  for(int i=0;i<p;i++){
    cin>>s>>d>>v;
    if(m[s][d]<v)cout<<m[s][d]+1<<endl;
    else puts("NA");
    }
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> dp(n, vector<int>(n, INF<int>));
    for (int i = 0; i < n; ++i) dp[i][i] = 0;
    for (int i = 0; i < n; ++i) {
        int r, k;
        cin >> r >> k;
        for (int j = 0; j < k; ++j) {
            int x;
            cin >> x;
            dp[r-1][x-1] = 1;
        }
    }
    for (int k = 0; k < n; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]);
            }
        }
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        a--; b--;
        if(dp[a][b] >= c) puts("NA");
        else cout << dp[a][b]+1 << "\n";
    }
    return 0;
}

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

#define pb push_back
#define fr first
#define sc second
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> pip; 
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int n, r, k, t;
int net[100][100];

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

int main() {

  cin>> n;
  for(int i=0; i<n; i++) {
    for(int j=0; j<n; j++) {
      net[i][j] = INF;
    }
  }

  for(int i=0; i<n; i++) {
    cin>> r>> k;  
    r--;
    for(int j=0; j<k; j++) {
      cin>> t;  
      t--;
      net[r][t] = 1;
    }
  }

  warshall_floyd();

  int p;
  cin>> p;
  for(int i=0; i<p; i++) {
    int s, d, v;
    cin>> s>> d>> v;
    s--; d--;
    if(net[s][d]+1>v) cout<< "NA"<< endl;
    else cout<< net[s][d]+1<< endl;
  }

}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define INF 1e9

int d[101][101];
int n;

void init(){
	for(int i = 0; i < 101; i++){
		for(int j = 0; j < 101; j++)	d[i][j] = INF;
	}
}

void solve(){
	for(int k = 1; k <= n; k++){
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++)	d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
		}
	}
}

int main(void){
	int np;
	init();
	cin >> n;
	for(int i = 0; i < n; i++){
		int v, e;
		cin >> v >> e;
		for(int j = 0; j < e; j++){
			int t;
			cin >> t;
			d[v][t] = 1;
		}
	}
	solve();
	cin >> np;
	for(int i = 0; i < np; i++){
		int s, g, ttl;
		cin >> s >> g >> ttl;
		if(d[s][g] != INF && d[s][g] + 1 <= ttl){
			printf("%d\n", d[s][g] + 1);
		}else{
			printf("NA\n");
		}
	}
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define MAX 10000000
using namespace std;

int main(){
  int n,m;
  int go[101][101];
  for(int i=0;i<101;i++){
    for(int j=0;j<101;j++) go[i][j] = MAX;
  }
  cin >> n;
  for(int i=0;i<n;i++){
    int r,k,tmp;
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> tmp;
      go[r][tmp] = 1;
    }
  }
  cin >> m;
  while(m--){
    int s,d,ttl;
    cin >> s >> d >> ttl;
    for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
	for(int k=1;k<=n;k++){
	  go[j][k] = min(go[j][k],go[j][i] + go[i][k]);
	}
      }
    }
    if(go[s][d]+1 > ttl) cout << "NA" << endl;
    else cout << go[s][d]+1 << endl;
  }
}