J. Jackie's Jack-O'-Lanterns

Codeforces
IDCF10278J
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
Each year in the middle of October, Jackie hosts a fabulous pumpkin carving contest. Participants from all across the neighborhood bring their elaborate jack-o'-lanterns to Jackie for her to judge and pick a winner. Of course, the jack-o'-lanterns in Jackie's contest are no ordinary jack-o'-lanterns. In fact, these carved pumpkins don't just have holes carved into them, but they can have floating pumpkin pieces inside of the carved holes, held up by magic (or perhaps conveniently placed toothpicks) such that the holes appear like dark rings. Further, these floating pumpkin pieces could be carved with holes that themselves could contain floating pumpkin pieces and so on! Now Jackie takes this contest very seriously and wants to make sure that each submitted jack-o'-lantern is entirely unique. She will not tolerate even remote plagiarism! Thus Jackie demands that each jack-o'-lantern represent a unique pumpkin _topology_. Jackie defines a pumpkin _topology_ to be the set of hierarchical relations between pumpkin pieces and embedded holes where both pumpkin pieces and holes are considered as distinct components in the topology. For instance, there may be several holes cut within the outer uncut pumpkin, and each distinct hole would be considered a descendant of the outer pumpkin flesh. Then, perhaps within one of these holes there might be one or several embedded floating pumpkin pieces, which would each be considered a descendant of the surrounding hole. Further within one of these pumpkin pieces could be other embedded holes which are descendants of the surrounding piece, etc. The carved-hole shapes and sizes can vary, but Jackie only cares about ensuring that no two jack-o'-lanterns share the same pumpkin _topology_. Given the appearance of two jack-o'-lanterns, help Jackie enforce her contest rules. The first line of input contains two integers, $R$ and $C$ ($3 <= R, C <= 150$), the dimension of the carved pumpkins in rows and columns respectively. Next, $R$ lines follow, each containing $C$ characters which are each either '#' to indicate a hole space or '.' to indicate pumpkin flesh. These lines describe the first jack-o'-lantern. Next follows a blank line for visual spacing, and after that, in the same format as the first jack-o'-lantern description, another $R$ lines follow which represent the second jack-o'-lantern that Jackie is comparing. Note that the first and last rows and columns of each jack-o'-lantern will be filled with '.' or pumpkin flesh. Every hole or pumpkin piece will be surrounded entirely by only one parent pumpkin piece or hole respectively. Two character's both representing either pumpkin flesh or hole spaces are considered adjacent and thus connected as part of the same topological unit if they are directly above, below, or beside one another (not diagonally). Print 'YES' (without quotes) if the jack-o'-lanterns share the same topology and 'NO' otherwise. In the first sample case, the two given pumpkins are topologically equivalent, sharing the topology represented by the tree below, where white nodes denote pumpkin flesh pieces and black nodes denote carved holes. The white root of the tree represents the outer pumpkin flesh. The two black "eye" holes of the first jack-o'-lantern are represented by the black nodes with one child each. The "nose" hole is represented by the black node with no children. Then the "mouth" hole containing four "teeth" pumpkin pieces is represented by the black node with four children pieces. Note that the second, more abstract pumpkin in this sample case contains the same hierarchically-ordered components. ## Input The first line of input contains two integers, $R$ and $C$ ($3 <= R, C <= 150$), the dimension of the carved pumpkins in rows and columns respectively. Next, $R$ lines follow, each containing $C$ characters which are each either '#' to indicate a hole space or '.' to indicate pumpkin flesh. These lines describe the first jack-o'-lantern. Next follows a blank line for visual spacing, and after that, in the same format as the first jack-o'-lantern description, another $R$ lines follow which represent the second jack-o'-lantern that Jackie is comparing.Note that the first and last rows and columns of each jack-o'-lantern will be filled with '.' or pumpkin flesh. Every hole or pumpkin piece will be surrounded entirely by only one parent pumpkin piece or hole respectively. Two character's both representing either pumpkin flesh or hole spaces are considered adjacent and thus connected as part of the same topological unit if they are directly above, below, or beside one another (not diagonally). ## Output Print 'YES' (without quotes) if the jack-o'-lanterns share the same topology and 'NO' otherwise. [samples] ## Note In the first sample case, the two given pumpkins are topologically equivalent, sharing the topology represented by the tree below, where white nodes denote pumpkin flesh pieces and black nodes denote carved holes. The white root of the tree represents the outer pumpkin flesh. The two black "eye" holes of the first jack-o'-lantern are represented by the black nodes with one child each. The "nose" hole is represented by the black node with no children. Then the "mouth" hole containing four "teeth" pumpkin pieces is represented by the black node with four children pieces. Note that the second, more abstract pumpkin in this sample case contains the same hierarchically-ordered components. In the second sample case, the second given jack-o'-lantern is not topologically equivalent to the first but instead bears the topology represented by this tree below. Note that it is a somewhat similar and would be topologically equivalent to the tree represented above if the black node containing two children below actually contained four instead.
**Definitions** Let $ G_1 = (V_1, E_1) $ and $ G_2 = (V_2, E_2) $ be two grid-based 4-connected graphs representing the two jack-o'-lanterns, where: - Each cell is a vertex $ v \in V_i $ with label $ \ell(v) \in \{ \text{`.`}, \text{`#'} \} $, denoting pumpkin flesh or hole, respectively. - An edge exists between two vertices if they are adjacent (up/down/left/right) and share the same label. - Connected components of `.` are *pumpkin pieces*; connected components of `#` are *holes*. Let $ T_1 $ and $ T_2 $ be the hierarchical topological trees derived from $ G_1 $ and $ G_2 $, respectively, where: - The root is the unique outer pumpkin piece (connected component of `.` touching the grid boundary). - Each hole is a child of the pumpkin piece or hole that immediately surrounds it (i.e., the minimal enclosing connected region of opposite type). - Each pumpkin piece (non-root) is a child of the hole that encloses it. - Tree structure reflects inclusion: a node is a descendant of another iff its region is entirely contained within the other’s region. **Constraints** 1. $ 3 \leq R, C \leq 150 $ 2. Boundary cells (first/last row/column) are all `.` 3. Each connected component of `.` or `#` is simply connected (no holes within holes of same type). 4. Every hole is fully enclosed by exactly one pumpkin piece or hole; every non-root pumpkin piece is fully enclosed by exactly one hole. **Objective** Determine whether $ T_1 \cong T_2 $ (i.e., the two trees are isomorphic as rooted ordered trees, where child order does not matter — unordered tree isomorphism). Output `"YES"` if isomorphic, `"NO"` otherwise.
API Response (JSON)
{
  "problem": {
    "name": "J. Jackie's Jack-O'-Lanterns",
    "description": {
      "content": "Each year in the middle of October, Jackie hosts a fabulous pumpkin carving contest. Participants from all across the neighborhood bring their elaborate jack-o'-lanterns to Jackie for her to judge and",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10278J"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Each year in the middle of October, Jackie hosts a fabulous pumpkin carving contest. Participants from all across the neighborhood bring their elaborate jack-o'-lanterns to Jackie for her to judge and...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ G_1 = (V_1, E_1) $ and $ G_2 = (V_2, E_2) $ be two grid-based 4-connected graphs representing the two jack-o'-lanterns, where:  \n- Each cell is a vertex $ v \\in V_i $ with labe...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments