/*
题意:求出多个全排列的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. Make Notepad++ auto close HTML/XML tags after the slash(the Dreamweaver way)

    I've been looking for a Notepad++ plugin that can close HTML/XML tags after a slash just like the wa ...

  2. install openvpn and openvpn manager in ubuntu

    sudo apt-get install openvpn sudo apt-get install network-manager-openvpn

  3. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  4. [Linux-shell] AWK

    Go to the first, previous, next, last section, table of contents. Printing Output One of the most co ...

  5. java 内存模型

    翻译自wiki百科:https://en.wikipedia.org/wiki/Java_memory_model 没找到直接在wiki上编辑中文的页面,我就在这翻译下,自己学习用. java内存模型 ...

  6. Android Scroll分析——滑动效果产生

    相对于在Android2.x版本上出现的长按.点击事件的效果,不得不说,滑动操作具有更好的用户体验.因此,从Android 4.X版本开始,出现了更多滑动操作的效果.越来越多第三方应用模仿这样的效果, ...

  7. Mac OS X 10.9.3 UI 设置界面无法设置时区解决

    10.9.3 在选项设置里无法设置时区,表现为选择时区的点的位置后无法保存,导致系统时间错乱,解决方法是用终端设置: sudo systemsetup -gettimezone sudo system ...

  8. Web Essentials之JavaScript,TypeScript和CoffeeScript

    返回Web Essentials功能目录 一些Javascript功能也可以用于TypeScript. 本篇目录 功能 智能提示 TypeScript CoffeeScript 功能 JSHint J ...

  9. [.net 面向对象编程基础] (21) 委托

    [.net 面向对象编程基础] (20)  委托 上节在讲到LINQ的匿名方法中说到了委托,不过比较简单,没了解清楚没关系,这节中会详细说明委托. 1. 什么是委托? 学习委托,我想说,学会了就感觉简 ...

  10. MooseFS学习-概述

    MFS(MooseFS)是一个容错的.网络分布式文件系统,是GFS的开源实现.它把数据分散在多个物理机上,对外展现为一个整体资源. 支持的功能 Unix的通用文件系统功能:目录树:记录POSIX文件属 ...