Alias Ledger
A build system lets teams give several names to the same tool: cc, gcc and clang_wrap may
all mean one compiler. The names are declared in an alias file, a plain text file in which every
line asserts that some names are aliases of each other, or that two names must stay apart.
Two names are in the same group if the == assertions of the whole file connect them,
directly or through a chain of other names: if a == b and b == c are asserted anywhere in the
file, then a and c are in the same group, no matter in which order the lines appear. Every name
is in the same group as itself.
The file is consistent if no != assertion names two names of the same group. Your job is to
read the file and either count its groups or point to the first broken != line.
File format
The file consists of lines numbered from 1. Every line is terminated by a line feed character, except that the last line may or may not have one (a line feed at the very end of the file does not start a new line). The file contains no other control characters except tabs; in particular there are no carriage returns.
Below, whitespace means a run of zero or more spaces and tabs. It may appear at the start and the end of any line and between any two tokens, but never inside an identifier or an operator.
An identifier is a string of 1 to 32 characters that starts with a letter (A–Z, a–z) or
an underscore _, followed by letters, digits 0–9, underscores and dots .. So lib.so.1,
_x9 and Python3.12 are identifiers. Identifiers are case-sensitive: Foo and foo are
different names.
Each line has exactly one of the following forms:
- A blank line: empty, or whitespace only.
- A comment line: whitespace, then
#, then any text up to the end of the line. The comment text may contain any characters with codes 32 to 126 and tabs, including#,==and!=; it is ignored entirely. - An alias line: between 2 and 10 identifiers separated by the operator
==, for examplecc == gcc == clang_wrap. It asserts that all of these identifiers are in the same group. The same identifier may occur several times in one line. - A separation line: exactly two identifiers separated by the operator
!=, for examplegcc != javac. The two identifiers may be equal.
An alias line or a separation line may be followed by a trailing comment: optional whitespace,
then # and any text up to the end of the line, as in a comment line. The whitespace around
operators and before # is optional, so a==b#note is a valid alias line.
Input
The whole input is the alias file. It has between 1 and 10^5 lines, every line is at most 500 characters long (not counting the line feed), and the whole input is at most 6·10^6 characters long. Every line is well formed as described above.
Output
Let the identifiers of the file be all distinct identifiers that occur in alias and separation lines (identifiers that occur only inside comments do not count).
If the file is consistent, print CONSISTENT followed by a space and the number of groups among
the identifiers of the file. If the file contains no identifiers, that number is 0.
Otherwise print CONFLICT followed by a space and the number of the first line (the smallest line
number) that is a separation line whose two identifiers are in the same group.
Notes
In the first example the groups are { cc, gcc, clang_wrap }, { cxx, gxx },
{ ld, ld.gold, ld.bfd } and { javac }, so the answer is 4. Note that javac occurs only in
a separation line but still forms a group of its own, and that the text gcc != gxx inside the
comment on line 2 is ignored.
In the second example, line 3 says lib.a != lib.b, and when that line is read nothing connects
the two names yet. But lines 1, 4 and 6 assert lib.a == libA, libA == lib.c and
lib.b == lib.c, which put lib.a and lib.b into the same group, so line 3 is broken. Line 5 is
fine because LIB.A and lib.a are different names. Line 7 is broken too, but line 3 comes first.