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记忆化搜索

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;

int head[1010];
int edge[1010*1010];
int a[6][1010];
int next[1010*1010];
int ww[1010][1010];
int d;
int dp[1010];

void add(int u,int v)
{
    edge[d]=v;
    next[d]=head[u];
    head[u]=d++;
}

void dfs(int u)
{
    int i;
    if(dp[u])return;
    dp[u]=0;
    int maxi=0;
    for(i=head[u];i!=-1;i=next[i]){
        int v=edge[i];
        dfs(v);
        if(maxi
       
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值