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 <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#define INF (2000000000)
#define MAX (2000)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;


int n;

int head[100];
int to[10000];
int next[10000];

int d[100];

void Search(int now)
{
	fill(d, d + 100, INF);
	d[now] = 0;
	
	while (true){
		bool f = false;
		
		for (int i = 0; i < n; i++){
			for (int e = head[i]; e != -1; e = next[e]){
				if (d[i] != INF && d[to[e]] > d[i] + 1){
					d[to[e]] = d[i] + 1;
					f = true;
				}
			}
		}
		
		if (!f) break;
	}
}

int main()
{
	memset(head, -1, sizeof(head));
	
	int m = 0;
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++){
		int r, k;
		scanf("%d %d", &r, &k);
		r--;
		
		for (int j = 0; j < k; j++){
			int t;
			scanf("%d", &t);
			t--;
			
			next[m] = head[r];
			head[r] = m;
			to[m] = t;
			m++;
		}
	}
	
	int p;
	scanf("%d", &p);
	
	for (int i = 0; i < p; i++){
		int f, t, v;
		scanf("%d %d %d", &f, &t, &v);
		f--; t--;
		
		Search(f);
		
		if (d[t] + 1 <= v){
			printf("%d\n", d[t] + 1);
		}
		else {
			puts("NA");
		}
	}
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int main(){
	int n; cin>>n;
	vector<vector<int> > e;
	vector<int> id(n);
	FOR(i,n){
		cin>>id[i];
		int x; cin>>x;
		e.push_back(vector<int>(x));
		FOR(j,x) cin>>e[i][j];
	}

	map<int,int> mp;
	FOR(i,n) mp[id[i]] = i;
	FOR(i,n) FOR(j,sz(e[i])) e[i][j] = mp[e[i][j]];

	vector<vector<int> > dp(n,vector<int>(n,INF));
	FOR(i,n) FOR(j,sz(e[i])) dp[i][e[i][j]] = 1;

	FOR(k,n) FOR(i,n) FOR(j,n) dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);

	int m; cin>>m;
	while(m--){
		int s,t,tll; cin>>s>>t>>tll;
		s = mp[s],t = mp[t];
		int ans = dp[s][t] + 1;
		if(ans > tll) printf("NA");
		else printf("%d",ans);
		
		puts("");
	}

	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--){
    bool v[n+1];
    r(i,n+1)v[i]=0;
    cin>>s>>t>>p;p--;
    queue<P>q;
    q.push(make_pair(s,0));
    v[s]=1;
    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(!v[a[w.f][i]]&&w.S+1<=p){
          q.push(make_pair(a[w.f][i],w.S+1));
          v[a[w.f][i]]=1;
        }
      }     
    }
    cout<<"NA"<<endl;
    if(0)L:n++;
  }
}

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

using namespace std;

struct Packet{
	Packet(){}

	Packet(int arg_ttl,int loc){
		ttl = arg_ttl;
		for(int i=0; i < 101; i++)checkTable[i] = 0;
		checkTable[loc] = 1;
		current = loc;
	}

	void set(int loc){
		checkTable[loc] = 1;
		current = loc;
	}

	int ttl,checkTable[101],current;
};

int main(){

	int n,r,k,to,table[101][101],num_of_packet,start,goal,t_limit,memo[101][101];
	Packet tmp;

	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);

	queue<Packet> Q;
	bool FLG;

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

		FLG = false;

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

		for(int i = 1; i <= n; i++){
			if(i != start && table[start][i] == 1){
				if(memo[i][goal] > 0 && memo[i][goal] < t_limit){
					printf("%d\n",memo[i][goal]+1);
					memo[start][goal] = memo[i][goal]+1;
					FLG = true;
					break;
				}
				Packet p(t_limit-1,start);
				p.set(i);
				Q.push(p);
			}
		}



		while(!Q.empty() && FLG == false){
			tmp = Q.front();
			Q.pop();

			if(tmp.current == goal){
				printf("%d\n",t_limit - tmp.ttl+1);
				FLG = true;

				while(!Q.empty())Q.pop();

				memo[start][goal] = t_limit - tmp.ttl+1;

				break;
			}

			if(tmp.ttl > 1){
				for(int i=1; i <= n; i++){
					if(i != tmp.current && tmp.checkTable[i] == 0 && table[tmp.current][i] == 1){
						if(memo[i][goal] > 0 && (memo[i][goal]+1) < tmp.ttl){
							printf("%d\n",t_limit-tmp.ttl+memo[i][goal]+2);
							memo[start][goal] = t_limit-tmp.ttl+memo[i][goal]+2;
							FLG = true;
							while(!Q.empty())Q.pop();
							break;
						}

						Packet p = tmp;
						p.ttl--;
						p.set(i);
						Q.push(p);
					}
				}
			}
		}

		if(!FLG){
			printf("NA\n");
		}

	}

    return 0;
}

C++: #include <iostream>
#include <queue>
#include <climits>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main() {
	int n, p, r, k, t, s, d, v;
	cin >> n;
	int m[100][100] = {};
	rep(i,n) {
		cin >> r >> k;
		rep(j,k) {
			cin >> t;
			m[r-1][t-1] = 1;
		}
	}
	int ret[100][100] = {};
	rep(i,100) {
		ret[i][i] = 1;
	}
	rep(i,n) {
		queue<int> q;
		q.push(i);
		while(!q.empty()) {
			int a = q.front(); q.pop();
			rep(j,n) {
				if(m[a][j] && ret[i][j]==0) {
					q.push(j);
					ret[i][j] = ret[i][a]+1;
				}
			}
		}
	}
	cin >> p;
	rep(i,p) {
		cin >> s >> d >> v;
		--s,--d;
		if(ret[s][d] > v || ret[s][d] == 0) cout<< "NA" << endl;
		else cout << ret[s][d] << endl;
	}
	return 0;
}