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 }
时间: 2025-01-05 03:36:48

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 trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1,?2,?...,?n in some order. Now he should find t

Codeforces 463D Gargari and Permutations(求k个序列的LCS)

题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n<=1000),每个序列中的数字分别为1~n,求着k个序列的最长公共子序列是多长?解题思路:由于每个序列的数字分别为1~n即各不相同,所以可以用pos[i][j]记录第i个序列中j的位置.设dp[i]表示以i结尾的最长公共子序列长度,那么我们可以按顺序遍历第一个序列的位置i,再在第一个序列中枚举位置j(j<

codeforces 463D Gargari and Permutations(dp)

题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],dp[j]+1); j<i //先预处理出两个数在所有序列中的位置关系, //例如两个数a和b,只要在任意一个序列中a在b的后面,则记after[a][b]=1. //在递推的时候如果!after[a][b],则进行状态转移. #include <cstdio> #include <cs

Codeforces 463D. Gargari and Permutations【DP】

题目大意: 给出1~n的k个排列(2<=k<=5),要求其中的最长公共子序列. 做法: 算是不难的DP,dp[i]表示以i为结尾的最长公共子序列的长度,由于每个数在一个排列中只可能出现一次,我们用一个二维数组pos[i][j]表示数字j在第i行出现在第几个位置,再用一个数组cnt[i] 记录i出现了多少次:当第i个数出现了k次之后,说明能够以该数为结尾构成公共子序列,那么dp[i]=max(dp[j]+1),其中i,j满足pos[u][i]>pos[u][j](1<=u<=k

Codeforces 467D Fedor and Essay(bfs)

题目链接:Codeforces 467D Fedor and Essay 题目大意:给定一个含n个单词的文本,然后给定m种变换,要求变换后r的个数尽量少,长度尽量短,不区分大小写. 解题思路:bfs,将每个单词处理成长度以及r的个数,然后从最优的开始更新即可,类似dp. #include <cstdio> #include <cstring> #include <map> #include <string> #include <vector> #

CodeForces 666B. World Tour【BFS】

题目链接 http://codeforces.com/problemset/problem/666/B 思路 给你一张有向图,叫你给出四个点的序列,使得这四个点依次间的最短路之和最大. n有3000,直接枚举四个点肯定超时,所以枚举b.c两个点,然后BFS预处理出能到b的最远的3个点,和c能到的最远的3个点. 之所以是3个点是因为,有可能备选点会和已定点重合,例如abc都定好了,然后d的备选是a.b,那就漏情况了,所以要备选3个点. AC代码 #include <iostream> #incl

CodeForces 540C Ice Cave (BFS)

http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 540C Description You play a computer game. Your character stands on some level of

Codeforces gym 100685 F. Flood bfs

F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Description We all know that King Triton doesn't like us and therefore shipwrecks, hurricanes and tsunami do happen. But being bored with the same routine

codeforces CF37E Trial for Chief BFS最短路

\(\rightarrow\) 戳我进CF原题 E. Trial for Chief time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input output: standard output Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chro