{"trustable":true,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n #problem-body \u003e pre {\n display: block;\n padding: 9.5px;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n word-break: break-all;\n word-wrap: break-word;\n color: #333;\n background: rgba(255, 255, 255, 0.5);\n border: 1px solid #ccc;\n border-radius: 6px;\n }\n\u003c/style\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv id\u003d\"problem-body\"\u003e\n\t\u003cp style\u003d\"text-align: justify;\"\u003eA tree is an undirected graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. - Wikipedia\u0026nbsp;\u003c/p\u003e\r\n\u003cp style\u003d\"text-align: justify;\"\u003eThe lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with N nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself). - Wikipedia\u003c/p\u003e\r\n\u003cp style\u003d\"text-align: justify;\"\u003eYour task in this problem is to find the LCA of any two given nodes v and w in a given tree T.\u003c/p\u003e\r\n\u003ch3 style\u003d\"text-align: center;\"\u003e\u003cimg src\u003d\"CDN_BASE_URL/7afea78784a923fb816b4d48a8b1dcab?v\u003d1715102835\" alt\u003d\"\"\u003e\u003c/h3\u003e\r\n\u003cp\u003e\u003cstrong\u003eFor example the LCA of nodes 9 and 12 in this tree is the node number 3.\u003c/strong\u003e\u003c/p\u003e\r\n\u003ch3 style\u003d\"text-align: justify;\"\u003eInput\u003c/h3\u003e\r\n\u003cp style\u003d\"text-align: justify;\"\u003eThe first line of input will be the number of test cases. Each test case will start with a number N the number of nodes in the tree, 1 \u0026lt;\u003d N \u0026lt;\u003d 1,000. Nodes are numbered from 1 to N. The next N lines each one will start with a number M the number of child nodes of the Nth node, 0 \u0026lt;\u003d M \u0026lt;\u003d 999 followed by M numbers the child nodes of the Nth node. The next line will be a number Q the number of queries you have to answer for the given tree T, 1 \u0026lt;\u003d Q \u0026lt;\u003d 1000. The next Q lines each one will have two number v and w in which you have to find the LCA of v and w in T, 1 \u0026lt;\u003d v, w \u0026lt;\u003d 1,000.\u003c/p\u003e\r\n\u003cp style\u003d\"text-align: justify;\"\u003eInput will guarantee that there is only one root and no cycles.\u003c/p\u003e\r\n\u003ch3 style\u003d\"text-align: justify;\"\u003eOutput\u003c/h3\u003e\r\n\u003cp style\u003d\"text-align: justify;\"\u003eFor each test case print Q + 1 lines, The first line will have “Case C:” without quotes where C is the case number starting with 1. The next Q lines should be the LCA of the given v and w respectively.\u003c/p\u003e\r\n\u003ch3 style\u003d\"text-align: justify;\"\u003eExample\u003c/h3\u003e\r\n\u003cdiv\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e1\r\n7\r\n3 2 3 4\r\n0\r\n3 5 6 7\r\n0\r\n0\r\n0\r\n0\r\n2\r\n5 7\r\n2 7\r\n\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003eCase 1:\r\n3\r\n1\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/div\u003e\n\u003c/div\u003e"}}]}