Codeforces Round #264 (Div. 2)D(图论+dp)

D. Gargari and Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?

You can read about longest common subsequence there:https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

Input

The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation.

Output

Print the length of the longest common subsequence.

Sample test(s)
input
4 3
1 4 2 3
4 1 2 3
1 2 4 3
output
3
Note

The answer for the first test sample is subsequence [1, 2, 3].


题意:找出k个排列的最长公共子序列

思路:因为找出的子序列里面的数都是在k个序列中出现一次并且满足先后关系,所以建图的时候只要 i 在 j 的前面这个关系在给出的k个排列中都有,那么就在 i 到 j 连一条边,          
            然后问题就转化为在图上找最长路径即可,可以用dfs记忆化搜索

你可能感兴趣的:(ACM,codeforces)