University Rankings
Time limit8sMemory limit512 MB
Given M rankings of N universities, find the longest sequence where each earlier university beats the next in every ranking.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
A rating agency publishes one ranking list per department. Every university in the survey runs the same departments, and each department list places all universities in a strict order from best to worst.
Because there are many lists, two universities often cannot be ordered against each other. The agency therefore uses a relation it calls absolutely better: university is absolutely better than university when comes before in every one of the department rankings.
Take three universities , , and three departments CS, EE, FLS with these rankings:
- CS: , ,
- EE: , ,
- FLS: , ,
comes before in all three lists, so is absolutely better than . and cannot be ordered: leads in CS, leads in FLS.
Find universities such that is absolutely better than for every . Report the largest possible . Universities are given as numbers.
Input
The first line holds one integer , the number of test cases ().
Each test case begins with a line holding two integers and (), the number of universities and the number of departments. lines follow. The -th of them holds integers, a permutation of through , listing the universities of the -th department's ranking from best to worst. If university appears before university on that line, the -th department of is better than the -th department of .
Output
Print lines, one per test case in the order given, each holding the largest possible . Print no extra spaces.