codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2257C.md (2942B)


      1 Source: https://codeforces.com/problemset/problem/2257/C
      2 
      3 C. Spying on the Beaver
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given a rooted tree$^{\text{∗}}$ with $n$ vertices, numbered from $1$ to $n$. The vertex numbered $1$ is the root of the tree. The Beaver, initially located at the root, travels through the tree to one of the beaver dams located at the vertices numbered $a_1, \ldots, a_m$.
     22 
     23 You need to determine which of these $m$ vertices the Beaver went to. To do this, you can place cameras on any edges of the tree. If the Beaver traverses an edge with a camera on it, you will see this. For clarity, we assume that after all the Beaver's movements, you will receive a sequence of edges with cameras in which the Beaver was observed passing through the corresponding edge.
     24 
     25 Since cameras are expensive, it is necessary to use the minimum number of them sufficient to uniquely determine the Beaver's destination. You are required to state the minimum necessary number of cameras $k$ and the edges on which they should be placed.
     26 
     27 $^{\text{∗}}$A rooted tree is a tree where one vertex is special and called the root.
     28 
     29 Input
     30 
     31 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2 \cdot 10^4$). The description of the test cases follows.
     32 
     33 In the first line of each test case, there is a single integer $n$ ($2 \le n \le 10^5$).
     34 
     35 The second line contains $n-1$ integers $p_2, \ldots, p_n$ — the parents of the vertices from the $2$nd to the $n$th ($1 \le p_i \lt i$; $2 \le i \le n$).
     36 
     37 The third line contains a single integer $m$ — the number of vertices containing beaver dams ($1 \le m \le n$).
     38 
     39 In the fourth line, there are $m$ integers $a_1, \ldots, a_m$ — the numbers of these vertices ($1 \le a_i \le n$; $1 \le i \le m$). All $a_i$ are distinct.
     40 
     41 It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
     42 
     43 Output
     44 
     45 For each test case, output exactly one line. First, output the number $k$ — the minimum required number of cameras, and then, in the same line, for each of the $k$ edges connecting the vertices $u$ and $p_u$, where cameras need to be installed, output the vertex number $u$.
     46 
     47 If there are multiple answers, you can output any one of them.
     48 
     49 Example
     50 
     51 Input
     52 
     53 ```
     54 4
     55 
     56 2
     57 
     58 1
     59 
     60 1
     61 
     62 1
     63 
     64 3
     65 
     66 1 1
     67 
     68 3
     69 
     70 2 3 1
     71 
     72 3
     73 
     74 1 2
     75 
     76 2
     77 
     78 2 3
     79 
     80 6
     81 
     82 1 2 2 1 1
     83 
     84 3
     85 
     86 5 3 1
     87 ```
     88 
     89 Output
     90 
     91 ```
     92 0
     93 2 2 3
     94 1 3
     95 2 2 5
     96 ```
     97 
     98 Note
     99 
    100 In the first test case, we do not need to install any cameras because there is only one dam in the tree, and the Beaver will definitely go there.
    101 
    102 In the second test case, it is necessary to place a camera on each edge because there is a dam located at each vertex.
    103 
    104 In the third test case, the dams are located in two adjacent vertices, so by placing a camera on the edge between these vertices, we can uniquely determine which one the Beaver went to.