#include <cstdio> #include <algorithm> #include <vector> #include <iostream> std::vector<int> s[505], ter, poprz; long long wywal, odp=1, pot=1, x=31, y=1000000007, wz, wzpop, n, m, l, h, hash[4000005]; bool spr(){ for( int i=1; i<=poprz.size(); i++ ){ hash[i]=((hash[i-1]*x)%y+poprz[i-1])%y; if( i-m>=0 && (hash[i]-(hash[i-m]*pot)%y)%y==wz ){ printf("%lld", odp); return 1; } wywal++; } for( int i=1; i<=ter.size(); i++ ){ hash[i]=0; wywal++; } return 0; } int main(){ scanf("%lld%lld", &n, &m); for( int i=1; i<=n; i++ ){ scanf("%lld", &l); for( int j=1; j<=l; j++ ){ scanf("%lld", &h); s[i].push_back(h); } } for( int i=1; i<=m; i++ ){ scanf("%lld", &h); wz=(wzpop*x%y+h)%y; wzpop=wz; pot=(pot*x)%y; } poprz.push_back(1); while(1){ /* for( std::vector<int>::iterator it=poprz.begin(); it!=poprz.end(); it++ ){ printf("%lld ", *it); } printf("\n"); if( odp>5 ) return 0;*/ if( spr() ) return 0; for( std::vector<int>::iterator it=poprz.begin(); it!=poprz.end(); it++ ){ for( std::vector<int>::iterator it2=s[*it].begin(); it2!=s[*it].end(); it2++ ){ ter.push_back(*it2); if( ter.size()>4000000 || wywal>100000000 ){ printf("-1\n"); return 0; } wywal++; } } odp++; poprz=ter; ter.clear(); } }
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 | #include <cstdio> #include <algorithm> #include <vector> #include <iostream> std::vector<int> s[505], ter, poprz; long long wywal, odp=1, pot=1, x=31, y=1000000007, wz, wzpop, n, m, l, h, hash[4000005]; bool spr(){ for( int i=1; i<=poprz.size(); i++ ){ hash[i]=((hash[i-1]*x)%y+poprz[i-1])%y; if( i-m>=0 && (hash[i]-(hash[i-m]*pot)%y)%y==wz ){ printf("%lld", odp); return 1; } wywal++; } for( int i=1; i<=ter.size(); i++ ){ hash[i]=0; wywal++; } return 0; } int main(){ scanf("%lld%lld", &n, &m); for( int i=1; i<=n; i++ ){ scanf("%lld", &l); for( int j=1; j<=l; j++ ){ scanf("%lld", &h); s[i].push_back(h); } } for( int i=1; i<=m; i++ ){ scanf("%lld", &h); wz=(wzpop*x%y+h)%y; wzpop=wz; pot=(pot*x)%y; } poprz.push_back(1); while(1){ /* for( std::vector<int>::iterator it=poprz.begin(); it!=poprz.end(); it++ ){ printf("%lld ", *it); } printf("\n"); if( odp>5 ) return 0;*/ if( spr() ) return 0; for( std::vector<int>::iterator it=poprz.begin(); it!=poprz.end(); it++ ){ for( std::vector<int>::iterator it2=s[*it].begin(); it2!=s[*it].end(); it2++ ){ ter.push_back(*it2); if( ter.size()>4000000 || wywal>100000000 ){ printf("-1\n"); return 0; } wywal++; } } odp++; poprz=ter; ter.clear(); } } |