#P10113. [GESP202312 八级] 大量的工作沟通
[GESP202312 八级] 大量的工作沟通
Background
Related multiple-choice and true/false problems: https://ti.luogu.com.cn/problemset/1140.
Problem Description
A company has employees, numbered from to . Among them, employee is the boss, and every other employee has exactly one direct supervisor. Assume that the direct supervisor of employee is .
The company has strict management rules: each employee can only be managed by themselves, their direct supervisor, or an indirect supervisor. More precisely, employee can manage employee if and only if , or , or can manage . In particular, the boss (employee ) can only be managed by themselves and cannot be managed by any other employee.
Now, some colleagues want to work together. They want to find one colleague to host the collaboration, and this person must be able to manage all colleagues participating in the collaboration. If multiple employees satisfy this condition, they want the one with the largest index. Can you help them?
Input Format
The first line contains an integer , the number of employees.
The second line contains positive integers separated by spaces: .
The third line contains an integer , meaning there are collaborations to arrange.
The next lines each describe one collaboration. Each line starts with an integer (), the number of employees participating in this collaboration, followed by integers giving the indices of the participating employees (it is guaranteed that the indices are valid and all distinct).
It is guaranteed that the company structure is valid, i.e., there is no employee who is their own direct or indirect supervisor.
Output Format
Output lines. Each line contains one integer, the chosen host for the corresponding collaboration.
5
0 0 2 2
3
2 3 4
3 2 3 4
2 1 4
2
2
0
7
0 1 0 2 1 2
5
2 4 6
2 4 5
3 4 5 6
4 2 4 5 6
2 3 4
2
1
1
1
0
Hint
Sample Explanation 1
For the first collaboration, employees have a common supervisor , who can host the collaboration.
For the second collaboration, employee themselves can manage all participants.
For the third collaboration, only the boss can manage all employees.
Constraints
For of the testdata, .
For of the testdata, .
For all testdata, , , .
2024/2/8 Added one set of hack testdata.
Translated by ChatGPT 5