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 }

转载于:https://www.cnblogs.com/hujunzheng/p/3947392.html

codeforces Gargari and Permutations(DAG+BFS)相关推荐

  1. codeforces Gargari and Permutations(DAG+BFS)

    1 /* 2 题意:求出多个全排列的lcs! 3 思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边! 4 最后用b ...

  2. 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 ...

  3. Gargari and Permutations CodeForces - 463D(建图+记忆化搜索)

    Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...

  4. codeforces 463D Gargari and Permutations

    题意: 从所给的序列中找出它们的最长公共子序列. 思路: DAG(有向无环图)+BFS. 如果数值 i 在所有序列中都在 j 前面.则i -> j连一条有向边.(好像还有个dp的思路的) 参考: ...

  5. Codeforces 540C Ice Cave (BFS)

    题目:http://codeforces.com/problemset/problem/540/C 代码: #include<stdio.h> #include<string.h&g ...

  6. Codeforces D. Fair 多源BFS求最短路

    点击打开链接 D. Fair time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  7. CodeForces - 1353D Constructing the Array(bfs)

    题目链接:点击查看 题目大意:给出一个长度为 n ,初始时全部为 0 的数组 a ,后续进行 n 次操作,每次操作找到最长的连续 0 ,如果有多个则选择位置最靠左边的,将这组连续 0 的,最中间位置的 ...

  8. 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 ...

  9. Codeforces 213E Two Permutations 线段树 (看题解)

    Two Permutations 关键是没想到按大小顺序把第二个排列一个一个加入线段树, 然后线段树维护整体的hash值, 得到的hs值减去一个sub 之后与, 第一个排列的hash值比较. #inc ...

最新文章

  1. 解决:【异常】Caused by: java.lang.IllegalStateException: Zip64 archives are not supported
  2. LR中的吞吐量与响应时间
  3. Re:从 0 开始的微服务架构--(三)微服务架构 API 的开发与治理--转
  4. cmake (2)路径指令
  5. 《机器学习实战》总结
  6. 搞硬件,钱少破事多,只能另谋出路!
  7. 搞定mac的bashrc
  8. TensorFlow 学习(十五)—— tensorflow.python.platform
  9. python数据框转化为矩阵_在Python中将系列重塑为Dataframe矩阵
  10. 使用bcryptjs对密码加密时,其校验原理是怎样的?
  11. 圣思园Spring精髓-Spring的底层设计思想
  12. linux 快速查找文件
  13. 阿里云ECS运维:恶意程序告警及处理过程
  14. 职业规划-三大职业生涯阶段
  15. 对于计算机学科的认识和一些感想
  16. AIT Worldwide Logistics与卡利塔航空建立民航预备队合作伙伴关系
  17. 读《华为区块链白皮书》
  18. python实部和虚部都是浮点数_python实部和虚部都是浮点数_python——Numpy库
  19. 4.3 Cholesky分解
  20. 技术至简-5:动画显示复指数信号的形状与物理意义

热门文章

  1. 鸿蒙手表esim,鸿蒙手表终于来了!或将支持 eSIM,实现独立通话
  2. spring 使用其他类protected方法_Java操作bean、属性、方法的使用工具类
  3. idea 开启Run DashBoard
  4. 工作流实战_25_flowable 流程中的自动跳过
  5. Mysql代码生成器转Oracle代码生成
  6. 实战01_SSM整合ActiveMQ支持多种类型消息
  7. 企业实战_11_MyCat垂直拆分相关配置
  8. window下远程监控linux下tomcat的jvm
  9. 软件设计师 - 数据流图
  10. 前端学习笔记之 创建节点 与 添加节点案例