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
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <queue>

using namespace std;

typedef vector<uint16_t> Sequence;
typedef vector<Sequence> Sequences;
typedef set<uint16_t> SeqSet;
typedef map<uint32_t, SeqSet> Hashes;
typedef vector<uint32_t> Distances;

Hashes hashesFront;
Hashes hashesBack;
Hashes hashesFull;
Hashes hashesMiddle;

Distances *distances = 0;

const uint32_t MUL=501;

void copyHashOwners(Sequence &starters, Hashes &hashes, uint32_t hash)
{
	auto it = hashes.find(hash);
	if (it != hashes.end())
	{
		starters.insert(starters.end(), it->second.begin(), it->second.end());
	}
}

set<uint32_t> cache;

void check_full(Sequence &newS, int from, Sequence &S,
		Sequences &starters, Sequences &output, Sequences &v, uint32_t hash)
{
	if (from >= S.size()) return;
	for (auto &next: starters[from])
	{
		newS.push_back(next+1);

		uint32_t local_hash = hash * MUL;
		local_hash += next+1;

			if (from + v[next].size() >= S.size())
			{
				if (cache.find(local_hash) == cache.end())
				{
					cache.insert(local_hash);
						output.push_back(newS);
				}
			}
			else
				check_full(newS, from + v[next].size(), S, starters,
						output, v, local_hash);

		newS.pop_back();
	}
}

const int NOTFOUND = 1000000000;

unsigned int bt(Sequences &v, Sequence &S, int depth)
{
	unsigned int res = NOTFOUND;
	Sequences starters(S.size());
	uint32_t hashBack = 0;
	uint32_t pmulback = 1;
	for (int i=S.size()-1; i>=0; i--)
	{
		uint32_t hash = 0;
		uint32_t pmul = 1;
		for (int j=i; j>=0; j--)
		{
			hash += S[j]*pmul;
			pmul *= MUL;
			copyHashOwners(starters[j], hashesFull, hash);
		}
		hashBack += S[i]*pmulback;
		pmulback *= MUL;
  	copyHashOwners(starters[i], hashesFront, hashBack);
	}
	// hashBack now contains full hash

	for (int i = S.size()-1; i >= 0; i--)
	{
		for (auto &p: starters[i])
		{
       uint16_t dist = v[p].size();
			 if (i+dist<S.size() && starters[i+dist].empty())
			 {
				 p = 65535;
			 }
		}
		starters[i].erase ( remove_if (starters[i].begin(), starters[i].end(),
					[] (uint16_t elem) { return elem == 65535; }),
				starters[i].end() );
	}
	Sequences prefixes(S.size());

	uint32_t hash = 0;
	uint32_t pmul = 1;
	// get also front hashes, (eliminate impossible - TODO?)
	for (int i = 0; i < S.size(); i++)
	{
		hash += S[i]*pmul;
		pmul *= MUL;
		copyHashOwners(prefixes[i], hashesBack, hash);
	}

	Sequences sequences;
	Sequence newS;
	check_full(newS, 0, S, starters, sequences, v, 0);
	newS.push_back(0);
	// generate possible parent strings
  for (int i = 0; i < S.size()-1; i++)
	{
		// prefixes ending at i: check full from (i+1)
		auto &p = prefixes[i];
		int psize = p.size();
		for (auto &prefix: p)
		{
			newS.pop_back();
			newS.push_back(prefix+1);
			check_full(newS, i+1, S, starters, sequences, v, prefix+1);
		}
	}
	if (!prefixes[S.size()-1].empty())
	{
		for (auto &p : prefixes[S.size()-1])
		{
			res = min(res, depth+distances->at(p));
		}
	}
	for (auto &s: sequences)
	{
    if (s.size()==1 && s[0]==1)
		{
			return depth;
		}
	}
	// check middle sequences
	auto itMiddle = hashesMiddle.find(hashBack);
	if (itMiddle != hashesMiddle.end())
	{
		for (auto &elem : itMiddle->second)
		{
			res = min(res, depth+distances->at(elem));
		}
	}
	for (auto &s: sequences)
	{
		unsigned int local_res;
		if (s.size() == 1)
		{
       local_res = depth+distances->at(s[0]-1);
		}
		else
		{
			local_res =	bt(v, s, depth + 1);
		}
		res = min(res, local_res);
	}
	return res;
}

void putHash(Hashes &hashes, uint32_t hash, uint16_t i)
{
			auto it = hashes.find(hash);
			if (it == hashes.end())
			{
        it = hashes.insert(make_pair(hash, SeqSet())).first;
			}
			it->second.insert(i);
}

int main()
{
	ios_base::sync_with_stdio (false);

	int N,M;

	cin >> N >> M;

	Sequences v(N);

	for (int i = 0; i < N; i++)
	{
		int I;
		cin >> I;
		v[i].reserve(I);
		for (int j = 0; j < I; j++)
		{
			int a;
			cin >> a;
			v[i].push_back(a);
		}
	}

	const uint32_t NOTVISITED = 1000000000;
	Distances singleDist(N, NOTVISITED);
	queue<uint16_t> q;
	q.push(1);
	singleDist[0] = 0;
	while (!q.empty())
	{
		auto elem = q.front()-1;
		q.pop();
		for (auto next: v[elem])
		{
			if (singleDist[next-1] == NOTVISITED)
			{
				singleDist[next-1] = singleDist[elem]+1;
				q.push(next);
			}
		}
	}

	distances = &singleDist;

	Sequence S;
	S.reserve(M);
	for (int i = 0; i < M; i++)
	{
		int a;
		cin >> a;
		S.push_back(a);
	}

  for (int i = 0; i < N; i++)
	{
		auto &seq = v[i];
		uint32_t hash = 0;
		uint32_t hashBack = 0;
		uint16_t seqSize = seq.size();
		for (int j = 0; j < seqSize-1; j++)
		{
			hash *= MUL;
			hash += seq[j];
			putHash(hashesFront, hash, i);

			hashBack *= MUL;
			hashBack += seq[seqSize-j-1];
			putHash(hashesBack, hashBack, i);
		}
		hash *= MUL;
		hash += seq[seqSize-1];
    putHash(hashesFull, hash, i);
		for (int k = 1; k < seqSize-2; k++)
		{
			hash = seq[k];
			for (int j = k+1; j < seqSize-1; j++)
			{
				hash *= MUL;
				hash += seq[j];
				putHash(hashesMiddle, hash, i);
			}
		}
	}

	if (S.size() == 1 && S[0] == 1)
		cout << 1 << endl;
	else
	{
		int res = bt(v, S, 2);
		cout << (res==NOTFOUND ? -1 : res) << endl;
	}

  return 0;
}