#include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define VI vector<int> #define PII pair<int,int> #define st first #define nd second #define mp make_pair #define pb push_back #define lint long long int #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} using namespace std; int n,m; VI S; VI rep[505]; int start[505]; int start_pary[505][505]; void wypelnij() { FOR(i,1,n+1) start[i] = -1; FOR(i,1,n+1) FOR(j,1,n+1) start_pary[i][j] = -1; VI kolejka; kolejka.pb(1); start[1] = 1; int pocz = 0; while (pocz < kolejka.size()) { int a = kolejka[pocz]; pocz++; FOR(i,0,rep[a].size()) { int w = rep[a][i]; if (start[w] == -1) { start[w] = start[a]+1; kolejka.pb(w); } } } vector<PII> kolejka2; FOR(i,0,rep[1].size()-1) { kolejka2.pb(mp(rep[1][i], rep[1][i+1])); start_pary[rep[1][i]][rep[1][i+1]] = 2; } pocz = 0; while (pocz < kolejka2.size()) { int a = kolejka2[pocz].st, b = kolejka2[pocz].nd; pocz++; FOR(i,0,rep[a].size()-1) { int u = rep[a][i], v =rep[a][i+1]; if (start_pary[u][v] == -1) { start_pary[u][v] = start_pary[a][b]+1; kolejka2.pb(mp(u,v)); } } FOR(i,0,rep[b].size()-1) { int u = rep[b][i], v =rep[b][i+1]; if (start_pary[u][v] == -1) { start_pary[u][v] = start_pary[a][b]+1; kolejka2.pb(mp(u,v)); } } int u = rep[a].back(), v = rep[b][0]; if (start_pary[u][v] == -1) { start_pary[u][v] = start_pary[a][b]+1; kolejka2.pb(mp(u,v)); } } } int p = 521; int mod = 1070000027; VI pref[505][13]; VI hashe_pref[505][13]; VI suf[505][13]; VI hashe_suf[505][13]; int przekracza[505]; int hashS[1005][1005]; void przeklej_przod(VI& v1, VI& v2, int ile) { FOR(i,0,ile) v2.pb(v1[i]); } void przeklej_tyl(VI& v1, VI& v2, int ile) { FOR(i,0,ile) v2.pb(v1[v1.size()-ile+i]); } void generuj() { FOR(i,1,n+1){ VI v; v.pb(i); pref[i][0] = v; } FOR(j,1,13) { FOR(i,1,n+1) { int hp_dl = 0; FORE(k,pref[i][j-1]) { int jeszcze = min( (int) rep[*k].size(), (m+1)-hp_dl); if (jeszcze == 0) break; przeklej_przod(rep[*k],pref[i][j], jeszcze); hp_dl += jeszcze; } } } FOR(i,1,n+1){ FOR(j,0,13) { if(pref[i][j].size()>m){ przekracza[i] = j; break; } } } FOR(j,0,13) { FOR(i,1,n+1) { if(pref[i][j].size() < m+1) suf[i][j] = pref[i][j]; else { int hp_dl = 0; int k = suf[i][j-1].size(); while (hp_dl < m+1) { k--; int literka = suf[i][j-1][k]; hp_dl += rep[literka].size(); } // debug2(k,hp_dl); // debug(suf[i][j-1][k]); przeklej_tyl(rep[suf[i][j-1][k]], suf[i][j], rep[suf[i][j-1][k]].size()-(hp_dl-(m+1))); k++; FOR(u,k,suf[i][j-1].size()) { int literka = suf[i][j-1][u]; przeklej_tyl(rep[suf[i][j-1][u]], suf[i][j], rep[suf[i][j-1][u]].size()); } //debug2(i,j); //debugv(suf[i][j]); } } } } map<int, VI> hasher[1005]; // hashe[i] , dla hasha podsłów długości i zwraca liste poczatkow void licz_hashe() { FOR(i,1,n+1) { FOR(j,0,13) { int hash = 0; FOR(u,0,pref[i][j].size()) { hash = (hash*1LL*p)%mod; hash += pref[i][j][u]; if (hash >= mod) hash-=mod; hashe_pref[i][j].pb(hash); } } } FOR(i,1,n+1) { FOR(j,0,13) { int hash = 0; int potega = 1; FORD(u,suf[i][j].size()-1,0) { hash += (potega*1LL*suf[i][j][u])%mod; if (hash>= mod) hash-=mod; hashe_suf[i][j].pb(hash); potega = (potega*1LL*p)%mod; } } } FOR(i,0,m) hashS[i][i] = S[i]; FOR(d,1,m) { FOR(i,0,m-d) { hashS[i][i+d] = (hashS[i][i+d-1]*1LL*p)%mod; hashS[i][i+d] += S[i+d]; if (hashS[i][i+d] >= mod) hashS[i][i+d] -= mod; } } FOR(d,0,m) { FOR(i,0,m-d) { if(hasher[d].find(hashS[i][i+d])!=hasher[d].end()) { hasher[d][hashS[i][i+d]].pb(i); } else { VI w; w.pb(i); hasher[d][hashS[i][i+d]] = w; } } } // FOR(i,0,m) debugt(hashS[i],m); } int best = 1e9; int zJednego(int k) { if( rep[k].size() >= m ) { FOR(i,0,rep[k].size()-m+1){ bool ok = true; FOR(j,0,m) { if(rep[k][i+j]!=S[j]) { ok = false; break; } } if(ok) return 1; } } int ans = 1e9; int co_najmniej_krok = przekracza[k]; if(co_najmniej_krok >= 1 && pref[k][co_najmniej_krok-1].size() ==m) co_najmniej_krok--; FOR(i,1,rep[k].size()-1) { int loc_res = 0; int l = rep[k][i]; FOR (step, co_najmniej_krok-1, przekracza[l]) { if(ans < step || best < step) break; int dlugosc_srodka = pref[l][step].size(); int H = hashe_pref[l][step].back(); if (hasher[dlugosc_srodka-1].find(H) != hasher[dlugosc_srodka-1].end()) { FORE(j, hasher[dlugosc_srodka-1][H]) { int pocz = *j; if(pocz==0) continue; if (hashe_suf[rep[k][i-1]][step][pocz-1] == hashS[0][pocz-1]) { int start = pocz+dlugosc_srodka; bool okk = true; int psi = i+1; while (1) { if( psi >= rep[k].size() ) { okk = false; break; } if (pref[rep[k][psi]][step].size() >= m-start) { if(hashe_pref[rep[k][psi]][step][m-start-1] != hashS[start][m-1]) { okk = false; } break; } else { if(hashe_pref[rep[k][psi]][step].back() != hashS[start][start+pref[rep[k][psi]][step].size()-1]) { okk = false; break; } else { start += pref[rep[k][psi]][step].size(); psi++; } } } if(okk) { ans = min(ans, step); } } } } } } return ans+1; } VI pref_pas[505][13]; VI suf_pas[505][13]; void przygotuj_pary() { FOR(i,1,n+1) FOR(j,0,13) FOR(u,0,min((int) pref[i][j].size(),m)) { if(hashe_pref[i][j][u] == hashS[m-1-u][m-1]) pref_pas[i][j].pb(u); } FOR(i,1,n+1) FOR(j,0,13) { FOR(u,0,min((int) suf[i][j].size(),m)) { if(hashe_suf[i][j][u] == hashS[0][u]) suf_pas[i][j].pb(u); } } } bool jest(VI& v, int k) { if(v.size()==0) return false; int lewo = 0, prawo = v.size()-1; while (prawo-lewo > 1) { int sr = (prawo+lewo)/2; if (v[sr] > k) prawo = sr; else lewo = sr; } if(v[lewo]==k) return true; if(v[prawo]==k) return true; return false; } int zPary(int k,int l) { FOR(step,1,13) { if (suf[k][step].size() + pref[l][step].size()<m) continue; FORE(j,suf_pas[k][step]) { int ile = *j+1; int reszta = m-ile-1; if (jest(pref_pas[l][step],reszta)) return step; } } return 1e9; } int main(){ read2(n,m); FOR(j,1,n+1) { make(a); rep[j].clear(); FOR(i,0,a) { make(b); rep[j].pb(b); } } readv(S,m); wypelnij(); //cout<<"WYPELNIL"<<endl; if (m==1) { printf("%d\n",start[S[0]]); return 0; } if (m==2) { printf("%d\n",start_pary[S[0]][S[1]]); return 0; } generuj(); //cout<<"WYGENEROWAL"<<endl; licz_hashe(); przygotuj_pary(); FOR(i,1,n+1) { //debug3(i,start[i], zJednego(i)); if(start[i]!=-1) best = min(best, zJednego(i)+start[i]); } FOR(i,1,n+1) FOR(j,1,n+1) { if(start_pary[i][j]!=-1) best = min(best, zPary(i,j)+start_pary[i][j]); } if(best==1e9) { printf("-1\n"); } else { printf("%d\n",best); } }
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define VI vector<int> #define PII pair<int,int> #define st first #define nd second #define mp make_pair #define pb push_back #define lint long long int #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} using namespace std; int n,m; VI S; VI rep[505]; int start[505]; int start_pary[505][505]; void wypelnij() { FOR(i,1,n+1) start[i] = -1; FOR(i,1,n+1) FOR(j,1,n+1) start_pary[i][j] = -1; VI kolejka; kolejka.pb(1); start[1] = 1; int pocz = 0; while (pocz < kolejka.size()) { int a = kolejka[pocz]; pocz++; FOR(i,0,rep[a].size()) { int w = rep[a][i]; if (start[w] == -1) { start[w] = start[a]+1; kolejka.pb(w); } } } vector<PII> kolejka2; FOR(i,0,rep[1].size()-1) { kolejka2.pb(mp(rep[1][i], rep[1][i+1])); start_pary[rep[1][i]][rep[1][i+1]] = 2; } pocz = 0; while (pocz < kolejka2.size()) { int a = kolejka2[pocz].st, b = kolejka2[pocz].nd; pocz++; FOR(i,0,rep[a].size()-1) { int u = rep[a][i], v =rep[a][i+1]; if (start_pary[u][v] == -1) { start_pary[u][v] = start_pary[a][b]+1; kolejka2.pb(mp(u,v)); } } FOR(i,0,rep[b].size()-1) { int u = rep[b][i], v =rep[b][i+1]; if (start_pary[u][v] == -1) { start_pary[u][v] = start_pary[a][b]+1; kolejka2.pb(mp(u,v)); } } int u = rep[a].back(), v = rep[b][0]; if (start_pary[u][v] == -1) { start_pary[u][v] = start_pary[a][b]+1; kolejka2.pb(mp(u,v)); } } } int p = 521; int mod = 1070000027; VI pref[505][13]; VI hashe_pref[505][13]; VI suf[505][13]; VI hashe_suf[505][13]; int przekracza[505]; int hashS[1005][1005]; void przeklej_przod(VI& v1, VI& v2, int ile) { FOR(i,0,ile) v2.pb(v1[i]); } void przeklej_tyl(VI& v1, VI& v2, int ile) { FOR(i,0,ile) v2.pb(v1[v1.size()-ile+i]); } void generuj() { FOR(i,1,n+1){ VI v; v.pb(i); pref[i][0] = v; } FOR(j,1,13) { FOR(i,1,n+1) { int hp_dl = 0; FORE(k,pref[i][j-1]) { int jeszcze = min( (int) rep[*k].size(), (m+1)-hp_dl); if (jeszcze == 0) break; przeklej_przod(rep[*k],pref[i][j], jeszcze); hp_dl += jeszcze; } } } FOR(i,1,n+1){ FOR(j,0,13) { if(pref[i][j].size()>m){ przekracza[i] = j; break; } } } FOR(j,0,13) { FOR(i,1,n+1) { if(pref[i][j].size() < m+1) suf[i][j] = pref[i][j]; else { int hp_dl = 0; int k = suf[i][j-1].size(); while (hp_dl < m+1) { k--; int literka = suf[i][j-1][k]; hp_dl += rep[literka].size(); } // debug2(k,hp_dl); // debug(suf[i][j-1][k]); przeklej_tyl(rep[suf[i][j-1][k]], suf[i][j], rep[suf[i][j-1][k]].size()-(hp_dl-(m+1))); k++; FOR(u,k,suf[i][j-1].size()) { int literka = suf[i][j-1][u]; przeklej_tyl(rep[suf[i][j-1][u]], suf[i][j], rep[suf[i][j-1][u]].size()); } //debug2(i,j); //debugv(suf[i][j]); } } } } map<int, VI> hasher[1005]; // hashe[i] , dla hasha podsłów długości i zwraca liste poczatkow void licz_hashe() { FOR(i,1,n+1) { FOR(j,0,13) { int hash = 0; FOR(u,0,pref[i][j].size()) { hash = (hash*1LL*p)%mod; hash += pref[i][j][u]; if (hash >= mod) hash-=mod; hashe_pref[i][j].pb(hash); } } } FOR(i,1,n+1) { FOR(j,0,13) { int hash = 0; int potega = 1; FORD(u,suf[i][j].size()-1,0) { hash += (potega*1LL*suf[i][j][u])%mod; if (hash>= mod) hash-=mod; hashe_suf[i][j].pb(hash); potega = (potega*1LL*p)%mod; } } } FOR(i,0,m) hashS[i][i] = S[i]; FOR(d,1,m) { FOR(i,0,m-d) { hashS[i][i+d] = (hashS[i][i+d-1]*1LL*p)%mod; hashS[i][i+d] += S[i+d]; if (hashS[i][i+d] >= mod) hashS[i][i+d] -= mod; } } FOR(d,0,m) { FOR(i,0,m-d) { if(hasher[d].find(hashS[i][i+d])!=hasher[d].end()) { hasher[d][hashS[i][i+d]].pb(i); } else { VI w; w.pb(i); hasher[d][hashS[i][i+d]] = w; } } } // FOR(i,0,m) debugt(hashS[i],m); } int best = 1e9; int zJednego(int k) { if( rep[k].size() >= m ) { FOR(i,0,rep[k].size()-m+1){ bool ok = true; FOR(j,0,m) { if(rep[k][i+j]!=S[j]) { ok = false; break; } } if(ok) return 1; } } int ans = 1e9; int co_najmniej_krok = przekracza[k]; if(co_najmniej_krok >= 1 && pref[k][co_najmniej_krok-1].size() ==m) co_najmniej_krok--; FOR(i,1,rep[k].size()-1) { int loc_res = 0; int l = rep[k][i]; FOR (step, co_najmniej_krok-1, przekracza[l]) { if(ans < step || best < step) break; int dlugosc_srodka = pref[l][step].size(); int H = hashe_pref[l][step].back(); if (hasher[dlugosc_srodka-1].find(H) != hasher[dlugosc_srodka-1].end()) { FORE(j, hasher[dlugosc_srodka-1][H]) { int pocz = *j; if(pocz==0) continue; if (hashe_suf[rep[k][i-1]][step][pocz-1] == hashS[0][pocz-1]) { int start = pocz+dlugosc_srodka; bool okk = true; int psi = i+1; while (1) { if( psi >= rep[k].size() ) { okk = false; break; } if (pref[rep[k][psi]][step].size() >= m-start) { if(hashe_pref[rep[k][psi]][step][m-start-1] != hashS[start][m-1]) { okk = false; } break; } else { if(hashe_pref[rep[k][psi]][step].back() != hashS[start][start+pref[rep[k][psi]][step].size()-1]) { okk = false; break; } else { start += pref[rep[k][psi]][step].size(); psi++; } } } if(okk) { ans = min(ans, step); } } } } } } return ans+1; } VI pref_pas[505][13]; VI suf_pas[505][13]; void przygotuj_pary() { FOR(i,1,n+1) FOR(j,0,13) FOR(u,0,min((int) pref[i][j].size(),m)) { if(hashe_pref[i][j][u] == hashS[m-1-u][m-1]) pref_pas[i][j].pb(u); } FOR(i,1,n+1) FOR(j,0,13) { FOR(u,0,min((int) suf[i][j].size(),m)) { if(hashe_suf[i][j][u] == hashS[0][u]) suf_pas[i][j].pb(u); } } } bool jest(VI& v, int k) { if(v.size()==0) return false; int lewo = 0, prawo = v.size()-1; while (prawo-lewo > 1) { int sr = (prawo+lewo)/2; if (v[sr] > k) prawo = sr; else lewo = sr; } if(v[lewo]==k) return true; if(v[prawo]==k) return true; return false; } int zPary(int k,int l) { FOR(step,1,13) { if (suf[k][step].size() + pref[l][step].size()<m) continue; FORE(j,suf_pas[k][step]) { int ile = *j+1; int reszta = m-ile-1; if (jest(pref_pas[l][step],reszta)) return step; } } return 1e9; } int main(){ read2(n,m); FOR(j,1,n+1) { make(a); rep[j].clear(); FOR(i,0,a) { make(b); rep[j].pb(b); } } readv(S,m); wypelnij(); //cout<<"WYPELNIL"<<endl; if (m==1) { printf("%d\n",start[S[0]]); return 0; } if (m==2) { printf("%d\n",start_pary[S[0]][S[1]]); return 0; } generuj(); //cout<<"WYGENEROWAL"<<endl; licz_hashe(); przygotuj_pary(); FOR(i,1,n+1) { //debug3(i,start[i], zJednego(i)); if(start[i]!=-1) best = min(best, zJednego(i)+start[i]); } FOR(i,1,n+1) FOR(j,1,n+1) { if(start_pary[i][j]!=-1) best = min(best, zPary(i,j)+start_pary[i][j]); } if(best==1e9) { printf("-1\n"); } else { printf("%d\n",best); } } |