codeforces Gargari and Permutations(DAG+BFS)
1 /* 2 题意:求出多个全排列的lcs! 3 思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边! 4 最后用bfs遍历整个图,求出源点到每一个点的距离,其中最大的距离就是最长的公共子序列的长度! 5 */ 6 #include<iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<algorithm> 10 #include<queue> 11 #include<vector> 12 #define N 1005 13 14 using namespace std; 15 vector<int>g[N]; 16 queue<int>q; 17 int pos[6][N]; 18 int d[N]; 19 int vis[N]; 20 int n, k; 21 int ans; 22 23 bool judge(int a, int b){//保证数字a 在数字 b的前边 24 for(int i=1; i<=k; ++i) 25 if(pos[i][a] > pos[i][b]) 26 return false; 27 return true; 28 } 29 30 void bfs(int x){ 31 q.push(x); 32 ans=0; 33 vis[0]=1; 34 while(!q.empty()){ 35 int u=q.front(); 36 q.pop(); 37 vis[u]=0; 38 ans=max(ans, d[u]); 39 int len=g[u].size(); 40 for(int i=0; i<len; ++i){ 41 int v=g[u][i]; 42 if(d[v]<d[u]+1){ 43 d[v]=d[u]+1; 44 if(!vis[v]){ 45 q.push(v); 46 vis[v]=1; 47 } 48 } 49 } 50 } 51 } 52 53 int main(){ 54 while(scanf("%d%d", &n, &k)!=EOF){ 55 for(int i=1; i<=k; ++i) 56 for(int j=1; j<=n; ++j){ 57 int x; 58 scanf("%d", &x); 59 pos[i][x]=j; 60 } 61 for(int i=1; i<=n; ++i){ 62 d[i]=0;//初始化所有点到源点的距离都为最小(因为是求最大距离,不是最短距离) 63 vis[i]=0; 64 g[0].push_back(i); 65 for(int j=i+1; j<=n; ++j) 66 if(judge(i, j)) 67 g[i].push_back(j); 68 else if(judge(j, i)) 69 g[j].push_back(i); 70 } 71 bfs(0); 72 printf("%d\n", ans); 73 for(int i=0; i<=n; ++i)//不要忘记将vector清空 74 g[i].clear(); 75 } 76 return 0; 77 }
codeforces Gargari and Permutations(DAG+BFS)相关推荐
- codeforces Gargari and Permutations(DAG+BFS)
1 /* 2 题意:求出多个全排列的lcs! 3 思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边! 4 最后用b ...
- 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 ...
- Gargari and Permutations CodeForces - 463D(建图+记忆化搜索)
Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...
- codeforces 463D Gargari and Permutations
题意: 从所给的序列中找出它们的最长公共子序列. 思路: DAG(有向无环图)+BFS. 如果数值 i 在所有序列中都在 j 前面.则i -> j连一条有向边.(好像还有个dp的思路的) 参考: ...
- Codeforces 540C Ice Cave (BFS)
题目:http://codeforces.com/problemset/problem/540/C 代码: #include<stdio.h> #include<string.h&g ...
- Codeforces D. Fair 多源BFS求最短路
点击打开链接 D. Fair time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- CodeForces - 1353D Constructing the Array(bfs)
题目链接:点击查看 题目大意:给出一个长度为 n ,初始时全部为 0 的数组 a ,后续进行 n 次操作,每次操作找到最长的连续 0 ,如果有多个则选择位置最靠左边的,将这组连续 0 的,最中间位置的 ...
- Codeforces 1070A Find a Number(BFS) 2018-2019 ICPC, NEERC, Southern Subregional Contest Problem A
Description You are given two positive integers ddd and sss. Find minimal positive integer nnn which ...
- Codeforces 213E Two Permutations 线段树 (看题解)
Two Permutations 关键是没想到按大小顺序把第二个排列一个一个加入线段树, 然后线段树维护整体的hash值, 得到的hs值减去一个sub 之后与, 第一个排列的hash值比较. #inc ...
最新文章
- 汽车开放系统架构(AUTOSAR)是什么
- 关于如如何运行tensorrt
- 循环造数据_项目管理数据与信息--DIKW模型
- 搜索时,怎样排除不需要的关键字
- 有了报表FineReport,为什么还要上FineBI?
- 中断挂起是什么意思_深入JVM(三)- 什么是垃圾及垃圾回收算法
- json转为tfrecord格式文件怎么转_word怎么转换成pdf格式?这样转很方便
- Angular NgModule 作用域相关问题
- java集合类程序代码_Java集合类源代码分析二:ArrayList(1)
- 概述无线传感网的MAC协议
- 5G到底厉害在什么地方?和4G有什么不同?
- CSDN数据库泄露!那些网站够安全吗?
- 腾讯位置服务仿微信发送位置功能
- 爬取国内创业公司、投资机构、独角兽公司信息
- UDP通讯之字节流与protobuf转换(C++版)
- [转] meta标签的使用方法
- netapp 开启内网穿透
- html表格td向下排序,jquery实现的table排序功能示例
- HDMI over ip 延长芯片方案
- 【异常解决】jxl.write.biff.JxlWriteException: Attempt to modify a referenced format