H. Optimize DFS

Codeforces
IDCF10286H
Time3000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You are given a tree with $n$ vertices. Every vertex $v$ has two values $a_v$ and $b_v$. You have to process $q$ queries of types: First, create an array $"used"[ 1 \\dots n ]$ filled with _false_ initially. Then run the function $"DFS"(v)$: The first line contains two integers $1 <= n, q <= 5 dot.op 10^5$. The second line contains $n$ integers, where the $i$-th is the initial value of $a_i$ ($0 <= a_i <= 5 dot.op 10^5$). The third line contains $n$ integers, where the $i$-th is the value of $b_i$ ($0 <= b_i <= 5 dot.op 10^5$). Each of the next $n -1$ lines describes an edge of the tree. The $i$-th line contains two integers $u_i$ and $v_i$ ($1 <= u_i, v_i <= n$, $u_i eq.not v_i$), the indices of the vertices connected by the $i$-th edge. It is guaranteed that the given graph is a tree. The next $q$ lines contain the description of the queries. The $i$-th query starts with an integer $t_i$ ($1 <= t_i <= 3$) that denotes the type of the query. If $t_i = 1$, then two integers $v_i$ and $x_i$ follow ($1 <= v_i <= n$, $0 <= x_i <= 5 dot.op 10^5$). Otherwise only $v_i$ is given. For each query of the 2nd type output one integer, the value $a_v$. ## Input The first line contains two integers $1 <= n, q <= 5 dot.op 10^5$. The second line contains $n$ integers, where the $i$-th is the initial value of $a_i$ ($0 <= a_i <= 5 dot.op 10^5$). The third line contains $n$ integers, where the $i$-th is the value of $b_i$ ($0 <= b_i <= 5 dot.op 10^5$).Each of the next $n -1$ lines describes an edge of the tree. The $i$-th line contains two integers $u_i$ and $v_i$ ($1 <= u_i, v_i <= n$, $u_i eq.not v_i$), the indices of the vertices connected by the $i$-th edge. It is guaranteed that the given graph is a tree.The next $q$ lines contain the description of the queries. The $i$-th query starts with an integer $t_i$ ($1 <= t_i <= 3$) that denotes the type of the query. If $t_i = 1$, then two integers $v_i$ and $x_i$ follow ($1 <= v_i <= n$, $0 <= x_i <= 5 dot.op 10^5$). Otherwise only $v_i$ is given. ## Output For each query of the 2nd type output one integer, the value $a_v$. [samples]
**Definitions** Let $ G = (V, E) $ be a grid graph with $ R $ rows and $ C $ columns, where each cell $ (i, j) $ is a vertex. Let $ s = (1, 1) $ be the start vertex and $ t = (R, C) $ be the target vertex. Each cell $ (i, j) $ has a type: - `'.'`: walkable floor - `'#'`: wall (impassable) - `'D'`: door (passable unless locked) **Constraints** 1. $ 2 \leq R, C \leq 1000 $ 2. Yellow can move orthogonally (up, down, left, right) between adjacent cells. 3. Walls (`'#'`) are permanently impassable. 4. Doors (`'D'`) are passable by default but can be locked at a cost of 1 per door. 5. Start $ s $ and target $ t $ may be doors; locking them blocks path access. 6. Goal: Find minimum number of doors to lock to disconnect $ s $ from $ t $. **Objective** Compute the minimum number of doors to lock such that no path exists from $ s $ to $ t $ in the modified graph. If $ s $ or $ t $ is a wall, output $ -1 $. If no path exists initially (due to walls), output $ 0 $. Otherwise, compute the minimum vertex cut (restricted to door vertices) separating $ s $ from $ t $. Formally: Let $ D \subseteq V $ be the set of door vertices. Find $ \min \{ |X| \mid X \subseteq D, \text{ and } s \not\leftrightarrow t \text{ in } G - X \} $. If impossible (i.e., $ s $ or $ t $ is blocked by wall), output $ -1 $.
API Response (JSON)
{
  "problem": {
    "name": "H. Optimize DFS",
    "description": {
      "content": "You are given a tree with $n$ vertices. Every vertex $v$ has two values $a_v$ and $b_v$. You have to process $q$ queries of types:  First, create an array $\"used\"[ 1 \\\\dots n ]$ filled with _false_ i",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10286H"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a tree with $n$ vertices. Every vertex $v$ has two values $a_v$ and $b_v$. You have to process $q$ queries of types: \n\nFirst, create an array $\"used\"[ 1 \\\\dots n ]$ filled with _false_ i...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ G = (V, E) $ be a grid graph with $ R $ rows and $ C $ columns, where each cell $ (i, j) $ is a vertex.  \nLet $ s = (1, 1) $ be the start vertex and $ t = (R, C) $ be the targe...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments