LCA - Lowest Common Ancestor

题意翻译

## Description: 一棵树是一个简单无向图,图中任意两个节点仅被一条边连接,所有连通无环无向图都是一棵树。-Wikipedia 最近公共祖先(LCA)是……(此处省去对LCA的描述),你的任务是对一棵给定的树$T$以及上面的两个节点$u,v$求出他们的$LCA$。 ![](https://cdn.luogu.org/upload/vjudge_pic/SP14932/0b23f0487b06b8d674d23253a6db517f49ca3acf.png) **例如图中$9$和$12$号节点的$LCA$为$3$号节点** ## Input: 输入的第一行为数据组数$T$,对于每组数据,第一行为一个整数$N(1\leq N\leq1000)$,节点编号从$1$到$N$,接下来的$N$行里每一行开头有一个数字$M(0\leq M\leq999)$,$M$为第$i$个节点的子节点数量,接下来有$M$个数表示第$i$个节点的子节点编号。下面一行会有一个整数$Q(1\leq Q\leq1000)$,接下来的$Q$行每行有两个数$u,v$,输出节点$u,v$在给定树中的$LCA$。 输入数据保证只有一个根节点并且没有环。 ## Output: 对于每一组数据输出$Q+1$行,第一行格式为"Case i:"(没有双引号),i表示当前数据是第几组,接下来的$Q$行每一行一个整数表示一对节点$u,v$的$LCA$。 ## Sample Input: ``` 1 7 3 2 3 4 0 3 5 6 7 0 0 0 0 2 5 7 2 7 ``` ## Sample Output: ``` Case 1: 3 1 ``` Translated by @_yxl_gl_

题目描述

A 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 The 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 Your task in this problem is to find the LCA of any two given nodes v and w in a given tree T. ### ![](https://cdn.luogu.com.cn/upload/vjudge_pic/SP14932/0b23f0487b06b8d674d23253a6db517f49ca3acf.png) **For example the LCA of nodes 9 and 12 in this tree is the node number 3.** ### Input The 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 <= N <= 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 <= M <= 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 <= Q <= 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 <= v, w <= 1,000. Input will guarantee that there is only one root and no cycles. ### Output For 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. ### Example ``` Input: 1 7 3 2 3 4 0 3 5 6 7 0 0 0 0 2 5 7 2 7 Output: Case 1: 3 1 ```

输入输出格式

输入格式


输出格式


输入输出样例

暂无测试点