Hide

Venn Intervals

A Venn interval diagram is a graphical way to illustrate intersections of sets in 1D, similar to the more familiar Venn diagrams in 2D. A Venn interval diagram is defined by assigning a non-zero-length integer interval $[l_ i, u_ i)$ to each set $S_ i$. The regions where different intervals overlap correspond to intersections of different combinations of the sets (see Figure 1).

\includegraphics[width=0.8\textwidth ]{venn}
Figure 1: Venn interval diagram corresponding to Sample Input 1. The intervals assigned to each of the six sets $A$ through $F$ are shown at the top; the regions encoded by the diagram are labeled along the number line. This Venn interval diagram has nine regions, and is not degenerate (since no interval is contained in any other.)

More formally, given the interval assigned to each set, you can compute the regions that appear in the Venn interval diagram with the following algorithm:

  • Combine together all of the interval starting points $l_ i$ and ending points $u_ i$ into a single set, and sort them to get an integer sequence $p_1 < p_2 < \cdots < p_ m$.

  • Label each interval $(p_ j, p_{j+1})$ spanning consecutive $p$ values with all the sets $S_ i$ for which $(p_ j, p_{j+1})$ is contained within $[l_ i, u_ i)$. (Note that partial overlaps are not possible: the $p$-interval is either contained within, or disjoint from, each set-interval).

  • The set of all non-empty labels are the regions of the Venn interval diagram. Each label $\{ S_ a, S_ b, \ldots \} $ represents an intersection of sets $S_ a \cap S_ b \cap \cdots .$

In the example Venn interval diagram shown above, there are six sets ($A$ through $F$) and nine regions ($B$, $A \cap B$, $A$, $A\cap C$, $A \cap C \cap D$, $C \cap D$, $D$, $E$, and $F$). This example Venn interval diagram has a “hole” where no sets overlap (between the end of region $D$ and the start of $E$); this is allowed. Note also that $E\cap F$ is not a region: $E$ and $F$’s intervals touch at their endpoints, but the two do not overlap on a $p$-interval.

Not every list of regions has a corresponding illustration as a Venn interval diagram. Consider for example the regions $A\cap B$, $B \cap C$, and $C\cap A$. There is no way to lay out intervals for $A$, $B$, and $C$ on the real line to form exactly these three regions (any Venn interval diagram that includes these three regions must also include $A \cap B \cap C$). In addition, a Venn interval diagram is degenerate if any interval is contained within another interval: if $l_ i \leq l_ j$ and $u_ j \leq u_ i$ for some $i\neq j$. For example, if $C$’s right endpoint were at $4$ instead of $5$, the Venn interval diagram in Figure 1 would become degenerate, since the interval assigned to the set $C$ would then be contained within $A$’s interval (corresponding to Sample Input 3).

Given a list of regions, construct a non-degenerate Venn interval diagram containing exactly those regions, if possible.

Input

The first line of the input contains a single integer $n$, the number of regions required in your diagram $(1 \leq n \leq 4\, 000).$ The next $n$ lines each describe one region. Each line begins with an integer $k$, the number of sets that intersect to form the region $(1 \leq k \leq 2\, 000)$, followed by $k$ space-separated set names. Each set name contains only lowercase or uppercase letters (a-z, A-Z) and no set name is longer than ten characters. All set names listed on the same line are distinct, and no two regions list the exact same collection of set names. No more than $2\, 000$ unique set names appear in total across all regions.

Output

If a non-degenerate Venn interval diagram does not exist whose regions match the input, print IMPOSSIBLE and produce no further output.

Otherwise, print the intervals describing any one valid diagram. For each set name that appears in the input, print a line of output starting with that set name, followed by two space-separated integers $l$ and $r$: the left and right endpoints of the interval assigned to that set name in your diagram. The endpoints must satisfy $-10^6 \leq l < u \leq 10^6.$ You may list the sets in any order, but every set name that appears in the input must correspond to exactly one line of output.

Every region in the input must appear at least once in your Venn interval diagram, and your diagram must not contain any regions other than those specified in the input. You diagram must not be degenerate: no interval should be enclosed inside another.

Sample Input 1 Sample Output 1
9
1 A
2 A B
1 B
2 A C
3 A C D
2 C D
1 D
1 E
1 F
B -1 1
A 0 4
C 2 5
D 3 6
E 7 8
F 8 9
Sample Input 2 Sample Output 2
3
2 A B
2 B C
2 C A
IMPOSSIBLE
Sample Input 3 Sample Output 3
8
1 A
2 A B
1 B
2 A C
3 A C D
1 D
1 E
1 F
IMPOSSIBLE
CPU Time limit 3 seconds
Memory limit 1024 MB
Difficulty 7.9hard
Statistics Show
Author
License Creative Commons License (cc by-sa)

Please log in to submit a solution to this problem

Log in