Liars and Truth Tellers
InterviewTime limit1sMemory limit128 MB
Each statement says whether one cow calls another honest or a liar; find the largest prefix of statements that admits a consistent truth/lie labeling of all cows.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, Binary search, Implementation
- Solved
- No attempts yet
Problem
A farmer has spent so much time with his cows that he has begun to understand their language. He notices that among his cows (), some always tell the truth and the rest always lie.
The farmer writes down statements () made by the cows. Each statement has one of two forms:
x y T— cow claims that cow always tells the truth.x y L— cow claims that cow always lies.
Every statement is about a pair of two different cows, and the same pair of cows may appear in several statements.
The farmer suspects that he copied some entries down incorrectly, so there may be no way to label every cow as a truth teller or a liar that is consistent with all statements. To salvage as much of the list as possible, find the largest value of for which some labeling of the cows as truth tellers or liars is consistent with the first statements.
Input
- Line 1: two space-separated integers and .
- Lines 2 to : each line has the form
x y Torx y L, describing one statement made by cow about cow (, ).
Output
- Line 1: the largest value of such that some assignment of "truth teller" or "liar" to each of the cows is consistent with the first statements.
Hint
In the sample, statements 1 and 3 cannot both hold, but statements 1 and 2 can: let cows 1, 2, and 3 tell the truth and let cow 4 be a liar.