MicroRNA Ranking
Time limit2sMemory limit512 MB
Given k permutations of n items, find a permutation where every earlier item beats the later one in at least half the rankings, choosing the lexicographically smallest such permutation.
- Level
Medium7 of 10
- Topics
- Graph, Topological sort, Sorting, Greedy
- Solved
- No attempts yet
Problem
Ahlaam studies computer science and is writing her master's thesis on a bioinformatics project about microRNAs, a class of molecules found in cells. She wants to find the microRNAs that are relevant to one health factor in humans.
Ahlaam has designed microRNA ranking algorithms. Each algorithm ranks the microRNAs from its own point of view. There are microRNAs numbered through , and each algorithm prints one permutation of them. In the permutation printed by an algorithm, the first microRNA is the one that algorithm considers most relevant to the health factor, and the last one is the least relevant.
Ahlaam wants to report a consensus ranking. In a consensus ranking, if microRNA is placed before microRNA , then at least half of the algorithms must rank before . Write a program that helps Ahlaam find a consensus ranking.
Input
The input holds several test cases. The first line of each test case has two integers and separated by a space, the number of microRNAs and the number of ranking algorithms (, ).
Each of the next lines holds a permutation of the numbers through . The -th of those lines is the output of the -th ranking algorithm.
The last line of the input is 0 0. Do not process that line.
Output
For each test case, print one line holding a permutation of the numbers through that is a consensus ranking. Separate the numbers with single spaces.
If more than one consensus ranking exists, print the lexicographically smallest one. A sequence is lexicographically smaller than a sequence if there is a positive integer such that for all and .
If no consensus ranking exists, print No solution instead.