/*
题意:求出多个全排列的lcs!
思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边!
最后用bfs遍历整个图,求出源点到每一个点的距离,其中最大的距离就是最长的公共子序列的长度!
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define N 1005 using namespace std;
vector<int>g[N];
queue<int>q;
int pos[][N];
int d[N];
int vis[N];
int n, k;
int ans; bool judge(int a, int b){//保证数字a 在数字 b的前边
for(int i=; i<=k; ++i)
if(pos[i][a] > pos[i][b])
return false;
return true;
} void bfs(int x){
q.push(x);
ans=;
vis[]=;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
ans=max(ans, d[u]);
int len=g[u].size();
for(int i=; i<len; ++i){
int v=g[u][i];
if(d[v]<d[u]+){
d[v]=d[u]+;
if(!vis[v]){
q.push(v);
vis[v]=;
}
}
}
}
} int main(){
while(scanf("%d%d", &n, &k)!=EOF){
for(int i=; i<=k; ++i)
for(int j=; j<=n; ++j){
int x;
scanf("%d", &x);
pos[i][x]=j;
}
for(int i=; i<=n; ++i){
d[i]=;//初始化所有点到源点的距离都为最小(因为是求最大距离,不是最短距离)
vis[i]=;
g[].push_back(i);
for(int j=i+; j<=n; ++j)
if(judge(i, j))
g[i].push_back(j);
else if(judge(j, i))
g[j].push_back(i);
}
bfs();
printf("%d\n", ans);
for(int i=; i<=n; ++i)//不要忘记将vector清空
g[i].clear();
}
return ;
}

codeforces Gargari and Permutations(DAG+BFS)的更多相关文章

  1. Codeforces #264 (Div. 2) D. Gargari and Permutations

    Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...

  2. Codeforces 463D Gargari and Permutations

    http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...

  3. Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题

    http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...

  4. Codeforces 463D Gargari and Permutations(求k个序列的LCS)

    题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n< ...

  5. Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】

    题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...

  6. codeforces 463D Gargari and Permutations(dp)

    题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...

  7. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  8. CodeForces 540C Ice Cave (BFS)

    http://codeforces.com/problemset/problem/540/C       Ice Cave Time Limit:2000MS     Memory Limit:262 ...

  9. codeforces 1283D. Christmas Trees(bfs)

    链接: https://codeforces.com/contest/1283/problem/D 题意:给定n个不同的整数点,让你找m个不同的整数点,使得这m个点到到这n个点最小距离之和最小. 思路 ...

随机推荐

  1. 学习django之正则表达式的语法

    正则表达式 正则表达式的常用语法: 1)单个字符: .          任意的一个字符 a|b        字符a或字符b [afg]      a或者f或者g的一个字符 [0-4]      0 ...

  2. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  3. 对于git的认识

    对于git的认识,我只想说,我不会把他的概念复制下来在博客上再发一遍,我对于他的了解是代源码的开放编写.对于git我会在今后去认真的理解他,不是所谓的抄袭.不会的东西我会尽力去学习.

  4. Centos普通用户提权至ROOT

    1.利用/bin/ping的漏洞普通用户提权.(rws中的s) [root@localhost ~]# ls -l /bin/ping -rwsr-xr-x. root root 9月 /bin/pi ...

  5. HTML5-样式

    外部样式,内部样式,内链样式 <!DOCTYPE html> <html> <head lang="en"> <meta charset= ...

  6. fulltext不支持Mysql中文全文索引

    Mysql对某表某字段建立了fulltext索引,也不支持中文. 当数据量很大的时候,比较成熟的做法是使用专门的全文索引系统,用这些专业的全文索引系统来分词,以mysql数据库中的数据作为数据源,来分 ...

  7. mysql客户端导入sql文件命令

    mysql -h localhost -u root -p dbname < filename

  8. innodb 锁分裂继承与迁移

    innodb行锁简介 行锁类型 LOCK_S:共享锁 LOCK_X: 排他锁 GAP类型 LOCK_GAP:只锁间隙 LOCK_REC_NO_GAP:只锁记录 LOCK_ORDINARY: 锁记录和记 ...

  9. Android 自定义View 总结

    Android系统本身给我们提供十分丰硕的组件让我们实现包罗万象的UI效果,与此同时,我们也能够非常方便实现各种方法来实现各种强大的功能.通过继承现有的UI控件,我们也能够拓展现有的功能.我们也能够完 ...

  10. 【AspNetCore】【WebApi】扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat)

    扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat) 一.背景 大家在使用WebApi时,会用到DateTime为参数,类似于这 ...