1/* 2 Question: find multiple fully-arranged LCS! 3. Idea: because it is a full arrangement, every number in each row will not be repeated. Therefore, if each fully-arranged number I is in front of the number J, then I, j. Create a directed edge! 4. Use BFs to traverse the entire graph and find the distance from the source point to each vertex. the maximum distance is the length of the longest common subsequence! 5 */6 # include <iostream> 7 # include <cstdio> 8 # include <cstring> 9 # include <algorithm> 10 # include <queue> 11 # include <vector> 12 # define n 100513 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) {// ensure that number A is 24 for (INT I = 1; I <= K; ++ I) Before number B) 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", & 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; // The minimum distance from all initialization points to the source point (because the maximum distance is obtained, not the shortest distance) 63 vis [I] = 0; 64g [0]. push_back (I); 65 for (Int J = I + 1; j <= N; ++ J) 66 If (Judge (I, j) 67g [I]. push_back (j); 68 else if (Judge (J, I) 69g [J]. push_back (I); 70} 71 BFS (0); 72 printf ("% d \ n", ANS); 73 for (INT I = 0; I <= N; + + I) // do not forget to clear the vector 74g [I]. clear (); 75} 76 return 0; 77}
Codeforces gargari and permutations (DAG + BFS)