D. Country Division

Codeforces
IDCF10221D
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
A country has $n$ cities that are connected by $(n -1)$ roads, and it is possible to reach every city from every other city. Before the election political analysts have made $q$ predictions how the election will proceed. In each of these predictions it is supposed that some cities will support one candidate (we will call such cities red), some other cities will support another candidate (we will call such cities blue), and the citizens in the remaining cities don't care about politics and they don't support anyone. For every prediction you have to answer, is it possible or not to block some roads so that from every red city it is possible to reach every other red city, from every blue city it is possible to reach every other blue city, but from every red city it is not possible to reach every blue city. The first line contains an integer $n$ ($2 <= n <= 200000$) — the number of cities. Each of the next $(n -1)$ lines contains two integers $u_i, v_i$ ($1 <= u_i, v_i <= n$) — the numbers of cities connected by roads. The next line contains an integer $q$ ($1 <= n <= 200000$) — the number of predictions. Each of the next $q$ lines contains a description of the prediction. It starts with two integers $r_j$ and $b_j$ ($1 <= r_j, b_j < n, r_j + b_j <= n$) — how many red and blue cities this prediction has. Then $r_j$ integers follow — the numbers of red cities. Then $b_j$ integers follow — the numbers of blue cities. All cities in each prediction are distinct. The sum of all $r_j$ and $b_j$ over all predictions doesn't exceed $200000$. For every prediction, output the answer for it — «_YES_» or «_NO_» — in a separate line. ## Input The first line contains an integer $n$ ($2 <= n <= 200000$) — the number of cities.Each of the next $(n -1)$ lines contains two integers $u_i, v_i$ ($1 <= u_i, v_i <= n$) — the numbers of cities connected by roads.The next line contains an integer $q$ ($1 <= n <= 200000$) — the number of predictions.Each of the next $q$ lines contains a description of the prediction. It starts with two integers $r_j$ and $b_j$ ($1 <= r_j, b_j < n, r_j + b_j <= n$) — how many red and blue cities this prediction has. Then $r_j$ integers follow — the numbers of red cities. Then $b_j$ integers follow — the numbers of blue cities. All cities in each prediction are distinct.The sum of all $r_j$ and $b_j$ over all predictions doesn't exceed $200000$. ## Output For every prediction, output the answer for it — «_YES_» or «_NO_» — in a separate line. [samples]
**Definitions** Let $ T $ be a tree with nodes labeled by positive integers (IDs), initially containing one node: $ \text{id} = 1 $. Let $ Q \in \mathbb{Z}^+ $ be the number of queries. Let $ \text{last} \in \mathbb{Z} $ be the answer to the previous query, initialized to 0. Let $ \text{curr} \in \mathbb{Z}^+ $ be the current number of nodes in the tree, initialized to 1. **Constraints** 1. $ 1 \le Q \le 2 \cdot 10^5 $ 2. Each query is of type $ t \in \{1, 2\} $. 3. For type-1 queries: - A new node with $ \text{id} = \text{curr} + 1 $ is added and connected to node $ p $, where $ p = (u_{\text{encoded}} + \text{last}) \mod \text{curr} + 1 $. - $ u_{\text{encoded}} $ is the encoded value provided in input. 4. For type-2 queries: - Two nodes $ u, v $ are given in encoded form: $ u = (u_{\text{encoded}} + \text{last}) \mod \text{curr} + 1 $, $ v = (v_{\text{encoded}} + \text{last}) \mod \text{curr} + 1 $. 5. All node IDs are distinct positive integers, assigned incrementally. **Objective** For each query: - If $ t = 1 $: Add a new node $ \text{curr} + 1 $ connected to node $ p = (u_{\text{encoded}} + \text{last}) \mod \text{curr} + 1 $, then compute and output the distance $ d(\text{curr}+1, 1) $, and update $ \text{last} $ to this distance and $ \text{curr} \gets \text{curr} + 1 $. - If $ t = 2 $: Decode $ u, v $ as above, compute the distance $ d(u, v) $ in the tree, output it, and update $ \text{last} $ to this distance. **Distance Definition** For any two nodes $ u, v $, the distance $ d(u, v) $ is the number of nodes on the unique simple path between $ u $ and $ v $ (including both endpoints).
API Response (JSON)
{
  "problem": {
    "name": "D. Country Division",
    "description": {
      "content": "A country has $n$ cities that are connected by $(n -1)$ roads, and it is possible to reach every city from every other city. Before the election political analysts have made $q$ predictions how the el",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10221D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "A country has $n$ cities that are connected by $(n -1)$ roads, and it is possible to reach every city from every other city. Before the election political analysts have made $q$ predictions how the el...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ T $ be a tree with nodes labeled by positive integers (IDs), initially containing one node: $ \\text{id} = 1 $.  \nLet $ Q \\in \\mathbb{Z}^+ $ be the number of queries.  \nLet $ \\t...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments