All submissions for this problem are available.
You will be given a tree represented by a matrix. The jth character of ith row will be 1 if there is a path from node i to j.
You will also be given two integers N and F. N is the number of nodes in the tree and F(0 based index) is the node to which you need to find the distance from the root node. Root node will always be 0. Print the distance of the node F from the root.
First line will contain an integer T, the count of test cases. Each test case will start with two space separated integers, N and F, in a newline. N lines will follow each of length N. Each character of these lines will be either 0 or 1.
Output must contain T lines, each having the distance of the node F from root node 0 for each tree.
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 10
- 0 ≤ F ≤ 9
, F is 0 based index.
Input: 2 1 0 0 9 6 011000000 000111000 000000111 000000000 000000000 000000000 000000000 000000000 000000000 Output: 0 2
Example case 1. Since there is only one node which is the root the distance is 0
Example case 2. Path to node 6 is
0->2->6, thus distance is 2.
|Tags||chandubaba, chandubaba, graph, graph-theory, keteki, simple|
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, PYP3, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.