codeforces Gargari and Permutations(DAG+BFS)
/*
题意:求出多个全排列的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)的更多相关文章
- 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 ...
- Codeforces 463D Gargari and Permutations
http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...
- Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...
- Codeforces 463D Gargari and Permutations(求k个序列的LCS)
题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n< ...
- Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】
题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...
- codeforces 463D Gargari and Permutations(dp)
题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
- codeforces 1283D. Christmas Trees(bfs)
链接: https://codeforces.com/contest/1283/problem/D 题意:给定n个不同的整数点,让你找m个不同的整数点,使得这m个点到到这n个点最小距离之和最小. 思路 ...
随机推荐
- PYTHON学习之路_PYTHON基础(10)
学习内容: Python进程与线程 1.线程及线程类 2.线程守护 3.线程等待 4.线程锁 5.信号量 6.timer用法 7.队列 8.事件驱动 9.生产者消费者模型 10.进程及进程同步 11. ...
- 64位WIN7下安装MPICH2
1.首先,下载32位的MPICH2,(注意哦,是32位,不是64位) http://202.117.4.228/files/B117000000042632/www.mcs.anl.gov/resea ...
- android IOC框架学习记录
一.框架如下几种: 1.Roboguice 2.Spring for Android 3.afinal 4.xUtils 二.Roboguice说明 项目地址:https://gith ...
- android 开发禁止系统修改app的字体大小
重写activity的getResources方法,一般在BaseActivity中重写就好了,其他activity继承BaseActivity //设置字体大小不随手机设置而改变 @Override ...
- MVC模式下如何实现RegisterStartupScript等功能
本文源于http://www.achtmaal.com/blog/asp-net-mvc-and-registerclientscriptinclude,非常感谢原文作者的智慧和分享 Register ...
- iOS直播直播,头都大了
随着直播市场的火热,市场大军都逐步进入直播市场 ,腾讯旗下的NOW直播也不例外 先说说直播设计底层 一 .流媒体 1 - 伪流媒体 1.1 扫盲:边下载边播放 1.2 伪流媒体:视频不是实时播放的,先 ...
- jq实现 按钮点击一次后 3秒后在可点击
if(printRemind(selectPrintTemplate,selectOrders,orderStatus,isPreview)) //调用打印数据并打印 ajaxDataAndDoPri ...
- C++混合编程之idlcpp教程Lua篇(7)
上一篇在这 C++混合编程之idlcpp教程Lua篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与LuaTutorial4工程相似,工程LuaTutorial5中,同样加入了四个文件: ...
- Docker Machine v1.11.2安装与使用
官方文档:Docker Machine 官方文档:Docker Toolbox boot2docker安装包官网下载链接:Docker Toolbox-1.11.1b.exe 此安装包包含的“boot ...
- 阿里巴巴-OS事业群-OS手机事业部-系统服务部门招聘Java开发工程师,有意者请进来
我是阿里巴巴-OS事业群-OS手机事业部-系统服务部的开发工程师,正在招聘Java开发工程师. 以下是职位描述: 岗位名称:Java开发工程师 招聘人数:5人 生效日期:2014-03-12 结束日期 ...