/*
题意:求出多个全排列的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. springmvc No mapping found for HTTP request with URI in Dispatc

    springmvc No mapping found for HTTP request with URI in Dispatc 博客分类: Java Web springmvcspring MVCNo ...

  2. 视频文件写入转换之图像处理-OpenCV应用学习笔记五

    在<笔记二>中我们做了视频播放和控制的实现,仅仅算是完成了对视频文件的读取操作:今天我们来一起练习下对视频文件的写入操作:格式转换. 实现功能: 打开一个视频文件play.avi,读取文件 ...

  3. python中re.findall()找到的结果替换

    正则表达式re模块中用findall查找到的是ascii码,所以当比对替换时也需要对应的ascii码才能匹配成功.以下程序是查找文件夹下文件名中含有男.女的文件,并将男替换成1,将女替换成2的程序 # ...

  4. Sphinx : 高性能SQL全文检索引擎

    Sphinx的特点 快速创建索引:3分钟左右即可创建近100万条记录的索引,并且采用了增量索引的方式,重建索引非常迅速. 闪电般的检索速度:尽管是1千万条的大数据量,查询数据的速度也在毫秒级以上,2- ...

  5. CoreCLR 在 Linux 下编译成功

    https://github.com/dotnet/coreclr/wiki/Building-and-Running-CoreCLR-on-Linux ubuntu-14.10 clang --ve ...

  6. PCWIFI--无线网络共享软件

    前段时间由于需要共享笔记本无线网络给手机使用,在网上找了几个软件试了一下,没找到比较好用的,要么是收费的,要么有广告,要么附带一大堆其他功能,所以决定自己写一个小软件来实现该功能.软件相关介绍如下:  ...

  7. Windows Server 2008更改远程桌面端口号

    windows 2008远程桌面端口默认是用的是3389端口,但是由于安全考虑,经常我们安装好系统后一般都会考虑把原来的3389端口更改为另外的端口. 更改过程: 打开注册表: 运行regedit 找 ...

  8. Android(蓝牙)

    因近期项目需求调试了Android蓝牙通讯接口,主要是两个终端作为服务端和客户端的通信,本文将部分重要知识点记录如下. 蓝牙是短距离无线通信,通常分经典蓝牙和低功耗蓝牙(即蓝牙4.0),两类蓝牙协议各 ...

  9. 【转】 Nginx深入详解之多进程网络模型

    [转自]http://blog.chinaunix.net/uid-22312037-id-3974068.html 一.进程模型        Nginx之所以为广大码农喜爱,除了其高性能外,还有其 ...

  10. C#Light for Unity 新例子

    近来有好几位询问C#Lite在Unity中使用的问题 我专门为C#Lite制作了 for Unity的新例子 ,这个例子名为languagetest 包含17个语言特性测试的文件 其中_6004_展示 ...