Drone Photo of the Stand
Limits: 2 sec., 256 MiB
The school stadium is getting ready for a celebration. The stand has \(n\) rows with \(m\) seats in each row, and a student sits in every seat. The rows are numbered from 1 to \(n\), and the seats in each row from 1 to \(m\). Every student holds a card above their head: yellow or blue.
The principal wants to take a photo of the stand from a drone so that the photo shows cards of only one color. It is too late to change the cards, so she decided to send some of the students to fetch water: exactly one whole row and exactly one column, that is, all the students sitting in the seat with the same number in every row. The student sitting at the intersection of the chosen row and column goes for water too.
After that, \((n - 1) \cdot (m - 1)\) students remain on the stand. The photo is successful if all their cards are of the same color: all yellow or all blue.
Determine whether it is possible to choose a row number \(r\) and a seat number \(c\) so that the photo is successful.
Input
The first line contains two integers \(n\) and \(m\) – the number of rows and the number of seats in each row.
Each of the next \(n\) lines
contains a string of length \(m\)
consisting of the characters Y and B.
Character number \(c\) in line number
\(r\) describes the card of the student
in seat \(c\) of row \(r\): Y means a yellow card,
B means a blue one.
Output
In the only line, print Yes if a row and a seat can be
chosen so that the photo is successful, or No
otherwise.
Constraints
\(2 \le n, m \le 2 \cdot 10^5\),
\(n \cdot m \le 2 \cdot 10^5\).
The scoring consists of the following blocks:
1 point for each example from the statement,
10 points: there are at most 3 yellow cards or at most 3 blue cards,
20 points: \(n \cdot m \le 1000\),
20 points: \(n = 2\),
20 points: \(n, m \le 500\),
27 points: no additional constraints.
You get the points for a block only if your program passes all tests of the block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 5 YYBYY YYYYY BBBYB YYYYY | Yes |
| Input (stdin) | Output (stdout) |
|---|---|
| 2 3 YBB YYB | Yes |
| Input (stdin) | Output (stdout) |
|---|---|
| 3 4 BYYY YYBY YYYB | No |
Notes
In the first example, all the blue cards are in the third row or in the third seat. So one can choose \(r = 3\), \(c = 3\), after which only yellow cards remain. Note that the blue card in the third seat of the third row belongs both to the chosen row and to the chosen column.
In the second example, two ways work:
the first row and the third seat go for water, then two yellow cards remain;
the second row and the first seat go for water, then two blue cards remain.
In the third example, the three blue cards are in three different
rows and in three different seats, so one row and one column remove at
most two of them. Leaving only blue cards does not work either: after
the choice, 6 students remain on the stand, but there are only 3 blue
cards. So the answer is No.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|