#include #include #include using namespace std; int n, l, b, f; #define DEBUG(a) vector graf[2000]; const int ciflen = 1000000000; struct L { int len; int cif[100]; void print() const { for (int i = len-1; i >= 0; i--) { printf("%d", cif[i]); } printf("\n"); } void operator = (int a) { len = 1; cif[0] = a; } /* void operator += (int a) { cif[0] += a; int pos = 0; while (pos < len && cif[pos] > ciflen) { cif[pos] -= ciflen; cif[++pos]++; } if (cif[len-1] > ciflen) { len++; cif[len-1] = 1; } } */ void operator += (const L &a) { if (len < a.len) len = a.len; bool carry = false; for (int i = 0; i < len; i++) { cif[i] += a.cif[i]; if (carry) cif[i] += 1; carry = false; if (cif[i] >= ciflen) { carry = true; cif[i] -= ciflen; } } if (carry) { len++; cif[len-1] = 1; } } L() { len = 1; for (int i = 0; i < 100; i++) cif[i] = 0; } }; struct V { int vrchol; L nasob; }; bool operator == (const V &a, const V &b) { return a.vrchol == b.vrchol; } bool operator < (const V &a, const V &b) { return a.vrchol < b.vrchol; } bool operator <= (const V &a, const V &b) { return a.vrchol <= b.vrchol; } int main() { /* L xxx, yyy; xxx = 999999; yyy = 999999; xxx += yyy; xxx.print(); */ scanf("%d %d %d %d\n",&n,&l,&b,&f); while (n >0 && l > 0 && b > 0 && f > 0) { for (int i = 1; i < n+1; i++) { graf[i].clear(); int cv, ps, vrch; scanf("%d %d", &cv, &ps); for (int j = 0; j < ps; j++) { scanf("%d", &vrch); graf[cv].push_back(vrch); DEBUG( printf("cesta z %d do %d\n", cv , vrch); ) } } for (int i = 0; i < b; i++) { int start; scanf("%d", &start); graf[n+1].push_back(start); } for (int i = 0; i < f; i++) { int konec; scanf("%d", &konec); graf[konec].push_back(n+2); } // cool :-) if (l > n) printf("impossible\n"); else { // CVUTaci neumi nakonfigurit vim V pocatek; pocatek.vrchol = n+1; pocatek.nasob = 1; set vrstva; set nasled; vrstva.insert(pocatek); for (int i = 0; i < l+1; i++) { for (set::iterator it = vrstva.begin(); it != vrstva.end(); ++it) { for (vector::iterator it2 = graf[(*it).vrchol].begin(); it2 != graf[(*it).vrchol].end(); ++it2) { V vyhledavatko; vyhledavatko.vrchol = (*it2); set::iterator f = nasled.find(vyhledavatko); if (f == nasled.end()) { V novy; novy.vrchol = (*it2); novy.nasob = (*it).nasob; nasled.insert(novy); } else { V tmp = (*f); nasled.erase(f); tmp.nasob += (*it).nasob; nasled.insert(tmp); //(*f).nasob += (*it).nasob; } } } vrstva = nasled; nasled.clear(); DEBUG( printf("vrstva %d: \n", i); for (set::iterator it = vrstva.begin(); it != vrstva.end(); ++it) { printf(" %d (%d)\n", (*it).vrchol, (*it).nasob); } ) } V koncovych; koncovych.vrchol = n+2; set::iterator f = vrstva.find(koncovych); if (f != vrstva.end()) (*f).nasob.print(); else printf("impossible\n"); } scanf("%d %d %d %d\n",&n,&l,&b,&f); } return 0; }