Inspector
Time limit5sMemory limit128 MB
Given statements of the form 'at time t, programmer j was present along with i others', find the largest prefix of statements that can all hold at once.
- Level
Hard8 of 10
- Topics
- Intervals, Brute force, Greedy, Implementation
- Solved
- No attempts yet
Problem
Inspector Byteasar is investigating a crime that happened at a software company, and he is trying to reconstruct the chain of events. The programmers, unfortunately, are quite absent-minded. The most useful things he can get from them are statements such as: "When I glanced at the clock at 14:42, there were five other programmers besides me logged in on the server."
Each programmer came to the office once during that day, stayed for one continuous stretch of time without ever stepping out, and then left for good, never returning the same day.
Baffled by the statements, Byteasar is not even sure they can all be trusted. Before anything else he wants to know whether it is at all possible that every statement is true at the same time. Help him find out.
Input
The first line contains an integer (), the number of test cases. The test cases follow one after another.
The first line of each test case contains two integers and (): the number of programmers working in the office and the number of statements recorded by Byteasar. The programmers are numbered from to .
Each of the next lines describes one statement with three integers , and (, , ): programmer claims that at moment he was in the office and that, apart from himself, exactly other programmers were there. Every programmer's arrival and departure happen at moments different from all the moments mentioned in the statements (that is, strictly before, after, or between them).
Output
For each test case print a single line with one positive integer (): the largest number of leading statements that can all be true at once. In other words, the first statements can hold simultaneously, but the first cannot. If all statements can be true together, print .
Notes
In the first example, the first four statements can hold at the same time, but adding the fifth one makes them contradictory: programmers and would then both have to be present from moment through moment , so at moment there would be at least three programmers in the office (numbers , and ). That contradicts programmer , who says only one other programmer was with him at moment . Hence the answer is .
In the second example all three statements are mutually consistent, so the answer is .