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 <vector>
#include <queue>
#include <climits>
#define REP(i,n) for(int i=0;i<n;++i)

//ダイグスとらで解くそうです

using namespace std;

typedef pair<int,int> DisNum;

vector<int> canto;

int Serch(vector<DisNum> canConnect[102],int from,int to,int hp)
{
	if(from == to)
		return -1;
	int distance[102] = {};//距離を保存
	REP(i,102) distance[i] = INT_MAX;
	priority_queue<DisNum,vector<DisNum>,greater<DisNum> > qu;//行き先のストックをキュー
	qu.push(DisNum(0,from));
	distance[from] = 0;
	while(!qu.empty())
	{
		DisNum nowfrom;
		nowfrom = qu.top();qu.pop();
		if(distance[nowfrom.second] != nowfrom.first) continue;
		if(nowfrom.second == to) return nowfrom.first;
		for(int i=0;i<canConnect[nowfrom.second].size();++i)
		{
			int v = canConnect[nowfrom.second][i].second;//今から調べる先の座標
			int n = nowfrom.second;
			if(distance[v] > distance[n] + canConnect[nowfrom.second][i].first)
			{
				distance[v] = distance[n] + canConnect[nowfrom.second][i].first;
				qu.push(DisNum(distance[v],v));
			}
		}
	}
}

int main(void)
{
	int maxn;
	canto.clear();
	vector<DisNum> canConnect[102];
	cin >> maxn;
	for(int i=0;i<maxn;++i)
	{
		int r,k;
		cin >> r >> k;
		for(int j=0;j<k;++j)
		{
			int t;
			cin >> t;
			canConnect[r].push_back(DisNum(1,t));
		}
	}
	int l;
	cin >> l;
	for(int j=0;j<l;++j)
	{
		int s,d,v;
		cin >> s >> d >> v;
		int ans = Serch(canConnect,s,d,v) + 1;
		if(ans <= v)
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

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

using namespace std;

int main()
{
	int n, a[101][101], s, t, u;
	
	while(cin >> n){
		fill(&a[0][0], &a[n][n], 99999);
		
		for(int i = 0; i <= n; i++){
			a[i][i] = 1;
		}
		for(int i = 0; i < n; i++){
			cin >> s >> t;
			while(t--){
				cin >> u;
				a[s][u] = 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] = (a[i][j] > a[i][k]+a[k][j])? a[i][k]+a[k][j] : a[i][j];
				}
			}
		}
		
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				cout << a[i][j] << " ";
			}
			cout << endl;
		}
		
		cin >> n;
		while(n--){
			cin >> s >> t >> u;
			if(a[s][t]+1 <= u){
				cout << a[s][t]+1 << endl;
			} else {
				cout << "NA" << endl;
			}
		}
	}
	
	return 0;
}

C++: #include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int N_MAX = 100;
const int INF = 0x7fffff;

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];
int visited[N_MAX + 1];

void bfs(int src, int dst, int limit) {
	queue<Node> que;
	fill(visited, visited + n + 1, INF);
	que.push(Node(src, limit - 1));
	while (!que.empty()) {
		Node current = que.front();
		que.pop();
		if (current.pos == dst) {
			cout << limit - current.ttl << endl;
			return;
		}
		if (current.ttl >= 0 && visited[current.pos] <= current.ttl) continue;
		visited[current.pos] = current.ttl;
		if (current.ttl == 0) continue;
		for (int next = 1; next <= n; next++) {
			if (adj[current.pos][next] && visited[next] > current.ttl && current.ttl > 0) {
				que.push(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 + 1, 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 <vector>
#include <queue>
#include <algorithm>
#include <functional>

using namespace std;
typedef pair<int, int> P;
static const int INF = 99999999;
struct SEdge
{
	int To;
	int Cost;
};
vector<int> d;
vector< vector<SEdge> > G;

void Dijkstra(int s)
{
	fill(d.begin(), d.end(), INF);
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P> > PQue;
	PQue.push(P(0, s));
	while(!PQue.empty())
	{
		P p = PQue.top();
		PQue.pop();
		int v = p.second;
		if(d[v] < p.first)
		{
			continue;
		}
		for(int i = 0; i < G[v].size(); ++i)
		{
			SEdge e = G[v][i];
			if(d[e.To] > d[v] + e.Cost)
			{
				d[e.To] = d[v] + e.Cost;
				PQue.push(P(d[e.To], e.To));
			}
		}
	}
}

void solve()
{
	int n;
	cin >> n;
	d.resize(n);
	G.resize(n);
	for(int i = 0; i < n; ++i)
	{
		int From, To_Size;
		cin >> From >> To_Size;
		--From;
		vector<int> Vec(To_Size);
		for(int j = 0; j < To_Size; ++j)
		{
			cin >> Vec[j];
			--Vec[j];
			SEdge e;
			e.To = Vec[j];
			e.Cost = 1;
			G[From].push_back(e);
		}
	}
	int p;
	cin >> p;
	for(int i = 0; i < p; ++i)
	{
		int s, g, vp;
		cin >> s >> g >> vp;
		--s;
		--g;
		Dijkstra(s);
		if(d[g] + 1 <= vp)
		{
			cout << d[g] + 1 << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int d[200][200];
int main(){
    int n;
    cin>>n;
    rep(i,200)rep(j,200)d[i][j]=1<<29;
    rep(i,200)d[i][i]=0;
    rep(i,n){
        int src,k;
        cin>>src>>k;
        src--;
        rep(j,k){
            int dst;cin>>dst;
            dst--;
            d[src][dst] = 1;
        }
    }

    rep(k,n)rep(i,n)rep(j,n)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    int p;cin>>p;
    rep(i,p){
        int s,t,ttl;
        cin>>s>>t>>ttl;--s,--t;
        if(d[s][t] >= ttl) puts("NA");
        else printf("%d\n", d[s][t]+1);
    }
}

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

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


void visit(int sn,int dis) {
  //  for(int i=1;i<=n;i++)cout << flg[i];
  // cout <<" ";
  
  flg[sn+1] = 1;
  
  if(sn == d-1 && dis <=v)  md = min(md,dis);
  else if(dis < v && dis < md) {
    for(int i=0;i<ro[sn].size();i++) {
      
      if(flg[ro[sn][i]] == 0){
	//	cout <<sn+1 << "to"<<ro[sn][i] << endl;
	visit(ro[sn][i]-1,dis+1);
	flg[ro[sn][i]] = 0;
      }
    } 
  }
}
  
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[kk[i]-1][j];
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;


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