Antibody Recognition
InterviewTime limit1sMemory limit1024 MB
Given two grids, decide whether the second can result from picking one start cell, flooding its monochromatic connected component, and repainting that whole component one color.
- Level
Medium6 of 10
- Topics
- BFS, DFS, Graph, Implementation
- Solved
- No attempts yet
Problem
VUNO develops Medical AI solutions that help medical experts make decisions, using artificial intelligence trained with big data and deep learning.
VUNO recently developed a powerful new imaging technique called SP. With this technique, human tissue is represented as a grid, and each cell of the grid is assigned a single data value that compresses various analysis results for that part. VUNO wants to study a new antibody called CPCU-1202 using the SP imaging technique.
When the CPCU-1202 vaccine is injected into tissue, an antibody is created in one of the grid cells. If there is a cell that is adjacent up, down, left, or right and has the same data value as the cell the antibody currently occupies, the antibody spreads to that cell. This process repeats until the antibody can no longer spread, at which point the antibody is fully absorbed into the tissue. As a result, the data values of all cells the antibody spread to are updated to some single identical value. It is possible that this updated value happens to equal the original data value.
VUNO's research data consists of a pair: the imaging result before the vaccine is injected into one tissue, and the imaging result after the vaccine is injected. Given the two imaging results, write a program that determines whether the vaccine injected into this tissue could be the CPCU-1202 vaccine.
Input
The first line gives two integers and , the size of the SP imaging result. () This means the imaging result is a grid with rows and columns.
The next lines give the imaging result before the vaccine is injected. Each line contains integers between and , separated by spaces. The -th number on the -th line is the data value of the cell at row , column of the imaging result.
The next lines give the imaging result after the vaccine is injected, in the same format as above.
Output
If the vaccine applied to the subject could be the CPCU-1202 vaccine, print YES. Otherwise, print NO.



