School Graduation
Time limit2sMemory limit1024 MB
Given an N by M grid of class letters, find the maximum number of colors so that cells in a shared column or class share a color.
- Level
Medium6 of 10
- Topics
- Union-find, Graph, Hash map, Implementation
- Solved
- No attempts yet
Problem
The school administration has run into a problem with the upcoming graduation ceremony, and they hope you can help them solve it. During the ceremony, the students will stand in rows with students in each row. The administration wants the ceremony to be as colorful as possible, so they will hand out hats in various colors to the students.
For the arrangement to look nice, every student in the same column must have the same hat color. So that nobody feels left out, every student in the same class must also have the same hat color. Each student's row and column are already decided, but not their hat color. The administration needs your help assigning hat colors to the students so that the ceremony is as colorful as possible.
Write a program that, given how the students will be arranged at the graduation ceremony, computes the maximum number of unique hat colors that can be assigned to the students.
Input
The first line contains three integers , (), and (): the number of rows, the number of columns, and the number of classes.
The following lines each contain characters and describe how the students will be arranged at the graduation ceremony. The character in row , column is an uppercase letter between A and the -th letter of the alphabet: the class that the student in row , column belongs to. There is guaranteed to be at least one student from each class.
Output
Print one integer: the maximum number of unique hat colors that can be assigned to the students so that every student in the same column, and every student in the same class, has the same hat color.
Hint
In the first sample, the second column contains one student from class A and one from class B. Since both of these students must have the same hat color, all of class A must have the same color as all of class B. It follows that every student at the ceremony must have the same color, so the answer is .
In the second sample, class A and class B must have the same color, since the first column contains a student from each of these two classes. Class C, however, can be given a different hat color. The answer is then .
In the third sample, we can give each class its own color, since no two students from different classes appear in the same column. The answer is .
In the last sample, we can assign one color to all students from classes A, B, and C, and another color to all students from classes D and E. The answer is .