1. /*
  2.   FORHAD-SUST-BD
  3. */
  4. #include <iostream>
  5. #include <string>
  6. #include <map>
  7. #include <queue>
  8. #include <stack>
  9. #include <algorithm>
  10. #include <list>
  11. #include <set>
  12. #include <cmath>
  13. #include <cstring>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <sstream>
  17. #include <stdlib.h>
  18. #include <vector>
  19. #include <iomanip>
  20.  
  21. using namespace std;
  22.  
  23. #ifdef __GNUC__
  24. typedef long long ll;typedef unsigned long long ull;
  25. #else
  26. typedef __int64 ll; typedef unsigned __int64 ull;
  27. #endif
  28.  
  29. #define INF 1<<28
  30. #define SIZE 100
  31. #define PI 2*acos(0)
  32. #define fg %I64d
  33. #define REP(i,n) for (i=0;i<n;i++)
  34. #define REV(i,n) for (i=n;i>=0;i--)
  35. #define FOR(i,p,k) for (i=p; i<k;i++)
  36. #define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it)
  37.  
  38. #define bug(x) cout<< "->" <<#x<<": "<<x<<endl
  39. #define Sort(x) sort(x.begin(),x.end())
  40. #define Reverse(x) reverse(x.begin(),x.end())
  41. #define MP(a,b) make_pair(a,b)
  42. #define Clear(x,with) memset(x,with,sizeof(x))
  43. #define Copy(c,r) memcpy(c,r,sizeof(r))
  44. #define SZ(x) (int)x.size()
  45. #define length(x) (int)x.length()
  46. #define All(x) x.begin(),x.end()
  47. #define pb push_back
  48. #define popcount(i) __builtin_popcount(i)
  49. #define gcd(a,b) __gcd(a,b)
  50. #define fs first
  51. #define sc second
  52. #define two(X) (1<<(X))
  53. #define twoL(X) (((ll)(1))<<(X))
  54. #define contain(S,X) (((S)&two(X))!=0)
  55. #define containL(S,X) (((S)&twoL(X))!=0)
  56.  
  57. typedef pair<int,int> pii;
  58. typedef pair<double,double> pdd;
  59. typedef vector<ll> vi;
  60. typedef vector<double>vd;
  61. typedef vector<ll>vll;
  62. typedef vector<string> vs;
  63. typedef vector<vi>vvi;
  64. typedef vector<vll>vvll;
  65. typedef vector<vd>vvd;
  66. typedef vector<pii>vpii;
  67. typedef map<string,int> msi;
  68. typedef map<int,int>mii;
  69. typedef map<pii,int>mpi;
  70.  
  71. template<class T> inline T sqr(T x){return x*x;}
  72. template<class T> inline bool isPrime(T n){if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
  73. template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No.
  74. template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();}
  75. int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
  76. ll toLl(string s){ll r=0;istringstream sin(s); sin>>r; return r;}
  77. template<class T> void debug(const T& e){cout<<e<<endl;}
  78. template<class T1,class T2> void debug(const T1& e1,const T2& e2){cout<<e1<<"\t"<<e2<<endl;}
  79. template<class T1,class T2,class T3> void debug(const T1& e1,const T2& e2,const T3& e3){cout<<e1<<"\t"<<e2<<"\t"<<e3<<endl;}
  80. template<class T1,class T2,class T3,class T4> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<endl;}
  81. template<class T1,class T2,class T3,class T4,class T5> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4,const T5& e5){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<endl;}
  82. template<class T> void debug(vector<T>& e){int i;REP(i,SZ(e)) cout<<e[i]<<" ";cout<<endl;}
  83. template<class T> void debug(vector< basic_string<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j];cout<<endl;} cout<<endl;}
  84. template<class T> void debug(vector< vector<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j]<<"\t";cout<<endl;} cout<<endl;}
  85. template<class T> void debug(T e[SIZE][SIZE],int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j]<<" ";cout<<endl;}}
  86.  
  87. ll Pow(ll B,ll P){ ll R=1; while(P>0) {if(P%2==1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
  88. //struct pq{ int cost,node;bool operator<(const pq &b)const{return cost>b.cost;}};// Min Priority Queue
  89. //bool comp(pq a,pq b){ return a.cost > b.cost;} //Comp Sort
  90.  
  91. //int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
  92. //int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
  93. //int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
  94.  
  95. vvi Multiplication(vvi A,vvi B) //Matrix Should be MxN ans NxP. return MxP matrix
  96. {
  97. ll m,n,p,i,j,k;
  98. double val;
  99.  
  100. m=SZ(A); n=SZ(B); p=SZ(B[0]);
  101. vvi C(m,vi(p));
  102.  
  103. REP(i,m)
  104. REP(j,p)
  105. REP(k,n)
  106. {
  107. if(C[i][j]<0) continue;
  108. if(A[i][k]==0 || B[k][j]==0) continue;
  109. if(A[i][k]<0 || B[k][j]<0) C[i][j]=-1;
  110. else if(C[i][j]+(double)A[i][k]*(double)B[k][j]>=(twoL(62)*2.0)) C[i][j]=-1;
  111. else C[i][j]+=(A[i][k]*B[k][j]);
  112. }
  113. return C;
  114. }
  115.  
  116. vvi PowerMat(vvi A,ll steps) //Compute A^steps
  117. {
  118. if(steps==1) return A;
  119. if(steps%2==1) return Multiplication(PowerMat(A,steps-1),A);
  120. vvi T = PowerMat(A,steps/2);
  121. return Multiplication(T,T);
  122. }
  123.  
  124. class GraphPaths
  125. {
  126. public:
  127. long long howMany(vector <string> graph, int start, int destination, int length)
  128. {
  129. ll i,j,n,v;
  130.  
  131. n=SZ(graph);
  132. vvi mat(n,vi(n));
  133. REP(i,n)
  134. {
  135. istringstream iss(graph[i]);
  136. while(iss>>v)
  137. {
  138. mat[i][v]=1;
  139. }
  140. }
  141. mat=PowerMat(mat,length);
  142. return mat[start][destination];
  143.  
  144. }
  145. };
  146.  
  147.  
  148. template<typename T> void print( T a ) {
  149. cerr << a;
  150. }
  151.  
  152. void print( long long a ) {
  153. cerr << a << "L";
  154. }
  155.  
  156. void print( string a ) {
  157. cerr << '"' << a << '"';
  158. }
  159.  
  160. template<typename T> void print( vector<T> a ) {
  161. cerr << "{";
  162. for ( int i = 0 ; i != a.size() ; i++ ) {
  163. if ( i != 0 ) cerr << ", ";
  164. print( a[i] );
  165. }
  166. cerr << "}" << endl;
  167. }
  168.  
  169. template<typename T> void assert_eq( int n, T have, T need ) {
  170. if ( have == need ) {
  171. cerr << "Case " << n << " passed." << endl;
  172. } else {
  173. cerr << "Case " << n << " failed: expected ";
  174. print( need );
  175. cerr << " received ";
  176. print( have );
  177. cerr << "." << endl;
  178. }
  179. }
  180.  
  181. template<typename T> void assert_eq( int n, vector<T> have, vector<T> need ) {
  182. if( have.size() != need.size() ) {
  183. cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
  184. print( have );
  185. print( need );
  186. return;
  187. }
  188. for( int i= 0; i < have.size(); i++ ) {
  189. if( have[i] != need[i] ) {
  190. cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
  191. print( have );
  192. print( need );
  193. return;
  194. }
  195. }
  196. cerr << "Case " << n << " passed." << endl;
  197. }
  198. void assert_eq( int n, string have, string need ) {
  199. if ( have == need ) {
  200. cerr << "Case " << n << " passed." << endl;
  201. } else {
  202. cerr << "Case " << n << " failed: expected ";
  203. print( need );
  204. cerr << " received ";
  205. print( have );
  206. cerr << "." << endl;
  207. }
  208. }
  209.  
  210. int main( int argc, char* argv[] )
  211. {
  212.  
  213. GraphPaths objectGraphPaths;
  214.  
  215. //test case0
  216. vector <string> param00;
  217. param00.push_back("1 2 3");
  218. param00.push_back("0 1 2");
  219. param00.push_back("0");
  220. param00.push_back("1 2");
  221. int param01 = 0;
  222. int param02 = 1;
  223. int param03 = 2;
  224. long long ret0 = objectGraphPaths.howMany(param00,param01,param02,param03);
  225. long long need0 = 2;
  226. assert_eq(0,ret0,need0);
  227.  
  228. //test case1
  229. vector <string> param10;
  230. param10.push_back("1");
  231. param10.push_back("2");
  232. param10.push_back("3");
  233. param10.push_back("4");
  234. param10.push_back("5");
  235. param10.push_back("0 3");
  236. int param11 = 0;
  237. int param12 = 0;
  238. int param13 = 10000001;
  239. long long ret1 = objectGraphPaths.howMany(param10,param11,param12,param13);
  240. long long need1 = 0;
  241. assert_eq(1,ret1,need1);
  242.  
  243. //test case2
  244. vector <string> param20;
  245. param20.push_back("1 2");
  246. param20.push_back("0");
  247. param20.push_back("0");
  248. int param21 = 0;
  249. int param22 = 0;
  250. int param23 = 126;
  251. long long ret2 = objectGraphPaths.howMany(param20,param21,param22,param23);
  252. long long need2 = -1;
  253. assert_eq(2,ret2,need2);
  254.  
  255. //test case3
  256. vector <string> param30;
  257. param30.push_back("0 1 2 3");
  258. param30.push_back("0 1 2 3");
  259. param30.push_back("0 1 2 3");
  260. param30.push_back("0 1 2 3");
  261. int param31 = 2;
  262. int param32 = 2;
  263. int param33 = 63;
  264. long long ret3 = objectGraphPaths.howMany(param30,param31,param32,param33);
  265. long long need3 = -1;
  266. assert_eq(3,ret3,need3);
  267.  
  268. }
  269.