Subsets
InterviewTime limit1sMemory limit128 MB
Given inequalities where a set name contains either an element or another set name, find each named set's minimal required elements.
- Level
Medium5 of 10
- Topics
- Graph, DFS, Topological sort, Implementation
- Solved
- No attempts yet
Problem
Write a program that, given a collection of set inequalities, finds the minimal set of elements that each set name must take.
A set inequality has the form X contains S, where is any set name and is either a set name or a set element.
- If is a set name, the inequality means that is a superset of or equal to , that is, .
- If is an element, the inequality means that contains the element .
Set names are the uppercase letters through , and elements are the lowercase letters through .
For each set name that appears in the input, determine its minimal set: the smallest set of elements the name must take so that all of the inequalities hold.
Input
The first line contains the number of set inequalities .
Each of the next lines contains one set inequality in the form X contains S.
Output
Print every set name that appears in the input, in alphabetical order. For each set name, print its minimal set with the elements listed in alphabetical order, using the format below.
Name = {elements}
For example, if the minimal set of consists of the elements and , print A = {c,d}. If a set has no elements, print empty braces, e.g. Q = {}.