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个点最小距离之和最小. 思路 ...
随机推荐
- 类型“System.Windows.Markup.IUriContext”在未被引用的程序集中定义 解决办法
错误 CS0012: 类型“System.Windows.Markup.IUriContext”在未被引用的程序集中定义.必须添加对程序集“System.Xaml, Version=4.0.0.0, ...
- oracle高阶知识点
------------------------------------------------- varchar2(4000)字符型,最大长度不能超过4000,与char的区别是不用空格补足 num ...
- [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 给VM中的RHEL6.5配置本地源
二步:1.启动时自动挂载安装盘:2.增加一个".repo"(或者改掉原来的源的配置p.s.除非你以后都不想用网络源或者已经知道如何更改源的配置,否则别改) 首先,在/media中创 ...
- java的值传递和引用传递
昨天博主在对于值传递和引用传递这里栽了一个大坑啊,导致一下午时间都浪费在这里,我们先说下值传递和引用传递java官方解释: 值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对 ...
- 利用HtmlAgilityPack库进行HTML数据抓取
主要介绍基于XPATH的文本分析方式的实现,代码如下: using System; using System.Collections.Generic; using System.Linq; using ...
- 我的ORM之八-- 事件
我的ORM索引 dbo.Event 需要新建一个类,继承:IDboEvent,并设置给:dbo.Event,如: dbo.Event = MyOqlEvent.GetInstance(); 在 IDb ...
- 一用钟情的VS插件系列总目录(值得收藏)
关于插件,大家的印象可能很多,比如开发者经常使用的Chrome浏览器的扩展程序,某个软件的一个扩展程序等等.我们使用插件的目的是为了提高我们的某些方面的工作效率或者让我们的软件源(Chrome浏览器等 ...
- Technical news July-11
http://blog.jobbole.com/73300/ 对比Ruby和Python的垃圾回收(2):代式垃圾回收机制 http://www.microsoftvirtualacademy.com ...
- ASP.NET集成模式下的管道事件