Codeforces 463D Gargari and Permutations(BFS)
来源:互联网 发布:linux 显示行号的命令 编辑:程序博客网 时间:2024/06/05 21:16
题目链接:Codeforces 463D Gargari and Permutations
题目大意:求k个序列的最长公共子序列。
解题思路:以每个数为节点,假如a和b,如果a在每个序列中的位置都在b前面的话,可以在a和b建立一条有向边,然后用BFS处理下。
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int maxn = 1005;int N, K, x;int pos[5][maxn];vector<int> g[maxn];bool check (int a, int b) { for (int i = 0; i < K; i++) if (pos[i][a] >= pos[i][b]) return false; return true;}int bfs (int s) { int d[maxn]; memset(d, 0, sizeof(d)); queue<int> que; que.push(s); int ans = 0; while (!que.empty() ) { int u = que.front(); ans = max(ans, d[u]); que.pop(); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (d[u] + 1 > d[v]) { d[v] = d[u] + 1; que.push(v); } } } return ans;}int main () { scanf("%d%d", &N, &K); for (int i = 0; i < K; i++) { for (int j = 1; j <= N; j++) { scanf("%d", &x); pos[i][x] = j; } } for (int i = 1; i <= N; i++) { g[0].push_back(i); for (int j = 1; j <= N; j++) if (check(i, j)) g[i].push_back(j); } printf("%d\n", bfs(0)); return 0;}
题目链接:Codeforces 463D Gargari and Permutations
题目大意:求k个序列的最长公共子序列。
解题思路:以每个数为节点,假如a和b,如果a在每个序列中的位置都在b前面的话,可以在a和b建立一条有向边,然后用BFS处理下。
阅读全文
0 0
- Codeforces 463D Gargari and Permutations(BFS)
- Codeforces 463D Gargari and Permutations(BFS)
- Codeforces 463D. Gargari and Permutations【DP】
- codeforces 463D Gargari and Permutations
- 【CodeForces】463D Gargari and Permutations DP
- Codeforces 463D Gargari and Permutations
- 【CODEFORCES】 D. Gargari and Permutations
- codeforces 463D D. Gargari and Permutations(dp)
- Codeforces 463D Gargari and Permutations DP(LCS变形)
- CF 463D Gargari and Permutations (dp)
- CF 463D Gargari and Permutations [dp]
- Codeforces #264 (Div. 2) D. Gargari and Permutations
- Codeforces #264 (Div. 2) D. Gargari and Permutations
- Codeforces #264 div.2 D. Gargari and Permutations
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces 264 (Div. 2) D. Gargari and Permutations
- Codeforces 463D. Gargari and Permutations (dp,distinct下的LCS问题)
- Codeforces 463D Gargari and Permutations【思维建图+Floyd跑最长路】不愧是CF的评测姬
- 广义表
- Ajax原生实现及相关知识讲解
- 趣味数学--用1到9这九个数组成一个四位数乘以一位数等于四位数的等式,每个数只能用一次
- OpenGL无法打开文件“freeglut.lib”错误的解决办法
- heartbeat+nginx
- Codeforces 463D Gargari and Permutations(BFS)
- CodeForces
- java中静态块、main方法、构造块、构造方法的执行顺序复习
- github配置pc与远程ssh
- JUnit4 源码阅读(一)
- 学习日记17
- 饿了么项目---5、vue-resource的使用(版本:1.3.4)
- 死锁的条件
- mybatis多个参数类型不同,sql语句中paramterType怎么写?