#include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; int n,a,b,c,d,k,m,q,z; const int MXN=505; int WYW; VI wzor; LL hwzor; int R(int a,int b) { return rand()%(b-a+1)+a; } VI V[MXN]; void operator+=(VI &a,VI b) { REP(i,b.size()) { a.PB(b[i]); } } VI next(VI c) { VI c2; REP(i,c.size()) { c2+=V[c[i]]; } return c2; } const LL MOD=(1<<17)-1;//chyba optymalne dla 10^6 elementów, ew 16 struct mapa { int SIZ=0; vector<pair<LL,int> > V[MOD+1]; inline int &operator[](LL a) { int x=a&MOD; for(int i=0;i<V[x].size();i++) { if(V[x][i].first==a)return V[x][i].second; } SIZ++; V[x].push_back(make_pair(a,0)); return V[x].back().second; } inline bool count(LL a) { int x=a&MOD; for(int i=0;i<V[x].size();i++) { if(V[x][i].f==a)return 1; } return 0; } }S; queue<VI> Q; int l=1013; LL L=1000000000001371; LL ldom=1; LL hasz(VI V) { LL ret=0; REP(i,V.size()) { ret=(ret*l+V[i])%L; } return ret; } void look(VI V,int lvl) { // WYW++; // if(S.count(hasz(V)))return; LL hV=hasz(V); // cerr<<"V: "<<V.size()<<" z: ";REP(i,V.size())cerr<<V[i]<<" ";cerr<<endl; // cerr<<hV<<endl; if(hV==hwzor) { printf("%d\n",lvl+1); exit(0); } S[hV]=lvl+1; Q.push(V); } VI tovec(VI &V,int a,int b) { VI ret; FOR(i,a,b) ret.PB(V[i]); return ret; } void wrzucaj(VI V,int lvl) { if(V.size()<m) { if(S.count(hasz(V))==0) look(V,lvl); return; } LL h=hasz(tovec(V,0,m-1)); if(S.count(h)==0) look(tovec(V,0,m-1),lvl); FOR(i,m,(int)V.size()-1) { h*=l; h+=V[i]; h-=ldom*V[i-m]; h+=L*1000LL; h%=L; if(S.count(h))continue; look(tovec(V,i-m+1,i),lvl); } } void reg(VI V) { Q.push(V); S[hasz(V)]=1; while(Q.size()) { V=Q.front(); Q.pop(); LL hV=hasz(V); int lvl=S[hV]; // cerr<<lvl<<" r"<<endl; // cerr<<"V:: ";REP(i,V.size())cerr<<V[i]<<" ";cerr<<endl; V=next(V); wrzucaj(V,lvl); } } void go() { scanf("%d%d",&n,&m); REP(i,m)ldom=ldom*l%L; FOR(i,1,n) { int w; scanf("%d",&w); REP(j,w) { scanf("%d",&a); V[i].PB(a); } } REP(i,m) { scanf("%d",&a); wzor.PB(a); } if(wzor.size()==1&&wzor[0]==1) { puts("1"); return; } hwzor=hasz(wzor); VI c={1}; reg(c); puts("-1");return; /* cerr<<"MXLVL: "<<mxl<<endl; printf("S.size: %d\n",S.SIZ); cerr<<"WYW: "<<WYW<<endl; return; */ } main() { go(); }
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 | #include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; int n,a,b,c,d,k,m,q,z; const int MXN=505; int WYW; VI wzor; LL hwzor; int R(int a,int b) { return rand()%(b-a+1)+a; } VI V[MXN]; void operator+=(VI &a,VI b) { REP(i,b.size()) { a.PB(b[i]); } } VI next(VI c) { VI c2; REP(i,c.size()) { c2+=V[c[i]]; } return c2; } const LL MOD=(1<<17)-1;//chyba optymalne dla 10^6 elementów, ew 16 struct mapa { int SIZ=0; vector<pair<LL,int> > V[MOD+1]; inline int &operator[](LL a) { int x=a&MOD; for(int i=0;i<V[x].size();i++) { if(V[x][i].first==a)return V[x][i].second; } SIZ++; V[x].push_back(make_pair(a,0)); return V[x].back().second; } inline bool count(LL a) { int x=a&MOD; for(int i=0;i<V[x].size();i++) { if(V[x][i].f==a)return 1; } return 0; } }S; queue<VI> Q; int l=1013; LL L=1000000000001371; LL ldom=1; LL hasz(VI V) { LL ret=0; REP(i,V.size()) { ret=(ret*l+V[i])%L; } return ret; } void look(VI V,int lvl) { // WYW++; // if(S.count(hasz(V)))return; LL hV=hasz(V); // cerr<<"V: "<<V.size()<<" z: ";REP(i,V.size())cerr<<V[i]<<" ";cerr<<endl; // cerr<<hV<<endl; if(hV==hwzor) { printf("%d\n",lvl+1); exit(0); } S[hV]=lvl+1; Q.push(V); } VI tovec(VI &V,int a,int b) { VI ret; FOR(i,a,b) ret.PB(V[i]); return ret; } void wrzucaj(VI V,int lvl) { if(V.size()<m) { if(S.count(hasz(V))==0) look(V,lvl); return; } LL h=hasz(tovec(V,0,m-1)); if(S.count(h)==0) look(tovec(V,0,m-1),lvl); FOR(i,m,(int)V.size()-1) { h*=l; h+=V[i]; h-=ldom*V[i-m]; h+=L*1000LL; h%=L; if(S.count(h))continue; look(tovec(V,i-m+1,i),lvl); } } void reg(VI V) { Q.push(V); S[hasz(V)]=1; while(Q.size()) { V=Q.front(); Q.pop(); LL hV=hasz(V); int lvl=S[hV]; // cerr<<lvl<<" r"<<endl; // cerr<<"V:: ";REP(i,V.size())cerr<<V[i]<<" ";cerr<<endl; V=next(V); wrzucaj(V,lvl); } } void go() { scanf("%d%d",&n,&m); REP(i,m)ldom=ldom*l%L; FOR(i,1,n) { int w; scanf("%d",&w); REP(j,w) { scanf("%d",&a); V[i].PB(a); } } REP(i,m) { scanf("%d",&a); wzor.PB(a); } if(wzor.size()==1&&wzor[0]==1) { puts("1"); return; } hwzor=hasz(wzor); VI c={1}; reg(c); puts("-1");return; /* cerr<<"MXLVL: "<<mxl<<endl; printf("S.size: %d\n",S.SIZ); cerr<<"WYW: "<<WYW<<endl; return; */ } main() { go(); } |