Problem:
パケット転送
インターネットでは、データはパケットに分割され、パケットごとにルータと呼ばれる中継機器を介して宛先に転送されます。各ルータはパケットに記載された宛先から次に転送すべきルータを判断します。さらに、無限にルータ間を転送され続けることを防ぐため、パケットには TTL（Time To Live） という値が付加されています。ルータは受け取ったパケットの TTL を 1 減算し、その結果が 0 ならそのパケットを破棄し、それ以外なら次のルータに転送します。
そこで、ネットワークの設計を手助けするプログラムを作ることになりました。ネットワークの接続情報と送信パケットの情報を入力として、各パケットが宛先ルータに到着するまでに経由するルータの数のうち最小の値を表示するプログラムを作成してください。
ネットワークは図のように複数のルータとそれらを結ぶケーブルで構成されています。ただし、各接続（ケーブル）は単方向であることに注意してください。各ルータが直接つながっているルータの番号の配列がネットワークの接続の情報として与えられます。ルータの数を
n
とすれば、各ルータは 1 から
n
までの整数で識別されます。送信元から宛先ルータまでの経路が複数ある場合は、経由するルータの数が少ない方の値を出力してください。また、パケットが宛先に到達しない場合は NA と出力してください。
例えば、以下の図のようなネットワークで、送信元ルータが 6、宛先ルータが 5 の場合を考えます。最短経路は 6→1→5 であり経由するルータは 3 個です。この場合、TTL はルータ 6、1 でそれぞれ減算されるので、送信時の TTL が 3 以上であればパケットは到達できます。宛先ルータでは TTL を減算する必要はありません。また、送信元と宛先が同じルータになるようなパケットは無いものとします。
Input
入力は以下の形式で与えられます。
n
r
1
k
1
t
11
t
12
...
t
1k
1
r
2
k
2
t
21
t
22
...
t
2k
2
:
r
n
k
n
t
n1
t
n2
...
t
nk
n
p
s
1
d
1
v
1
s
2
d
2
v
2
:
s
p
d
p
v
p
1 行目にルータの総数
n
（
n
≤ 100）、続く
n
行に
i
番目のルータの接続情報が与えられます。接続情報として、
i
番目のルータの番号
r
i
、
i
番目のルータと直接接続しているルータの個数
k
i
、
i
番目のルータから送信できるルータの番号
t
i1
,
t
i2
, ...
t
ik
i
が与えられます。
続く行にパケットの個数
p
（
p
≤ 1000）、続く
p
行に
i
番目のパケットの情報が与えられます。パケットの情報として、 送信元ルータの番号
s
i
, 宛先ルータの番号
d
i
, TTL の値
v
i
(0 ≤
v
i
≤ 10000) が与えられます。
Output
各パケットごとに、経由するルータの個数または NA を１行に出力してください。
Sample Input
7
1 4 2 5 4 3
2 1 5
3 1 6
4 1 7
5 2 7 6
6 1 1
7 0
6
1 2 2
1 5 3
1 2 1
5 1 3
6 3 3
1 7 4
Output for the Sample Input
2
2
NA
3
3
3

Submitted solutions:

code_snippet: #include<iostream>
#include<map>
#include<vector>
#include<queue>
#include<set>
using namespace std;

int main(){
    int n, r, k, t;
    cin >> n;

    map<int, vector<int>> path;
    for(int i = 0; i < n; i++){
        cin >> r >> k;
        for(int j = 0; j < k; j++){
            cin >> t;
            path[r].push_back(t);
        }
    }

    int p, s, d, v;
    cin >> p;
    for(int i = 0; i < p; i++){
        cin >> s >> d >> v;
        queue<pair<int, int>> q;
        set<int> appear;

        q.push({s,1});

        int ans = -1;
        while(!q.empty()){
            pair<int,int> p = q.front();    q.pop();
            if(appear.find(p.first) != appear.end())   continue;
            
            if(p.first == d){
                ans = p.second;
                break;
            }

            appear.insert(p.first);

            for(int j = 0; j < path[p.first].size(); j++){
                if(appear.find(path[p.first][j]) == appear.end()){
                    q.push({path[p.first][j], p.second+1});
                }
            }
        }

        if(ans == -1 || ans > v){
            cout << "NA" << endl;
        }else{
            cout << ans << endl;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3180
final_rank: 15

code_snippet: #include <cstdio>
#include <vector>
#include <utility>
using namespace std;

#define INF 10000000

int n,r,k,t,p,s,d,v;
vector<pair<int,vector<int> > > vec;

int find(int num){
	for(int i = 0; i < n; i++){
		if(vec[i].first == num){
			return i;
		}
	}
	return -1;
}

int sch(int st, int ed){
	int from = find(st);
	int to = find(ed);
	vector<int> vv = vector<int>(n,INF);
	vv[from] = 0;
	for(int i = 1; i <= n; i++){
		for(int vi = 0; vi < n; vi++){
			if(vv[vi] == i - 1){
				for(int j = 0; j < vec[vi].second.size(); j++){
					int tmp = find(vec[vi].second[j]);
					if(vv[tmp] == INF){
						vv[tmp] = i;
					}
				}
			}
		}
	}
	return vv[to];
}

int main(){
	scanf("%d",&n);
	vec = vector<pair<int,vector<int> > >(n);
	for(int i = 0; i < n; i++){
		scanf("%d%d",&r,&k);
		vec[i].first = r;
		vec[i].second = vector<int>(k);
		for(int j = 0; j < k; j++){
			scanf("%d",&t);
			vec[i].second[j] = t;
		}
	}
	scanf("%d",&p);
	for(int i = 0; i < p; i++){
		scanf("%d%d%d",&s,&d,&v);
		int l = sch(s,d);
		l += 1;
		if(l <= v){
			printf("%d\n",l);
		}
		else{
			printf("NA\n");
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2748
final_rank: 5

code_snippet: /* ????????????????????????????????????????????? */
#include <stdio.h>

/* ?????°???????????? */
typedef	void			VD;
typedef	signed char		I1;
typedef unsigned char 	U1;
typedef	signed short	I2;
typedef unsigned short 	U2;
typedef	signed int		I4;
typedef unsigned int 	U4;

/* ?????°?????? */
#define MAX_ROUTER 100
#define MAX_TTL 10000

#define CONNECT 1
#define DISCONNECT 0
#define SENTINEL -1

#define TRUE 1
#define FALSE 0

#define INPUT_SUCCESS 0
#define INPUT_ERORR -1

/* ????????????????????£?¨? */
VD setInit(VD);
I1 inputConnectInfo(VD);
I1 inputTransmitPacket(VD);
I4 CalcSumRouter(I4 i4Sender,I4 i4Destination,I4 i4TTL);
/* ??°?????????????????° */
I1 u1Connect[MAX_ROUTER+1][MAX_ROUTER+1];
I4 i4RouterNum;

/* main??? */
I4 main(VD)
{
	I1 i1ErrorCheck;
	I4 i4CalcNum;
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	
	setInit();
	i1ErrorCheck = inputConnectInfo();
	if(i1ErrorCheck == INPUT_SUCCESS){
		scanf("%d",&i4CalcNum);
		for( i4RoopCnt1=0; i4RoopCnt1<i4CalcNum; i4RoopCnt1++)
		{
			inputTransmitPacket();
		}
	}
	else{
		;
	}
	return 0;
}

/* ???????????¢??° */
VD setInit(VD)
{
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	
	for( i4RoopCnt1=1; i4RoopCnt1<=MAX_ROUTER; i4RoopCnt1++)
	{
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++)
		{
			u1Connect[i4RoopCnt1][i4RoopCnt2] = DISCONNECT;
		}
	}
	return;
}

/* ??????????????\?¶??????±??\?????¢??° */
I1 inputConnectInfo(VD)
{
	I1 i1Return;
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	I4 i4RouterCnt;
	I4 i4ConnectNum;
	I4 i4AccessPoint;
	
	i1Return = INPUT_SUCCESS;
	
	scanf("%d",&i4RouterNum);
	if(i4RouterNum <= (I4)MAX_ROUTER){
		for( i4RoopCnt1=1; i4RoopCnt1<=i4RouterNum; i4RoopCnt1++){
			scanf("%d%d",&i4RouterCnt,&i4ConnectNum);
			for( i4RoopCnt2=0; i4RoopCnt2<i4ConnectNum; i4RoopCnt2++){
				scanf("%d",&i4AccessPoint);
				u1Connect[i4RouterCnt][i4AccessPoint] = CONNECT;
			}
			u1Connect[i4RouterCnt][i4RouterNum+1] = SENTINEL;
		}
	}
	else{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

/* ???????????±???????????±??\?????¢??° */
I1 inputTransmitPacket(VD)
{
	I1 i1Return;
	I4 i4StartRouter;
	I4 i4GoalRouter;
	I4 i4TTL;
	I4 i4Ans;
	
	i1Return = INPUT_SUCCESS;
	
	scanf("%d%d%d",&i4StartRouter,&i4GoalRouter,&i4TTL);
	if(i4TTL <= MAX_TTL){
		i4Ans = CalcSumRouter(i4StartRouter,i4GoalRouter,i4TTL);
		if(i4Ans > 0){
			printf("%d\n",i4Ans);
		}
		else{
			printf("NA\n");
		}
	}
	else{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

/* ??????????????????????¨????????????°?¨??????¢??° */
I4 CalcSumRouter(I4 i4Sender,I4 i4Destination,I4 i4TTL)
{
	/* ?????°??£?¨? */
	I1 i1StarTable[MAX_ROUTER+1][MAX_ROUTER+1];	/* ????????¨ */
	I4 i4MinVal[MAX_ROUTER+1];
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	I4 i4RoopCnt3;
	I4 i4RoopMax;
	
	/* ????????? */
	for( i4RoopCnt1=1; i4RoopCnt1<=i4RouterNum; i4RoopCnt1++){
		i4MinVal[i4RoopCnt1] = 0;
		for(i4RoopCnt2=1; i4RoopCnt2<=i4RouterNum; i4RoopCnt2++){
			i1StarTable[i4RoopCnt1][i4RoopCnt2] = FALSE;
		}
	}
	
	if(i4TTL<=i4RouterNum){
		i4RoopMax = i4TTL;
	}
	else{
		i4RoopMax = i4RouterNum;
	}
	
	i1StarTable[1][i4Sender] = TRUE;
	
	for(i4RoopCnt1=1; i4RoopCnt1<i4RoopMax; i4RoopCnt1++){
		for(i4RoopCnt2=1; i4RoopCnt2<=i4RouterNum; i4RoopCnt2++){
			if(i1StarTable[i4RoopCnt1][i4RoopCnt2] == TRUE){
				for(i4RoopCnt3=1; i4RoopCnt3<=i4RouterNum; i4RoopCnt3++){
					if(u1Connect[i4RoopCnt2][i4RoopCnt3] == CONNECT){
						if(i4RoopCnt3 == i4Destination){
							return i4RoopCnt1+1;
						}
						else{
							;
						}
						i1StarTable[i4RoopCnt1+1][i4RoopCnt3] = TRUE;
					}
					
				}
			}
			else{
				;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 2696
final_rank: 8

code_snippet: /* ????????????????????????????????????????????? */
#include <stdio.h>

/* ?????°???????????? */
typedef	void			VD;
typedef	signed char		I1;
typedef unsigned char 	U1;
typedef	signed short	I2;
typedef unsigned short 	U2;
typedef	signed int		I4;
typedef unsigned int 	U4;

/* ?????°?????? */
#define MAX_ROUTER 100
#define MAX_TTL 10000

#define CONNECT 1
#define DISCONNECT 0
#define SENTINEL -1

#define TRUE 1
#define FALSE 0

#define INPUT_SUCCESS 0
#define INPUT_ERORR -1

/* ????????????????????£?¨? */
VD setInit(VD);
I1 inputConnectInfo(VD);
I1 inputTransmitPacket(VD);
I4 CalcDijikstra(I4 i4Sender,I4 i4Destination,I4 i4TTL);
/* ??°?????????????????° */
I1 u1Connect[MAX_ROUTER+1][MAX_ROUTER+1];
I4 i4RouterNum;

I4 main(VD)
{
	I1 i1ErrorCheck;
	I4 i4CalcNum;
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	
	setInit();
	i1ErrorCheck = inputConnectInfo();
	if(i1ErrorCheck == INPUT_SUCCESS){
		scanf("%d",&i4CalcNum);
		for( i4RoopCnt1=0; i4RoopCnt1<i4CalcNum; i4RoopCnt1++)
		{
			inputTransmitPacket();
		}
	}
	else{
		;
	}
	return 0;
}

VD setInit(VD)
{
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	
	for( i4RoopCnt1=1; i4RoopCnt1<=MAX_ROUTER; i4RoopCnt1++)
	{
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++)
		{
			u1Connect[i4RoopCnt1][i4RoopCnt2] = DISCONNECT;
		}
	}
	return;
}

I1 inputConnectInfo(VD)
{
	I1 i1Return;
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	I4 i4RouterCnt;
	I4 i4ConnectNum;
	I4 i4AccessPoint;
	
	i1Return = INPUT_SUCCESS;
	
	scanf("%d",&i4RouterNum);
	if(i4RouterNum <= (I4)MAX_ROUTER){
		for( i4RoopCnt1=1; i4RoopCnt1<=i4RouterNum; i4RoopCnt1++)		{
			scanf("%d%d",&i4RouterCnt,&i4ConnectNum);
			for( i4RoopCnt2=0; i4RoopCnt2<i4ConnectNum; i4RoopCnt2++)			{
				scanf("%d",&i4AccessPoint);
				u1Connect[i4RouterCnt][i4AccessPoint] = CONNECT;
			}
			u1Connect[i4RouterCnt][i4RouterNum+1] = SENTINEL;
		}
	}
	else{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

I1 inputTransmitPacket(VD)
{
	I1 i1Return;
	I4 i4StartRouter;
	I4 i4GoalRouter;
	I4 i4TTL;
	I4 i4Ans;
	
	i1Return = INPUT_SUCCESS;
	
	scanf("%d%d%d",&i4StartRouter,&i4GoalRouter,&i4TTL);
	if(i4TTL <= MAX_TTL){
		i4Ans = CalcDijikstra(i4StartRouter,i4GoalRouter,i4TTL);
		if(i4Ans > 0){
			printf("%d\n",i4Ans);
		}
		else{
			printf("NA\n");
		}
	}
	else{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

I4 CalcDijikstra(I4 i4Sender,I4 i4Destination,I4 i4TTL)
{
	I1 i1StarTable[MAX_ROUTER+1][MAX_ROUTER+1];
	I4 i4MinVal[MAX_ROUTER+1];
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	I4 i4RoopCnt3;
	
	for( i4RoopCnt1=1; i4RoopCnt1<=MAX_ROUTER; i4RoopCnt1++){
		i4MinVal[i4RoopCnt1] = 0;
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++){
			i1StarTable[i4RoopCnt1][i4RoopCnt2] = FALSE;
		}
	}
	
	i1StarTable[1][i4Sender] = TRUE;
	
	for(i4RoopCnt1=1; i4RoopCnt1<MAX_ROUTER; i4RoopCnt1++){
		if(i4TTL <= i4RoopCnt1){
			return 0;
		}
		else{
			;
		}
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++){
			if(i1StarTable[i4RoopCnt1][i4RoopCnt2] == TRUE){
				for(i4RoopCnt3=1; i4RoopCnt3<=MAX_ROUTER; i4RoopCnt3++){
					if(u1Connect[i4RoopCnt2][i4RoopCnt3] == CONNECT){
						if(i4RoopCnt3 == i4Destination){
							return i4RoopCnt1+1;
						}
						else{
							;
						}
						i1StarTable[i4RoopCnt1+1][i4RoopCnt3] = TRUE;
					}
					
				}
			}
			else{
				;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 2664
final_rank: 10

code_snippet: /* ????????????????????????????????????????????? */
#include <stdio.h>

/* ?????°???????????? */
typedef	void			VD;
typedef	signed char		I1;
typedef unsigned char 	U1;
typedef	signed short	I2;
typedef unsigned short 	U2;
typedef	signed int		I4;
typedef unsigned int 	U4;

/* ?????°?????? */
#define MAX_ROUTER 100
#define MAX_TTL 10000

#define CONNECT 1
#define DISCONNECT 0
#define SENTINEL -1

#define TRUE 1
#define FALSE 0

#define INPUT_SUCCESS 0
#define INPUT_ERORR -1

/* ????????????????????£?¨? */
VD setInit(VD);
I1 inputConnectInfo(VD);
I1 inputTransmitPacket(VD);
I4 CalcDijikstra(I4 i4Sender,I4 i4Destination,I4 i4TTL,I4 i4RouterSum);

I4 CalcDijikstra(I4 i4Sender,I4 i4Destination,I4 i4TTL,I4 i4RouterSum);
/* ??°?????????????????° */
I1 u1Connect[MAX_ROUTER+1][MAX_ROUTER+1];
I4 i4RouterNum;

I4 main(VD)
{
	I1 i1ErrorCheck;
	I4 i4CalcNum;
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	
	setInit();
	i1ErrorCheck = inputConnectInfo();
	if(i1ErrorCheck == INPUT_SUCCESS){
		scanf("%d",&i4CalcNum);
		for( i4RoopCnt1=0; i4RoopCnt1<i4CalcNum; i4RoopCnt1++)
		{
			inputTransmitPacket();
		}
	}
	return 0;
}

VD setInit(VD)
{
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	
	for( i4RoopCnt1=1; i4RoopCnt1<=MAX_ROUTER; i4RoopCnt1++)
	{
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++)
		{
			u1Connect[i4RoopCnt1][i4RoopCnt2] = DISCONNECT;
		}
	}
	return;
}

I1 inputConnectInfo(VD)
{
	I1 i1Return;
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	I4 i4RouterCnt;
	I4 i4ConnectNum;
	I4 i4AccessPoint;
	
	i1Return = INPUT_SUCCESS;
	
	scanf("%d",&i4RouterNum);
	if(i4RouterNum <= (I4)MAX_ROUTER)
	{
		for( i4RoopCnt1=1; i4RoopCnt1<=i4RouterNum; i4RoopCnt1++)
		{
			scanf("%d%d",&i4RouterCnt,&i4ConnectNum);
			for( i4RoopCnt2=0; i4RoopCnt2<i4ConnectNum; i4RoopCnt2++)
			{
				scanf("%d",&i4AccessPoint);
				u1Connect[i4RouterCnt][i4AccessPoint] = CONNECT;
			}
			u1Connect[i4RouterCnt][i4RouterNum+1] = SENTINEL;
		}
	}
	else
	{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

I1 inputTransmitPacket(VD)
{
	I1 i1Return;
	I4 i4StartRouter;
	I4 i4GoalRouter;
	I4 i4TTL;
	I4 i4Ans;
	
	i1Return = INPUT_SUCCESS;
	
	scanf("%d%d%d",&i4StartRouter,&i4GoalRouter,&i4TTL);
	if(i4TTL <= MAX_TTL)
	{
		i4Ans = CalcDijikstra(i4StartRouter,i4GoalRouter,i4TTL,1);
		if(i4Ans > 0){
			printf("%d\n",i4Ans);
		}
		else
		{
			printf("NA\n");
		}
	}
	else
	{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

I4 CalcDijikstra(I4 i4Sender,I4 i4Destination,I4 i4TTL,I4 i4RouterSum)
{
	I1 i1StarTable[MAX_ROUTER+1][MAX_ROUTER+1];
	I4 i4MinVal[MAX_ROUTER+1];
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	I4 i4RoopCnt3;
	
	for( i4RoopCnt1=1; i4RoopCnt1<=MAX_ROUTER; i4RoopCnt1++)
	{
		i4MinVal[i4RoopCnt1] = 0;
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++)
		{
			i1StarTable[i4RoopCnt1][i4RoopCnt2] = FALSE;
		}
	}
	
	i1StarTable[1][i4Sender] = TRUE;
	
	for(i4RoopCnt1=1; i4RoopCnt1<MAX_ROUTER; i4RoopCnt1++)
	{
		if(i4TTL <= i4RoopCnt1)
		{
			return 0;
		}
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++)
		{
			if(i1StarTable[i4RoopCnt1][i4RoopCnt2] == TRUE)
			{
				for(i4RoopCnt3=1; i4RoopCnt3<=MAX_ROUTER; i4RoopCnt3++)
				{
					if(u1Connect[i4RoopCnt2][i4RoopCnt3] == CONNECT)
					{
						if(i4RoopCnt3 == i4Destination)
						{
							return i4RoopCnt1+1;
						}
						i1StarTable[i4RoopCnt1+1][i4RoopCnt3] = TRUE;
					}
					
				}
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 2700
final_rank: 9

code_snippet: #include<functional>
#include<algorithm>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define pf push_front
#define pb push_back
#define int long long
using namespace std;
const int INF=1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const int N=101;

int net[N][N];

int bfs(int n,int s,int d){
  vector<int> time(n);
  queue<int> que;
  rep(i,n)time[i]=INF;
  time[s]=0;
  que.push(s);
  int u;
  while(!que.empty()){
    u=que.front(),que.pop();
    rep(i,n){
      if(time[i]>time[u]+net[u][i]){
        time[i]=time[u]+net[u][i];
        que.push(i);
      }
    }
  }
  return time[d];
}

signed main(){
  int n;
  cin>>n;
  rep(i,n)rep(j,n)net[i][j]=INF;
  rep(i,n){
    int r,k;
    cin>>r>>k;
    r--;
    rep(i,k){
      int t;
      cin>>t;
      t--;
      net[r][t]=1;
    }
  }

  int p;
  cin>>p;
  rep(i,p){
    int s,d,v;
    cin>>s>>d>>v;
    s--;d--;
    int ans=bfs(n,s,d)+1;

    if(ans<=v)cout<<ans<<endl;
    else cout<<"NA"<<endl;
  }
 return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3100
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	int g[n][n];
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			g[i][j] = 100000000;
	int r,k,t;
	for(int i = 0; i < n; i++){
		cin >> r >> k;
		r--;
		for(int j = 0; j < k; j++){
			cin >> t;
			t--;
			g[r][t] = 1;
		}
	}
	int p;
	cin >> p;
	for(int i = 0; i < p; i++){
		int s,d,v;
		cin >> s >> d >> v;
		s--; d--;
		vector <int> used(n,false);
		vector <int> dist(n,100000000);
		dist[s] = false;
		while(1){
			int va = -1;
			for(int j = 0; j < n; j++){
				if(used[j] == false && (va == -1 || dist[va] > dist[j]))
					va = j;
			}
			if(va == -1)
				break;
			used[va] = true;
			for(int j = 0; j < n; j++)
				dist[j] = min(dist[j] , dist[va] + g[va][j]);	
		}

		if(v > dist[d])
			cout << dist[d] + 1 << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3096
final_rank: 13

code_snippet: #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>

//#include<bits/stdc++.h>


using namespace std;



int main() {

	bool y[105][105] = {};


	int n; cin >> n;
	for (int i = 0; i < n; i++)
	{
		int a,b; cin >> a>>b;
		for (int j = 0; j < b; j++) {
			int z;
			cin >> z;
			y[a][z] = 1;
		}
	}
	
	int p; cin >> p;
	for (int i = 0; i < p; i++) {
		int s, d, t;
		cin >> s >> d >> t;
		queue<int>Q;
		Q.push(s);
		int h = 1;
		int R[105] = {};
		for (int j = 0; j <= 102; j++)
			R[j] = 999999;
		R[s] = 1;
		while (Q.size()) {
			int v = Q.front();
			h = R[v];
			h++;
		
			
			bool F[105] = {};
			for (int j = 1; j <= n; j++) {
				if (y[v][j] && R[j] > h) {
					R[j] = h;
					Q.push(j);
				}



			}
			Q.pop();	
		}
/*		for (int j = 0; j < n; j++)
			cout << R[j] << " ";*/
		if (R[d] <= t)cout << R[d] << endl;
		else cout << "NA" << endl;
	}
	
	return 0;

}
accuracy: 1
time_ms: 10
memory_kb: 3100
final_rank: 11

code_snippet: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<bits//stdc++.h>
#include<functional>
using namespace std;
struct P{
	int cost,pos;
};
bool operator<(P a,P b) {return a.cost<b.cost;}
bool operator>(P a,P b) {return a.cost>b.cost;}
long long a[101];
const long long inf=800000000;
vector<long long>e[101];
int main() {
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int r,k,c;
		cin>>r>>k;
		for(int i=0;i<k;i++){
			cin>>c;
			e[r].push_back(c);
		}
	}
	int p;
	cin>>p;
	for(int z=0;z<p;z++){
		int s,d,v;
		cin>>s>>d>>v;
		fill(a,a+n+1,inf);
		priority_queue<P>p;
		p.push(P{1,s});
		a[s]=1;
		while(!p.empty()){
			P t=p.top();p.pop();
			for(int i=0;i<(int)e[t.pos].size();i++){
				if(a[t.pos]+1<a[e[t.pos][i]]){
					a[e[t.pos][i]]=a[t.pos]+1;
					p.push(P{a[t.pos]+1,e[t.pos][i]});
				}
			}
		}
		if(a[d]<=v) cout<<a[d]<<endl;
		else cout<<"NA"<<endl;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3160
final_rank: 14

code_snippet: #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,P[101],D[101],num,min,min_index,current,Distance[101][101];

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

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

	for(int i = 1; i<= n; i++){
		for(int k=1; k <= n; k++){
			if(i != k){
				start = i, goal = k;

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

				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 index=1; index <= n; index++){
						if(table[current][index] == 1){
							D[index] = (D[index] <= (D[current]+1))?D[index]:(D[current]+1);
						}
					}
				}

				Distance[start][goal] = D[goal];
			}
		}
	}

	scanf("%d",&num_of_packet);

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

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

	}

    return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 2780
final_rank: 16

code_snippet: #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;
}
accuracy: 1
time_ms: 20
memory_kb: 2756
final_rank: 6

code_snippet: #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){
				if(memo[index][goal] > 0 && memo[index][goal] + 1 <= t_limit){
					printf("%d\n",memo[index][goal]+1);
					FLG = true;
					break;
				}
				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; //?????????????????????????????????????????°break

			P[min_index] = 1; //min_index?????????????????¨??????
			current = min_index;
			num++; //???????????????????????°???1?¢???????

			/*if(memo[current][goal] > 0 && D[current]+memo[current][goal] <= t_limit){
				D[goal] = D[current]+memo[current][goal]-1;
				break;
			}*/

			for(int i=1; i <= n; i++){
				if(table[current][i] == 1){	//start???????????????????????¨???????????£????????????
					D[i] = (D[i] <= (D[current]+1))?D[i]:(D[current]+1); //?????????????????´??\????????¢??¨???current????????±???????????¢???????????????D??¨??????
				}
			}

		}

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

	}

    return 0;
}
accuracy: 0.6666666666666666
time_ms: 20
memory_kb: 2728
final_rank: 19

code_snippet: #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);


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

		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; //start??°?????????????????¢?????¨???????????????
		}

		P[start] = 1; //??????????????°????????????????????¨??????
		num = 1;

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

		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; //?????????????????????????????????????????°break

			P[min_index] = 1; //min_index?????????????????¨??????
			current = min_index;
			num++; //???????????????????????°???1?¢???????

			for(int i=1; i <= n; i++){
				if(table[current][i] == 1){	//start???????????????????????¨???????????£????????????
					D[i] = (D[i] <= (D[current]+1))?D[i]:(D[current]+1); //?????????????????´??\????????¢??¨???current????????±???????????¢???????????????D??¨??????
				}
			}

		}

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

	}

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 2792
final_rank: 7

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back 

int bfs(int s, int t, int c);

vector<vector<int> > g(101);

int main()
{
    int n;
    cin >> n;
    loop(i, n) {
        int r, k, t;
        cin >> r >> k;
        loop(j, k) {
            cin >> t;
            g[r].pb(t);
        }
    }
    int p;
    cin >> p;
    loop (i, p) {
        int s, d, v;
        cin >> s >> d >> v;
        int ans = bfs(s, d, v);
        if (ans != -1) {
            cout << v-ans << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

int bfs(int s, int t, int c)
{
    vector<bool> check(101, false);
    queue<int> fq, nq, sq;
    fq.push(s);
    while (1) {
        c--;
        while (!fq.empty()) {
            int pos = fq.front(); fq.pop();
            if (check[pos]) {
                continue;
            } else {
                check[pos] = true;
            }
            //cout << pos << endl;
            if (pos == t) {
                return c;
            } else {
                for (int i: g[pos]) {
                    nq.push(i);
                }
            }
        }
        if (c == 0) {
            return -1;
        }
        fq = nq;
        nq = sq;
    }
}
accuracy: 0.6666666666666666
time_ms: 30
memory_kb: 3104
final_rank: 20

code_snippet: #include<iostream>
#include<queue>
using namespace std;
int d[101][101]={};
int x[101];
int n;
void bfs(int);
int main(){
  int p;
  int na[100];
  int to;
  int th;
  int st,go,ttl;
  //syoki
  for(int i=0;i<n;i++){
    x[i]=10000;
  }
  //in
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> na[i] >> to;
    for(int j=0;j<to;j++){
      cin >> th;
      d[na[i]][th]=1;
    }
  
  }
  //in2
  cin >> p;
  for(int i=0;i<p;i++){
    for(int j=0;j<=n;j++){
      x[j]=10000;
    }
    
    cin>> st >> go >> ttl;
    x[st]=1;
    bfs(st);
    if(x[go]<=ttl && x[go]!=10000)cout << x[go] << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

///bfs///////////////////////////////////////////
void bfs(int s){
  queue<int>q;
  int u;
  q.push(s);
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int v=0;v<=n;v++){
      if(d[u][v]==1&&x[v]==10000){
	x[v]=x[u]+1;
	q.push(v);
      }
    }
  }
  return ;
}
accuracy: 1
time_ms: 10
memory_kb: 1244
final_rank: 2

code_snippet: #include<iostream>
#include<queue>
using namespace std;
int d[101][101]={};
int x[101];
int n;
void bfs(int);
int main(){
  int p;
  int na[100];
  int to;
  int th;
  int st,go,ttl;
  //syoki
  for(int i=0;i<n;i++){
    x[i]=10000;
  }
  //in
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> na[i] >> to;
    for(int j=0;j<to;j++){
      cin >> th;
      d[na[i]][th]=1;
    }
  
  }
  //in2
  cin >> p;
  for(int i=0;i<p;i++){
    for(int j=0;j<=n;j++){
      x[j]=10000;
    }
    
    cin>> st >> go >> ttl;
    x[st]=1;
    bfs(st);
    if(x[go]<=ttl)cout << x[go] << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

///bfs///////////////////////////////////////////
void bfs(int s){
  queue<int>q;
  int u;
  q.push(s);
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int v=0;v<=n;v++){
      if(d[u][v]==1&&x[v]==10000){
	x[v]=x[u]+1;
	q.push(v);
      }
    }
  }
  return ;
}
accuracy: 0.6666666666666666
time_ms: 10
memory_kb: 1240
final_rank: 17

code_snippet: #include<iostream>
#include<queue>
using namespace std;
int d[1000][1000]={};
int x[100];
int n;
int nm=0;
void bfs(int);
int main(){
  int p;
  int na[100];
  int to;
  int th;
  int st,go,ttl;
  //syoki
  for(int i=0;i<n;i++){
    x[i]=10000;
  }
  //in
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> na[i] >> to;
    if(na[i]>nm)nm=na[i];
    for(int j=0;j<to;j++){
      cin >> th;
      d[na[i]][th]=1;
    }
  
  }
  //in2
  cin >> p;
  for(int i=0;i<p;i++){
    for(int j=0;j<=nm;j++){
      x[j]=10000;
    }
    
    cin>> st >> go >> ttl;
    x[st]=1;
    bfs(st);
    if(x[go]<=ttl)cout << x[go] << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

///bfs///////////////////////////////////////////
void bfs(int s){
  queue<int>q;
  int u;
  q.push(s);
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int v=0;v<=nm;v++){
      if(d[u][v]==1&&x[v]==10000){
	x[v]=x[u]+1;
	q.push(v);
      }
    }
  }
}
accuracy: 0.6666666666666666
time_ms: 10
memory_kb: 1576
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
const int INF = 10000000;
int wf[101][101];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			wf[i][j] = i == j ? 0 : INF;
		}
	}
	for(int i = 0; i < n; i++) {
		int a, t;
		cin >> a >> t;
		while(t--) {
			int b;
			cin >> b;
			wf[a][b] = 1;
		}
	}

	for(int k = 1; k <= n; k++)
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);

	int q;
	cin >> q;
	while(q--) {
		int s, d, v;
		cin >> s >> d >> v;
		if(wf[s][d] >= v) {
			cout << "NA" << endl;
		}
		else {
			cout << wf[s][d] + 1 << endl;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1240
final_rank: 1

code_snippet: #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 distance second

int n;
bool info[100][100];

int bfs(int s,int d){
	queue<NODE> Q;
	bool done[100]={false};
	
	Q.push( make_pair(s,0) );
	while( !Q.empty() ){
		NODE now = Q.front();
		Q.pop();
		done[now.pos] = true;

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

		for(int next=0;next<n;next++){
			if(info[now.pos][next] && !done[next]){
				Q.push( make_pair(next,now.distance+1) );
			}
		}
	}
	return INT_MAX;
}

int main(){
	int r,k,t,p,s,d,v;
	
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> r >> k;
		r--;
		for(int j=0;j<k;j++){
			cin >> t;
			t--;
			info[r][t] = true;
		}
	}	
	
	cin >> p;
	for(int i=0;i<p;i++){
		cin >> s >> d >> v;
		s--;d--;
		if(v > bfs(s,d) )cout << bfs(s,d)+1 << endl;
		else cout << "NA" << endl;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 1224
final_rank: 3

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

struct edge{ 
	int to, cost;
	edge(int to, int cost) :to(to), cost(cost){};
};
vector<edge> G[110];

int n, r, k, t;
int p, s, d, v;

int dis[110];

void dijkstra(int start){
	dis[start] = 0;

	priority_queue<P> que;
	que.push(P(0, start));

	while (!que.empty()){
		P p = que.top(); que.pop();
		int a = p.second;

		if (dis[a] < p.first)continue;

		for (int i = 0; i < G[a].size(); i++){
			edge e = G[a][i];
			if (dis[e.to] > dis[a] + e.cost){
				dis[e.to] = dis[a] + e.cost;
				que.push(P(dis[e.to], e.to));
			}

		}
	}
}

int main(){
	cin >> n;
	REP(i, n){
		cin >> r >> k;
		REP(j, k){
			cin >> t;
			G[r].push_back(edge(t, 1));
		}
	}
	cin >> p;
	REP(i, p){
		REP(j, 110)dis[j] = INF;
		cin >> s >> d >> v;
		dijkstra(s);
		if (dis[d] + 1 <= v)cout << dis[d] + 1 << endl;
		else cout << "NA" << endl;
	}
}
accuracy: 1
time_ms: 30
memory_kb: 1204
final_rank: 4