#5408. Problem 2. Moo Hunt

0

Problem 2. Moo Hunt

Problem 2. Moo Hunt

USACO 2026 Second Contest, Bronze

Bessie is playing the popular game "Moo Hunt". In this game, there are NN (3N203 \le N \le 20) cells in a line, numbered from 11 to NN. All cells either have the character MM or OO with the ii-th cell having character sis_i.

Bessie plans to perform KK (1K21051 \le K \le 2 \cdot 10^5) mooves. On her ii-th moove, Bessie will tap 33

different

cells (xi,yi,zix_{i},y_{i},z_{i}) (1xi,yi,ziN1 \le x_{i},y_{i},z_{i} \le N). Bessie will earn a point if sxi=Ms_{x_i}=M and syi=szi=Os_{y_i}=s_{z_i}=O. In other words, Bessie will earn a point if she forms the string MOOMOO by tapping cells xi,yi,zix_{i},y_{i},z_{i} in that order.

Farmer John wants to help Bessie get a new high score. He wants you to find the maximum possible score Bessie could get across all possible boards if she performs the KK mooves as well as the number of different boards that will allow Bessie to achieve this maximum possible score. Two boards are different if there exists a cell such that the corresponding characters at the cell are different.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains NN and KK, the number of cells and the number of mooves Bessie will perform.

Each of the next KK lines contains xi,yi,zix_i, y_i, z_i describing Bessie's ii-th move (xi,yi,zix_i, y_i, z_i are pairwise distinct).

OUTPUT FORMAT (print output to the terminal / stdout):

Output the maximum possible score Bessie could achieve, followed by the count of different boards that will allow Bessie to achieve this maximum score.

SAMPLE INPUT:


5 6
1 2 3
1 2 3
1 3 5
2 3 4
5 3 2
5 2 3

SAMPLE OUTPUT:


4 2

The boards MOOOMMOOOM and MOOMMMOOMM allow Bessie to achieve a maximum score of 44. In both boards, Bessie will earn points on mooves 1,2,5,61,2,5,6. It can be shown that this is the maximum score Bessie can achieve, and those two boards are the only possible boards allowing Bessie to achieve a score of 44.

SAMPLE INPUT:


6 12
2 4 3
2 3 4
3 5 2
3 5 1
3 1 5
3 1 2
6 1 5
1 6 4
2 3 6
3 6 2
4 1 6
3 4 2

SAMPLE OUTPUT:


6 3

The boards that allow Bessie to achieve a maximum possible score of 66 are OOMOOOOOMOOO, OOMMOOOOMMOO, and OOMOOMOOMOOM.

SCORING: Inputs 3-5: N8,K104N \le 8, K \le 10^4Inputs 6-12: There will be one test for each N{14,15,16,17,18,19,20}N \in \{14,15,16,17,18,19,20\} with no additional constraints on KK.

Problem credits: Alex Liang