Benchmark AI / Public workspace

Humanity's Last Code Exam / 2022_P / Turning Red

Problem

Answer published by the source. Consult the official source to check your work against its answer.

platform

atcoder

question content

**Problem Description:** Mei’s parents have spent the last year remodeling their house, but their lighting system is quite complex! Each room in the house has an LED light, which can be set to red, green, or blue. Throughout the house are various buttons which are each connected to one or more lights. When a button is pressed, any red lights connected to that button become green, any green lights connected to that button become blue, and any blue lights connected to that button become red. Each button can be pressed multiple times. Because the house was built prior to the invention of crossbar wiring, each light is controlled by at most two buttons. Mei’s favorite color is red, so she wants to turn all of the lights red. Her parents, fearing the buttons will wear out, have asked her to minimize the total number of button presses. **Input:** - The first line of input contains two positive integers l l and b b , where 1l2×105 1 \leq l \leq 2 \times 10^5 is the number of lights and 0b2×l 0 \leq b \leq 2 \times l is the number of buttons. - The second line of input is a string of l l characters, all either R, G, or B, where the i i -th character is the initial color of the i i -th light. - The next b b lines describe the buttons. Each of these lines begins with an integer k k (1kl 1 \leq k \leq l ), the number of lights controlled by this button. Then k k distinct integers follow, the lights controlled by this button. The lights are indexed from 1 to l l , inclusive. Each light appears at most twice across all buttons. **Output:** - Output the minimum number of button presses Mei needs to turn all the lights red. If it is impossible for Mei to turn all of the lights red, output "impossible". **Sample Input 1:**
Plain-text mathematical notation (without MathML)
**Problem Description:**

Mei’s parents have spent the last year remodeling their house, but their lighting system is quite complex! Each room in the house has an LED light, which can be set to red, green, or blue. Throughout the house are various buttons which are each connected to one or more lights. When a button is pressed, any red lights connected to that button become green, any green lights connected to that button become blue, and any blue lights connected to that button become red. Each button can be pressed multiple times. Because the house was built prior to the invention of crossbar wiring, each light is controlled by at most two buttons.

Mei’s favorite color is red, so she wants to turn all of the lights red. Her parents, fearing the buttons will wear out, have asked her to minimize the total number of button presses.

**Input:**

- The first line of input contains two positive integers l and b, where 1≤l≤2×10⁵ is the number of lights and 0≤b≤2×l is the number of buttons.
- The second line of input is a string of l characters, all either R, G, or B, where the i-th character is the initial color of the i-th light.
- The next b lines describe the buttons. Each of these lines begins with an integer k (1≤k≤l), the number of lights controlled by this button. Then k distinct integers follow, the lights controlled by this button. The lights are indexed from 1 to l, inclusive. Each light appears at most twice across all buttons.

**Output:**

- Output the minimum number of button presses Mei needs to turn all the lights red. If it is impossible for Mei to turn all of the lights red, output "impossible".

**Sample Input 1:**

Original LaTeX notation
**Problem Description:**

Mei’s parents have spent the last year remodeling their house, but their lighting system is quite complex! Each room in the house has an LED light, which can be set to red, green, or blue. Throughout the house are various buttons which are each connected to one or more lights. When a button is pressed, any red lights connected to that button become green, any green lights connected to that button become blue, and any blue lights connected to that button become red. Each button can be pressed multiple times. Because the house was built prior to the invention of crossbar wiring, each light is controlled by at most two buttons.

Mei’s favorite color is red, so she wants to turn all of the lights red. Her parents, fearing the buttons will wear out, have asked her to minimize the total number of button presses.

**Input:**

- The first line of input contains two positive integers \( l \) and \( b \), where \( 1 \leq l \leq 2 \times 10^5 \) is the number of lights and \( 0 \leq b \leq 2 \times l \) is the number of buttons.
- The second line of input is a string of \( l \) characters, all either R, G, or B, where the \( i \)-th character is the initial color of the \( i \)-th light.
- The next \( b \) lines describe the buttons. Each of these lines begins with an integer \( k \) (\( 1 \leq k \leq l \)), the number of lights controlled by this button. Then \( k \) distinct integers follow, the lights controlled by this button. The lights are indexed from 1 to \( l \), inclusive. Each light appears at most twice across all buttons.

**Output:**

- Output the minimum number of button presses Mei needs to turn all the lights red. If it is impossible for Mei to turn all of the lights red, output "impossible".

**Sample Input 1:**

Code

8 6
GBRBRRRG
2 1 4
1 2
4 4 5 6 7
3 5 6 7
1 8
1 8

**Sample Output 1:**

Code

8

**Sample Input 2:**

Code

4 3
RGBR
2 1 2
2 2 3
2 3 4

**Sample Output 2:**

Code

impossible

**Sample Input 3:**

Code

4 4
GBRG
2 1 2
2 2 3
2 3 4
1 4

**Sample Output 3:**

Code

6

**Sample Input 4:**

Code

3 3
RGB
1 1
1 2
1 3

**Sample Output 4:**

Code

3

question title

Turning Red

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import