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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

vector<int> V[500];
vector<int> H[20000];
vector<int> S;
int n, m;

bool Search(int i)
{
    int it = 0;
    for(int j = 0; j < H[i].size(); j++)
    {
        it = j;
        for(int k = 0; k < S.size(); k++)
        {
            if(H[i][it] == S[k])
                it++;
            else break;

            if(k == S.size() - 1)
                return true;
        }
    }
    return false;
}

void Split()
{
    H[0] = V[1];
    for(int i = 0; ; i++)
    {
        int val = 0;

        for(int j = 0; j < H[i].size(); j++)
        {
            val = H[i][j];
            H[i + 1].insert( H[i + 1].end(), V[val].begin(), V[val].end() );
        }

        if(Search(i + 1) == true)
        {
            cout << i + 3 << endl;
            break;
        }
    }
}

int main()
{
 	std::ios_base::sync_with_stdio(false);
    cin >> n >> m;

    for(int i = 1; i <= n; i++)
    {
        int a;
        cin >> a;
        for(int j = 0; j < a; j++)
        {
            int b;
            cin >> b;
            V[i].push_back(b);
        }
    }

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

    Split();

    return 0;
}