Logo
(追記) (追記ここまで)

31890번 - Turning Red 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
3 초 1024 MB106323141.892%

문제

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, as seen in Figure P.1.

Figure P.1: The initial state of the lights in Sample Input 1. Buttons and wires not shown.

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.

입력

The first line of input contains two positive integers $l$ and $b,ドル where $l$ (1ドル≤l≤2 \cdot 10^5$) is the number of lights and $b$ (0ドル≤b≤2 \cdot 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 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.

제한

예제 입력 1

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

예제 출력 1

8

예제 입력 2

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

예제 출력 2

impossible

예제 입력 3

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

예제 출력 3

6

예제 입력 4

3 3
RGB
1 1
1 2
1 3

예제 출력 4

3

힌트

출처

ICPC > World Finals > ICPC World Finals 2022 P번

ICPC > World Finals > ICPC World Finals 2023 G번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /