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 <cstdio>
#include <algorithm>

using namespace std;

const int inf = 1 << 28;
int n, m;
int d[102][102];

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      d[i][j] = inf;
    }
  }
  for (int i = 0; i < n; ++i) {
    int r, k, t;
    cin >> r >> k;
    r--;
    for (int j = 0; j < k; ++j) {
      cin >> t;
      t--;
      d[r][t] = 1;
    }
  }
  
  for (int i = 0; i < n; ++i) d[i][i] = 0;
  
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }

  cin >> m;
  for (int i = 0; i < m; ++i) {
    int s, de, v;
    cin >> s >> de >> v;
    s--; de--;
    //printf("(%d,%d) ", s, de);
    if (d[s][de]+1 <= v) {
      cout << d[s][de]+1 << endl;
    } else {
      //cout << d[s][de] << endl;
      cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;



int main(){

	int n,r,k,to,table[101][101],num_of_packet,start,goal,t_limit,memo[101][101],P[101],D[101],num,min,min_index,current;

	for(int i=1; i < 101; i++){
		for(int k=1; k < 101; k++){
			table[i][k] = 0;
			memo[i][k] = 0;
		}
	}

	scanf("%d",&n);

	for(int i = 0; i < n; i++){
		scanf("%d %d",&r,&k);
		for(int p=0;p < k; p++){
			scanf("%d",&to);
			table[r][to] = 1;
		}
	}

	scanf("%d",&num_of_packet);

	bool FLG;

	for(int i=0; i < num_of_packet; i++){
		scanf("%d %d %d",&start,&goal,&t_limit);

		FLG = false;

		if(t_limit <= 1){
			printf("NA\n");
			continue;
		}

		if(memo[start][goal] != 0){
			if(memo[start][goal] <=  t_limit){
				printf("%d\n",memo[start][goal]);
			}else{
				printf("NA\n");
			}
			continue;
		}

		for(int index=1; index <= n; index++){
			P[index] = 0;
			D[index] = 1000000000;
		}

		P[start] = 1;
		num = 1;

		for(int index = 1; index <= n; index++){
			if(index != start && table[start][index] == 1){
				D[index] = 1;
			}
		}

		if(FLG)continue;

		while(num < n){
			min = 1000000000;
			for(int index=1;index <= n; index++){
				if(P[index] == 0 && D[index] < min){
					min = D[index];
					min_index =index;
				}
			}

			if(min == 1000000000)break;

			P[min_index] = 1;
			current = min_index;
			num++;
			for(int i=1; i <= n; i++){
				if(table[current][i] == 1){
					D[i] = (D[i] <= (D[current]+1))?D[i]:(D[current]+1);
				}
			}

		}

		if(D[goal] >= t_limit){
			printf("NA\n");
		}else{
			memo[start][goal] = D[goal]+1;
			printf("%d\n",D[goal]+1);
		}

	}

    return 0;
}

C++: #include <deque>
#include <iostream>
#include <algorithm>
using namespace std;
const int N_MAX = 100;

class Node {
public:
	int pos, ttl;
	Node(int p, int t) : pos(p), ttl(t) {}
};

int n;
// adj[from][to] from->toの辺があるか
bool adj[N_MAX + 1][N_MAX + 1];

void bfs(int src, int dst, int limit) {
	deque<Node> que;
	que.push_back(Node(src, limit - 1));
	while (!que.empty()) {
		Node current = que.front();
		que.pop_front();
		if (current.pos == dst) {
			cout << limit - current.ttl << endl;
			return;
		}
		if (current.ttl == 0) continue;
		for (int next = 1; next <= n; next++) {
			if (adj[current.pos][next]) {
				que.push_back(Node(next, current.ttl - 1));
			}
		}
	}
	cout << "NA" << endl;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int r, k;
		cin >> r >> k;
		fill(adj[r], adj[r] + n, false);
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			adj[r][t] = true;
		}
	}
	int p;
	cin >> p;
	for (int i = 0; i < p; i++) {
		int s, d, v;
		cin >> s >> d >> v;
		bfs(s, d, v);
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;

int main()
{
	int n; cin>>n;
	vvi dp(n,vi(n,INFTY));
	rep(i,n)
		dp[i][i]=0;
	rep(i,n){
		int r,k; cin>>r>>k;
		rep(j,k){
			int t; cin>>t;
			dp[r-1][t-1]=1;
		}
	}
	
	rep(k,n) rep(i,n) rep(j,n)
		dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
	
	int p; cin>>p;
	rep(i,p){
		int s,d,v; cin>>s>>d>>v;
		if(dp[s-1][d-1]<v)
			cout<<dp[s-1][d-1]+1<<endl;
		else
			cout<<"NA"<<endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int net[103][103],n,maxx=0;
typedef pair<int,int> BVB;
int ans(int,int,int);

int main(){
  int num,n1,th,pkn,st,ed,c;

  cin >> n;

  for(int i=0;i<n;i++){ 
    
    cin >> num >> n1;
    if(num >= maxx)maxx = num;

    for(int j=0;j<n1;j++){
      cin >> th;
      net[num][th]=1;
    }

  }
  
  for(int m=0;m<maxx;m++)for(int n=0;n<maxx;n++)if(net[m][n]!=1)net[m][n]=0;

  cin >> pkn;

  for(int i=0;i<pkn;i++){
    cin >> st >> ed >> c;


    if(ans(st,ed,c) != -2)cout << ans(st,ed,c) << endl;
    else cout << "NA" << endl;

  }

}


int ans(int st,int ed,int c){
  queue<BVB> Nari;
  int yes=10001;
  BVB next,fr;

  fr.first=st;
  fr.second=c;
  Nari.push(fr);
  if(st == ed && c != 0)return 1;


  while(1){
    next=Nari.front();
   
    Nari.pop();
    next.second=next.second-1;

    if(fr.second == (next.second+1) && next.second == 0 && next.first != ed)return -2;

    if(next.second < 0)break; 
    
    for(int i=1;i<=maxx;i++){
      
      if(net[next.first][i] == 1 && i == ed ){

	if(yes >  (c-next.second+1))yes = (c - next.second)+1;
	
	
      }
            
      if(net[next.first][i] == 1){
	Nari.push(BVB(i,next.second));

      }
      
    }
       
  }
  if(yes != 10001){return yes;}
  else return -2; 
}