Group Division
InterviewTime limit1sMemory limit1024 MB
Given students and their friend pairs, find the largest number of groups where every friend pair stays together, which equals the count of friend clusters.
- Level
Easy3 of 10
- Topics
- Union-find, Graph, Hash map
- Solved
- No attempts yet
Problem
During a school trip, the students must be divided into groups. Naturally, the students want to be in the same group as their friends. Given the name of each student and which students are friends with each other, write a program that computes the maximum number of groups the students can be divided into while every student gets the group they want.
Input
The first line contains an integer (), the number of students on the trip. The next lines each contain one student's name. Each name is between 1 and 20 characters long and contains only the letters A-Z. All names are different.
The next line contains an integer (), the number of friend pairs. The last lines each contain a friend pair as two names separated by a space.
Output
Print one line with one integer: the maximum number of groups the students can be divided into.