eolymp
bolt
Try our new interface for solving problems
Məsələlər

Trust groups

Trust groups

The personnel department of \textit{Association of Cookie Monsters} (ACM) has noticed that the productivity of various work groups in the company is not as good as it could be. They have interviewed the employees in the aff ected groups and they have detected the root of the problem: trust (or, rather, the lack thereof). Some employees do not trust the rest of the group, and this is decreasing their motivation and happiness. The personnel department wants to solve this problem, and has decided to reorganize the groups so that they are \textit{stable}, i.e., they are formed by people who trust each other. They have asked the employees, and they know the people each employee trusts directly. Moreover, if employee \textbf{A} trusts employee \textbf{B} and employee \textbf{B} trusts employee \textbf{C}, then employee \textbf{A} will trust employee \textbf{C}. And obviously, each employee trusts himself. They want to create as few groups as possible to reduce administration overhead (they also do not want to work too hard). With this information they have contacted you, and asked you to write a program that nds the minimum number of \textit{stable} groups that can be created. \InputFile The input consists of several test cases. Each test case begins with a line containing two positive integers \textbf{P} and \textbf{T} (\textbf{1 }≤ \textbf{P} ≤ \textbf{1000}, \textbf{0} ≤ \textbf{T} ≤ \textbf{999000}) separated by a single space. \textbf{P} lines come next, each containing the name of one person. The names will have the following format: surname, a comma, a space and first name (for example McBride, John or Smith, Peter). Both the surname and the first name will be strings of uppercase or lowercase characters (with no blanks or punctuation marks), with a maximum length of \textbf{10} characters. There will not be repetitions in the complete names of the people. After the names there will appear \textbf{T} blocks of \textbf{2} lines representing the trust relations between people. Each line of the block will contain the name of a person in the same format as before, and the block will mean that the person in the first line trusts the person in the second line. All people appearing in the confi dence relations will have appeared in the previous list of \textbf{P} people. The input will end with the "phantom" test case \textbf{0 0}, which must not be processed. \OutputFile For each test case, the output will be a line containing a positive integer representing the minimum number of \textit{stable}groups of people that can be formed.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
3 2
McBride, John
Smith, Peter
Brown, Anna
Brown, Anna
Smith, Peter
Smith, Peter
Brown, Anna
3 2
McBride, John
Smith, Peter
Brown, Anna
Brown, Anna
Smith, Peter
McBride, John
Smith, Peter
0 0
Çıxış verilənləri #1
2
3