#include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> PII; typedef pair <ll, ll> PLL; typedef pair <PLL, PLL> PP; typedef unsigned int ui; const int mod = 1e9+7; const int inf = 1e9+9; const ll MOD = 1e9+696969; const ll INF = 1e18; int n, m, k, a, b, c, p, q, T, steps; vector <int> wzorzec, tekst; vector <int> v[1010]; int PI[10000100]; vector <int> s; void KMP() { s.resize(wzorzec.size() + tekst.size() + 2); for (int i=1; i<=tekst.size(); ++i) s[m + i + 1] = tekst[i-1]; PI[0] = PI[1] = 0; b = 0; int all = tekst.size() + m + 1; for (int i=2; i<=all; ++i) { //printf("%d ", s[i]); while (s[i] != s[b + 1] && b > 0) b = PI[b]; if (s[b + 1] == s[i]) ++b; PI[i] = b; if (PI[i] == m) OUT(steps); } } int main() { scanf("%d%d", &n, &m); for (int i=1; i<=n; ++i) { scanf("%d", &a); for (int j=0; j<a; ++j) { scanf("%d", &b); v[i].pb(b); } } tekst.clear(); tekst.pb(1); for (int i=1; i<=m; ++i) { scanf("%d", &a); wzorzec.pb(a); } if (a == 1 && m == 1) OUT(1); s.resize(10000); for (int i=1; i<=m; ++i) s[i] = wzorzec[i-1]; s[m + 1] = -1; steps=1; while (tekst.size() < 100000) { ++steps; vector <int> nowy; for (int i=0; i<tekst.size(); ++i) { int el = tekst[i]; for (int j=0; j<v[el].size(); ++j) nowy.pb(v[el][j]); } tekst.clear(); tekst = nowy; KMP(); } puts("-1"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> PII; typedef pair <ll, ll> PLL; typedef pair <PLL, PLL> PP; typedef unsigned int ui; const int mod = 1e9+7; const int inf = 1e9+9; const ll MOD = 1e9+696969; const ll INF = 1e18; int n, m, k, a, b, c, p, q, T, steps; vector <int> wzorzec, tekst; vector <int> v[1010]; int PI[10000100]; vector <int> s; void KMP() { s.resize(wzorzec.size() + tekst.size() + 2); for (int i=1; i<=tekst.size(); ++i) s[m + i + 1] = tekst[i-1]; PI[0] = PI[1] = 0; b = 0; int all = tekst.size() + m + 1; for (int i=2; i<=all; ++i) { //printf("%d ", s[i]); while (s[i] != s[b + 1] && b > 0) b = PI[b]; if (s[b + 1] == s[i]) ++b; PI[i] = b; if (PI[i] == m) OUT(steps); } } int main() { scanf("%d%d", &n, &m); for (int i=1; i<=n; ++i) { scanf("%d", &a); for (int j=0; j<a; ++j) { scanf("%d", &b); v[i].pb(b); } } tekst.clear(); tekst.pb(1); for (int i=1; i<=m; ++i) { scanf("%d", &a); wzorzec.pb(a); } if (a == 1 && m == 1) OUT(1); s.resize(10000); for (int i=1; i<=m; ++i) s[i] = wzorzec[i-1]; s[m + 1] = -1; steps=1; while (tekst.size() < 100000) { ++steps; vector <int> nowy; for (int i=0; i<tekst.size(); ++i) { int el = tekst[i]; for (int j=0; j<v[el].size(); ++j) nowy.pb(v[el][j]); } tekst.clear(); tekst = nowy; KMP(); } puts("-1"); } |