一道ACM题目,关于图论
题目:Characters in Star Wars each speak a language, but they typically understand a lot more languages that they don’t or can’t speak. For example, Han Solo might speak in Galactic Basic and Chewbacca might respond in Shyriiwook; since they each understand the language spoken by the other, they can communicate just fine like this.
We’ll say two characters can converse if they can exchange messages in both directions. Even if they didn’t understand each other’s languages, two characters can still converse as long as there is a sequence of characters who could translate for them through a sequence of intermediate languages. For example, Jabba the Hutt and R2D2 might be able to converse with some help. Maybe when Jabba spoke in Huttese, Boba Fett could translate to Basic, which R2D2 understands. When R2D2 replies in Binary, maybe Luke could translate to Basic and then Bib Fortuna could translate back to Huttese for Jabba.
In Star Wars Episode IV, there’s a scene with a lot of different characters in a cantina, all speaking different languages. Some pairs of characters may not be able to converse (even if others in the cantina are willing to serve as translators). This can lead to all kinds of problems, fights, questions over who shot first, etc. You’re going to help by asking some of the patrons to leave. The cantina is a business, so you’d like to ask as few as possible to leave. You need to determine the size of the smallest set of characters S such that if all the characters in S leave, all pairs of remaining characters can converse.
For example, in the first sample input below, Chewbacca and Grakchawwaa can converse, but nobody else understands Shyriiwook, so they can’t converse with others in the bar. If they leave, everyone else can converse. In the second sample input, Fran and Ian can converse, as can Polly and Spencer, but no other pairs of characters can converse, so either everyone but Polly and Spencer must leave or everyone but Fran and Ian.
Input
Input starts with a positive integer, 1≤N≤100, the number of characters in the cantina. This is followed by N lines, each line describing a character. Each of these N lines starts with the character’s name (which is distinct), then the language that character speaks, then a list of 0 to 20 additional languages the character understands but doesn’t speak. All characters understand the language they speak. All character and language names are sequences of 1 to 15 letters (a-z and A-Z), numbers, and hyphens. Character names and languages are separated by single spaces.
Output
Print a line of output giving the size of the smallest set of characters S that should be asked to leave so that all remaining pairs of characters can converse.
中文翻译:
在星际交流中,往往由于语言不通所以会导致冲突。每个星球都有自己的语言,比如星球1 能说英语并且能听懂法语(能听懂法语只是能听懂,但不能用法语回答)。星球2能说法语并且能听懂英语,所以他们能交流(星球一说英语后星球二听懂了,用法语回答,星球一也能听懂)。另外一个例子,星球1能说英语并且听的懂法语,但是星球而能说法语但是只能听的懂西班牙语,所以他们不能交流)。题目要求输入N个星球例子,输入星球名字, 能说的语言,能听的懂的语言(能听的懂的语言可以有很多种,但是能说的只能一种)。输入后你的任务是从中挑出最少的不能跟其他交流的星球的个数(意思是踢出这些星球后其他星球都可以交流)。//可以通过第三方交流 星球一说的话如果星球二不懂,但是星球三懂,星球三可以成为中间的翻译。
程序举例:
输入
6
earth0 French Italian
earth1 English German
earth2 German Italian
earth3 Italian French Spanish
earth4 Spanish Portugese
earth5 Portugese Spanish
输出:
4
//原题链接: https://naq15.