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 <stdio.h>
#include <queue>

using namespace std;

typedef struct {
  int *ks;
  int length;
} node;

node *nodes;

int dikstra(int s, int e, int deep){
  int c=0;
  queue<int> stk;
  stk.push(s);

  while(1){
    c++;
    if(c >= deep) return -1;
    int len = stk.size();
    for(int i=0;i<len;i++){

      int v = stk.front();
      stk.pop();

      for(int j=0;j<nodes[v].length;j++){
        if(nodes[v].ks[j]==e) return c+1;
        stk.push(nodes[v].ks[j]);
      }
    }
  }
}

int main(){
  int n;
  scanf("%d", &n);

  nodes = new node[n];

  int i,j;
  int t,u,v;
  for(i=0;i<n;i++){
    scanf("%d%d", &t,&u);
    nodes[t-1].ks = new int[u];
    nodes[t-1].length = u;
    for(j=0;j<u;j++){
      scanf("%d", &v);
      nodes[t-1].ks[j] = v-1;
    }
  }

  int m;
  scanf("%d", &m);
  for(i=0;i<m;i++){
    scanf("%d%d%d", &t, &u, &v);
    int r;
    if((r=dikstra(t-1, u-1,v)) != -1) printf("%d\n", r);
    else puts("NA");
  }
}

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

int main() {
  int n,INF=1<<28;
  cin >> n;
  int d[n+1][n+1];
  fill(d[0],d[n+1],INF);
  for(int i=1; i<=n; i++) d[i][i]=0;
  for(int i=0; i<n; i++) {
    int x,m;
    cin >> x >> m;
    for(int j=0; j<m; j++) {
      int y;
      cin >> y;
      d[x][y]=1;
    }
  }
  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 q;
  cin >> q;
  while(q--) {
    int x,y,z;
    cin >> x >> y >> z;
    if(d[x][y]+1<=z) cout << d[x][y]+1 << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<limits.h>
using namespace std;

int n;
int d[103][103];
void wf();

int main(){ 
  while(cin >> n)wf();
}




void wf(){
  int i1,m,J2;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(i == j)d[i][j]=0;
      d[i][j]=1000000000;//INT_MAXテつづつセテつづδつバテつグテつづδゥ
    }
  }

  for(int i=0;i<n;i++){
    cin >> i1 >> m;
    for(int j=0;j<m;j++){
      cin >> J2;
      d[i1-1][J2-1]=1;
    }
  }

  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]);
  int oh;

  cin >> oh;
  int st,ed,ttl;
  for(int i=0;i<oh;i++){
    cin >> st >> ed >> ttl;
    if(d[st-1][ed-1]+1 > ttl)cout << "NA" << endl;
    else cout << d[st-1][ed-1]+1 << endl;

  }/*
 for(int i=0;i<n;i++){
     for(int j=0;j<n;j++)cout << d[i][j] << " " ;
     cout << endl;
   }
   */

}

C++: //59
#include<iostream>
#include<vector>
#include<queue>
#include<utility>

using namespace std;

int main(){
  int n;
  cin>>n;
  vector<int> e[101];
  for(int i=0;i<n;i++){
    int r,k;
    cin>>r>>k;
    while(k--){
      int t;
      cin>>t;
      e[r].push_back(t);
    }
  }
  int p;
  cin>>p;
  while(p--){
    int s,d,v;
    cin>>s>>d>>v;
    queue<pair<int,int> > que;
    que.push(make_pair(1,s));
    bool p[101]={};
    while(!que.empty()){
      int c=que.front().second;
      if(c==d||que.front().first>v)break;
      if(!p[c]){
	for(int i=0;i<e[c].size();i++){
	  que.push(make_pair(que.front().first+1,e[c][i]));
	}
      }
      p[c]=true;
      que.pop();
    }
    if(que.empty()||que.front().first>v){
      cout<<"NA"<<endl;
    }else{
      cout<<que.front().first<<endl;
    }
  }
  return 0;
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
typedef long long ll;

int d[110][110];

int main(void) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            d[i][j] = INF;
        }
        d[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
        int r, k;
        cin >> r >> k;
        --r;
        int t;
        for (int j = 0; j < k; j++) {
            cin >> t;
            d[r][t-1] = 1;
        }
    }
    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]);
            }
        }
    }
    int p;
    cin >> p;
    for (int i = 0; i < p; i++) {
        int s, D, v;
        cin >> s >> D >> v;
        if (v > d[s-1][D-1]) {
            cout << d[s-1][D-1]+1 << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

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

int i, j, n, r, k, t, p, s, _d, v, ans;
int dic(int d[][100]) {
  int mi = s, f[100] = {};
  vector <int> cost(100, 100000000);
  cost[s] = 0; //テ・ツ、ツァテ、ツコツ?
  for(i = 0; i < n; i++) {
    f[mi] = 1;
    for(j = 0; j < n; j++)  if(d[mi][j] == 1 && !f[j] && cost[j] > cost[mi] + 1) cost[j] = cost[mi] + 1;
    int min = 200000000;
    for(j = 0; j < n; j++)  if(!f[j] && min > cost[j]) min = cost[mi = j];
  }
  return cost[_d];
}    

int main() {
  while(cin >> n) {
    int d[100][100] = {};
    for(i = 0; i < n; i++) {//
      cin >> r >> k;
      r--;
      while(k--) {
	cin >> t;
	t--;
	d[r][t] = 1;
      }
    }
    cin >> p;
    while(p--) {
      cin >> s >> _d >> v;
      s--; _d--;
      ans = dic(d) + 1;
      if(ans <= v)  cout << ans << endl;
      else cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<stack>
#include<vector>
#include <map>
#include <queue>
#include<string.h>
using namespace std;
typedef pair<int,int> P;

int main()
{
  int n;
  int node[100][100];
  cin>>n;
  for(int i=0;i<100;i++)
    for(int j=0;j<100;j++)
      node[i][j]=0;
  
  
  for(int x=0;x<n;x++)
    {
      int k,t;
      cin>>k>>t;
      for(int i=0;i<t;i++)
	{
	  int tmp;
	  cin>>tmp;
	  node[k][tmp]=1;
	  //	  cout<<node[k][tmp]<<endl;
	  //  cout<<"nodeadd:"<<k<<" "<<tmp<<endl;
	}
    }

  int p;
  cin>>p;
  for(;p--;)
    {
      int s,d,v;
      cin>>s>>d>>v;
      queue<P> qu;
      int ans=-1;
      for(int i=0;i<=n;i++)
	{
	  if(node[s][i]==1)
	    {
	      qu.push(P(i,2));
	    }
	}
      while(!qu.empty())
	{
	  P a = qu.front();
	  qu.pop();
	  if(a.second>v)
	    break;
	  if(a.first==d)
	    {
	      ans = a.second;
	      break;
	    }
	  for(int i=0;i<=n;i++)
	    {
	      if(node[a.first][i]==1)
		qu.push(P(i,a.second+1));    
	      
	    }
	}
      if(ans==-1)
	cout<<"NA"<<endl;
      else
	cout<<ans<<endl;
    }
}