Problem:
問題文
キュゥべえ
はワルプルギスの夜に向けて魔女の合成に明け暮れている．ワルプルギスの夜のために，特別な
グリーフシード
(魔女の卵) を使って手に入れやすい魔女をうまく合成し目的である舞台装置の魔女を創り上げなければならないからである．
キュゥべえ
は手に入れやすい種類の魔女の
グリーフシード
から魔女の素となるものを取り出し，空の特別な
グリーフシード
に入れることで，その
グリーフシード
に魔女を宿らせることができる．また，この世界には，ある組み合わせで複数の魔女を合成すると新たな
1
つの魔女を生み出すことができるという合成法則がいくつかある．そこで，いくつかの特別な
グリーフシード
を使い，それらの中にある魔女の素となるものを合成し，
1
つの特別な
グリーフシード
に新たな魔女を宿らせることができる．また，合成した結果空となった特別な
グリーフシード
は再利用することができる．例えば，
3
個の特別な
グリーフシード
に入った魔女を合成すると，それらの
3
個の
グリーフシード
は，新たな魔女が宿った
1
個の特別な
グリーフシード
と
2
個の空の特別な
グリーフシード
となる．しかし，同じ種類の魔女を同時に複数の
グリーフシード
に入れることは出来ない．
キュゥべえ
は魔女の特別な
グリーフシード
への寄宿と魔女の合成を繰り返すことにより舞台装置の魔女を創りだしたい．舞台装置の魔女を創りだすにはいくつの特別な
グリーフシード
が必要か求めよ．
入力形式
入力は以下のような形式で与えられる．
N\ E\ T\\
W_1\ W_2\ …\ W_N\\
G_1\ C_1\ S_{1,1}\ S_{1,2}\ …\ S_{1,C_1}\\
G_2\ C_2\ S_{2,1}\ S_{2,2}\ …\ S_{2,C_2}\\
…\\
G_E\ C_E\ S_{E,1}\ S_{E,2}\ …\ S_{E,C_E}\\
N
は魔女の種類の個数，
E
は合成法則の個数，
T
は舞台装置の魔女の番号である．
W_i
は
i
番目の魔女が手に入れやすい魔女であるかどうかを表し，
1
ならば手に入れやすい魔女であり，
0
ならば手に入れやすい魔女ではない．
続く
E
行に合成法則が次のように表される：「番号が
S_{i,1}\ S_{i,2}\ …\ S_{i,C_i}
の魔女を合成することによって
G_i
の番号の魔女が得られる．」
出力形式
舞台装置の魔女を創りだすのに最低限必要な特別な
グリーフシード
の個数を出力せよ．創りだすことができない場合は
-1
を出力せよ．
制約
1 \lt N \leq 300
0 \leq E \leq 1000
1 \leq T \leq N
0 \leq W_i \leq 1\ (1 \leq i \leq N)
1 \leq G_i \leq N\ (1 \leq i \leq E)
2 \leq C_i \leq 10\ (1 \leq i \leq E)
1 \leq S_{i,j} \leq N\ (1 \leq i \leq E,\ 1 \leq j \leq C_i)
\{G_i, S_{i,1}\ S_{i,2}\ …\ S_{i,C_i}\}
は全て異なる．
入力中に同一の合成法則が
2
回以上現れることはない．
入出力例
入力例 1
3 1 3
1 1 0
3 2 1 2
出力例1
2
入力例 2
5 0 1
1 1 1 1 1
出力例 2
1
入力例 3
18 5 1
0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
1 4 2 3 4 5
2 2 6 7
3 3 8 9 10
4 4 11 12 13 14
5 4 15 16 17 18
出力例 3
5
入力例 4
6 4 6
1 1 1 0 0 0
4 2 1 2
5 2 2 3
6 2 4 5
6 3 1 2 4
出力例 4
3
入力例 5
13 9 13
1 1 0 1 1 0 1 1 0 1 1 0 0
3 2 1 2
6 2 4 5
9 2 7 8
12 2 10 11
6 2 2 3
9 2 5 6
12 2 8 9
3 2 11 12
13 4 3 6 9 12
出力例 5
5
入力例 6
4 4 4
1 1 0 0
1 2 2 3
2 2 3 1
3 2 1 2
3 3 1 2 4
出力例 6
-1
Problem Setter: Flat35

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N, E, T;
    cin >> N >> E >> T;
    T--;
    vector<int> W(N);
    rep(i,0,N) cin >> W[i];
    vector<int> G(E);
    vector<vector<int>> S(E);
    rep(i,0,E) {
        cin >> G[i];
        G[i]--;
        int C;
        cin >> C;
        S[i].resize(C);
        rep(j,0,C) cin >> S[i][j], S[i][j]--;
    }
    vector<int> DP(N,inf);
    rep(i,0,N) if (W[i] == 1) DP[i] = 1;
    rep(_,0,4000) {
        rep(i,0,E) {
            vector<int> V;
            rep(j,0,S[i].size()) V.push_back(DP[S[i][j]]);
            sort(ALL(V));
            reverse(ALL(V));
            int MAX = -inf;
            rep(j,0,S[i].size()) chmax(MAX, V[j]+j);
            chmin(DP[G[i]],MAX);
        }
    }
    cout << (DP[T] == inf ? -1 : DP[T]) << endl;
}
accuracy: 1
time_ms: 400
memory_kb: 3492
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#define INF 2000000000

using namespace std;

struct WitchCraft {
    int result_witch;
    int ingredient_count;
    vector<int> ingredients;
};

int witch_count, rule_count, target_witch;
int powers[11], bit_population[1024];
int pending_dependencies[1000], synthesis_matrix[1000][300];
int min_seeds_needed[300];
int dp_table[1024];
vector<int> dependency_graph[1000];
WitchCraft synthesis_rules[1000];

void initialize_powers() {
    for (int i = 0; i < 11; ++i) {
        powers[i] = 1 << i;
    }
}

void precompute_bit_populations() {
    for (int mask = 0; mask < powers[10]; ++mask) {
        int count = 0;
        for (int i = 0; i < 10; ++i) {
            if (mask & (1 << i)) {
                ++count;
            }
        }
        bit_population[mask] = count;
    }
}

void input_data() {
    cin >> witch_count >> rule_count >> target_witch;
    
    for (int i = 0; i < rule_count; ++i) {
        pending_dependencies[i] = 0;
        fill(synthesis_matrix[i], synthesis_matrix[i] + witch_count, 0);
    }

    fill(min_seeds_needed, min_seeds_needed + witch_count, INF);

    for (int i = 0; i < witch_count; ++i) {
        int availability;
        cin >> availability;
        if (availability == 1) {
            min_seeds_needed[i] = 1;
        }
    }

    for (int i = 0; i < rule_count; ++i) {
        cin >> synthesis_rules[i].result_witch >> synthesis_rules[i].ingredient_count;
        --synthesis_rules[i].result_witch;
        for (int j = 0; j < synthesis_rules[i].ingredient_count; ++j) {
            int ingredient;
            cin >> ingredient;
            --ingredient;
            synthesis_rules[i].ingredients.push_back(ingredient);
            if (min_seeds_needed[ingredient] == INF) {
                dependency_graph[ingredient].push_back(i);
                ++pending_dependencies[i];
                synthesis_matrix[i][ingredient] = 1;
            }
        }
    }
}

void process_syntheses() {
    queue<int> ready_synthesis;
    
    for (int i = 0; i < rule_count; ++i) {
        if (pending_dependencies[i] == 0) {
            ready_synthesis.push(i);
        }
    }

    while (!ready_synthesis.empty()) {
        int current_rule = ready_synthesis.front();
        ready_synthesis.pop();

        int num_ingredients = synthesis_rules[current_rule].ingredient_count;
        fill(dp_table, dp_table + powers[num_ingredients], INF);
        dp_table[0] = 0;

        for (int mask = 0; mask < powers[num_ingredients] - 1; ++mask) {
            for (int bit = 0; bit < num_ingredients; ++bit) {
                if (!(mask & (1 << bit))) {
                    int next_mask = mask | (1 << bit);
                    int reusable_seeds = dp_table[mask] - bit_population[mask];
                    dp_table[next_mask] = min(dp_table[next_mask], dp_table[mask] + max(0, min_seeds_needed[synthesis_rules[current_rule].ingredients[bit]] - reusable_seeds));
                }
            }
        }

        bool updated = false;
        int &result_witch_seeds = min_seeds_needed[synthesis_rules[current_rule].result_witch];
        if (result_witch_seeds > dp_table[powers[num_ingredients] - 1]) {
            result_witch_seeds = dp_table[powers[num_ingredients] - 1];
            updated = true;
        }

        for (int dep : dependency_graph[synthesis_rules[current_rule].result_witch]) {
            if (synthesis_matrix[dep][synthesis_rules[current_rule].result_witch] == 1) {
                synthesis_matrix[dep][synthesis_rules[current_rule].result_witch] = 0;
                if (--pending_dependencies[dep] == 0) {
                    ready_synthesis.push(dep);
                }
            } else if (updated && pending_dependencies[dep] == 0) {
                ready_synthesis.push(dep);
            }
        }
    }
}

void output_result() {
    if (min_seeds_needed[target_witch - 1] == INF) {
        cout << "-1\n";
    } else {
        cout << min_seeds_needed[target_witch - 1] << "\n";
    }
}

int main() {
    initialize_powers();
    precompute_bit_populations();
    input_data();
    process_syntheses();
    output_result();

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4664
final_rank: 17

code_snippet: #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define MAX_COST 2000000000
#define MODULO 1000000007
#define EPSILON 1e-9
using namespace std;

struct Synthesis {
    int final_witch;
    int num_ingredients;
    vector<int> ingredients;
};

int num_witches, num_synthesis_rules, target_witch;
int powers_of_two[11], bit_count[1024];
int dependency_count[1000], synthesis_dependency[1000][300];
int min_grief_seeds[300];
int dp[1024];
vector<int> dependency_graph[1000];
Synthesis syntheses[1000];

int main() {
    for (int i = 0; i < 11; i++) {
        powers_of_two[i] = pow(2, i);
    }

    // Precompute the number of bits set for each possible state
    for (int state = 0; state < powers_of_two[10]; state++) {
        int bit_sum = 0;
        for (int bit = 0; bit < 10; bit++) {
            if (state & (1 << bit)) {
                bit_sum++;
            }
        }
        bit_count[state] = bit_sum;
    }

    // Input number of witches, synthesis rules, and the target witch
    scanf("%d %d %d", &num_witches, &num_synthesis_rules, &target_witch);

    // Initialize dependency arrays
    for (int i = 0; i < num_synthesis_rules; i++) {
        dependency_count[i] = 0;
        for (int j = 0; j < num_witches; j++) {
            synthesis_dependency[i][j] = 0;
        }
    }

    // Set the initial minimum grief seeds required for each witch to a large number
    for (int i = 0; i < num_witches; i++) {
        min_grief_seeds[i] = MAX_COST;
    }

    // Input initial availability of witches
    int availability;
    for (int i = 0; i < num_witches; i++) {
        scanf("%d", &availability);
        if (availability == 1) {
            min_grief_seeds[i] = 1;
        }
    }

    // Input synthesis rules
    for (int i = 0; i < num_synthesis_rules; i++) {
        scanf("%d %d", &syntheses[i].final_witch, &syntheses[i].num_ingredients);
        syntheses[i].final_witch--;

        for (int j = 0; j < syntheses[i].num_ingredients; j++) {
            scanf("%d", &availability);
            availability--;
            syntheses[i].ingredients.push_back(availability);
            if (min_grief_seeds[availability] == MAX_COST) {
                dependency_graph[availability].push_back(i);
                dependency_count[i]++;
                synthesis_dependency[i][availability] = 1;
            }
        }
    }

    // Start with synthesis rules that have no dependencies
    queue<int> ready_to_synthesize;
    for (int i = 0; i < num_synthesis_rules; i++) {
        if (dependency_count[i] == 0) {
            ready_to_synthesize.push(i);
        }
    }

    while (!ready_to_synthesize.empty()) {
        int current_rule = ready_to_synthesize.front();
        ready_to_synthesize.pop();

        int num_ingredients = syntheses[current_rule].num_ingredients;
        for (int state = 0; state < powers_of_two[num_ingredients]; state++) {
            dp[state] = MAX_COST;
        }
        dp[0] = 0;

        for (int state = 0; state < powers_of_two[num_ingredients] - 1; state++) {
            for (int ingredient = 0; ingredient < num_ingredients; ingredient++) {
                if (state & (1 << ingredient)) {
                    continue;
                }
                int next_state = state + powers_of_two[ingredient];
                int available_grief_seeds = dp[state] - bit_count[state];
                dp[next_state] = min(dp[next_state], dp[state] + max(0, min_grief_seeds[syntheses[current_rule].ingredients[ingredient]] - available_grief_seeds));
            }
        }

        bool updated = false;
        if (min_grief_seeds[syntheses[current_rule].final_witch] > dp[powers_of_two[num_ingredients] - 1]) {
            min_grief_seeds[syntheses[current_rule].final_witch] = dp[powers_of_two[num_ingredients] - 1];
            updated = true;
        }

        for (int i = 0; i < dependency_graph[syntheses[current_rule].final_witch].size(); i++) {
            int next_rule = dependency_graph[syntheses[current_rule].final_witch][i];
            if (synthesis_dependency[next_rule][syntheses[current_rule].final_witch] == 1) {
                synthesis_dependency[next_rule][syntheses[current_rule].final_witch] = 0;
                dependency_count[next_rule]--;
                if (dependency_count[next_rule] == 0) {
                    ready_to_synthesize.push(next_rule);
                }
            } else {
                if (!updated) continue;
                if (dependency_count[next_rule] == 0) {
                    ready_to_synthesize.push(next_rule);
                }
            }
        }
    }

    target_witch--;
    if (min_grief_seeds[target_witch] == MAX_COST) {
        printf("-1\n");
    } else {
        printf("%d\n", min_grief_seeds[target_witch]);
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5224
final_rank: 19

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, e, t;
    cin >> n >> e >> t;
    vector<int> w(n);
    for (int i = 0; i < n; i++) {
        cin >> w[i];
    }
    const int inf = 1e9;
    vector<int> ans(n, inf);
    for (int i = 0; i < n; i++) {
        if (w[i]) {
            ans[i] = 1;
        }
    }
    vector<int> g(e);
    vector<vector<int>> s(e);
    for (int i = 0; i < e; i++) {
        cin >> g[i];
        g[i]--;
        int c;
        cin >> c;
        for (int j = 0; j < c; j++) {
            int x;
            cin >> x;
            x--;
            s[i].emplace_back(x);
        }
    }
    for (int z = 0; z < e; z++) {
        for (int i = 0; i < e; i++) {
            vector<int> a;
            for (int x: s[i]) {
                a.emplace_back(ans[x]);
            }
            sort(a.rbegin(), a.rend());
            int b = 1;
            for (int j = 0; j < (int) a.size(); j++) {
                b = max(b, a[j] + j);
            }
            ans[g[i]] = min(ans[g[i]], b);
        }
    }
    t--;
    cout << (ans[t] == inf ? -1 : ans[t]) << '\n';
}
accuracy: 1
time_ms: 110
memory_kb: 3488
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, E, T;
    cin >> N >> E >> T;
    --T;
    vector<int> W(N);
    for (auto& x : W) cin >> x;
    vector<vector<int>> S(E);
    vector<int> G(E);
    rep(i,0,E) {
        int C;
        cin >> G[i] >> C;
        --G[i];
        S[i].resize(C);
        for (auto& x : S[i]) cin >> x, --x;
    }
    vector<int> dist(N, 1e9);
    using P = pair<int, int>;
    priority_queue<P, vector<P>, greater<>> pq;
    rep(i,0,N) if (W[i]) {
        dist[i] = 1;
        pq.push({i, 1});
    }
    vector<bool> visited(N);
    while (!pq.empty()) {
        int d, i;
        tie(d, i) = pq.top();
        pq.pop();
        if (d > dist[i]) continue;
        rep(j,0,E) {
            if (visited[G[j]]) continue;
            vector<int> cost;
            for (int k : S[j]) cost.push_back(dist[k]);
            sort(all(cost));
            reverse(all(cost));
            int m = 0;
            rep(k,0,cost.size()) chmax(m, k+cost[k]);
            if (m < dist[G[j]]) {
                dist[G[j]] = m;
                pq.push({m, G[j]});
            }
        }
    }
    cout << (dist[T] < 1e9 ? dist[T] : -1) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3452
final_rank: 8

code_snippet: #include "bits/stdc++.h"
using namespace std;

#define REP(a, b) for (long long a = 0; a < b; ++a)
#define ll long long
#define ld long double
#define ALL(x) begin(x), end(x)

#define int long long

void solve()
{
  int n, e, t;
  cin >> n >> e >> t;
  t--;
  vector<int> dp(n, 1e18);
  vector<vector<int>> vertexs[400];
  REP(i, n)
  {
    int a;
    cin >> a;
    if (a == 1)
      dp[i] = 1;
  }
  REP(i, e)
  {
    int target;
    int a;
    cin >> target;
    cin >> a;
    target--;
    vector<int> gogo;
    REP(q, a)
    {
      int b;
      cin >> b;
      b--;
      gogo.push_back(b);
    }
    vertexs[target].push_back(gogo);
  }

  REP(i, n)
  {
    REP(q, n)
    {
      for (auto x : vertexs[q])
      {
        vector<int> tmps;
        REP(q, x.size())
        {
          tmps.push_back(dp[x[q]]);
        }
        sort(ALL(tmps));
        reverse(ALL(tmps));
        int target = 0;
        REP(q, tmps.size())
        {
          target = max(target, tmps[q] + q);
        }
        dp[q] = min(dp[q], target);
      }
    }
  }
  if (dp[t] == 1e18)
    dp[t] = -1;
  cout << dp[t] << endl;
}

#undef int
int main()
{
  iostream::sync_with_stdio(false);
  cout << fixed << setprecision(100);
  solve();
}
accuracy: 1
time_ms: 40
memory_kb: 3504
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;

template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------------ ACL --------------------

// #include <atcoder/modint>
// constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
// using mint = atcoder::static_modint<MOD>;
// ostream &operator<<(ostream &os, const mint &v) {
//   return os << v.val();
// }

// ------------ End of template --------------

#define endl "\n"
using ll = long long;
using pii = pair<int, int>;

const bool multitest = false;

void solve() {
  int N, E, T;
  cin >> N >> E >> T;
  T--;
  vector<int> dp(N, INF);
  for (int i = 0; i < N; i++) {
    int w;
    cin >> w;
    if (w == 1) dp[i] = 1;
  }

  vector<int> g(E);
  vector<vector<int>> S(E);
  for (int i = 0; i < E; i++) {
    cin >> g[i];
    g[i]--;
    int C;
    cin >> C;
    S[i].resize(C);
    for (int j = 0; j < C; j++) {
      cin >> S[i][j];
      S[i][j]--;
    }
  }

  for (int _ = 0; _ < N; _++) {
    for (int i = 0; i < E; i++) {
      vector<int> cs;
      bool ready = true;
      for (int j : S[i]) {
        if (dp[j] == INF) {
          ready = false;
          break;
        }
        cs.push_back(dp[j]);
      }
      if (!ready) continue;
      sort(all(cs), greater<int>());
      dmp(cs);
      int need = 0;
      for (int j = 0; j < cs.size(); j++) chmax(need, j + cs[j]);
      chmin(dp[g[i]], need);
    }
  }

  if (dp[T] == INF)
    cout << -1 << endl;
  else
    cout << dp[T] << endl;
  return;
}

int main() {
  fastio();
  if (!multitest) {
    solve();
  } else {
    cerr << "[Warning] Multi testcase mode on" << endl;
    int t;
    cin >> t;
    while (t--) solve();
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3484
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

const ll MOD = 1e9 + 7;

// BEGIN CUT
ll modpow(ll x, ll y, ll m) {
    ll a = 1, p = x;
    while (y > 0) {
        if (y % 2 == 0) {
            p = (p * p) % m;
            y /= 2;
        } else {
            a = (a * p) % m;
            y--;
        }
    }
    return a;
}
// END CUT

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
    int N, E, T;
    cin >> N >> E >> T;
    T--;
    vector<int> W(N);
    for (int i = 0; i < N; i++)
        cin >> W[i];
    vector<vector<int>> S(E);
    vector<int> G(E), C(E);
    for (int i = 0; i < E; i++) {
        cin >> G[i] >> C[i];
        G[i]--;
        S[i].resize(C[i]);
        for (int j = 0; j < C[i]; j++) {
            cin >> S[i][j];
            S[i][j]--;
        }
    }
    vector<int> need(N, 1 << 25);
    for (int i = 0; i < N; i++) {
        if (W[i])
            need[i] = 1;
    }
    while (1) {
        bool update = 0;
        for (int i = 0; i < E; i++) {
            vector<int> vec;
            for (int j = 0; j < C[i]; j++) {
                vec.push_back(need[S[i][j]]);
            }
            sort(ALL(vec), greater<int>());
            int ma = 0;
            for (int j = 0; j < C[i]; j++)
                ma = max(ma, j + vec[j]);
            if (need[G[i]] > ma) {
                need[G[i]] = ma;
                update = 1;
                break;
            }
        }
        if (!update)
            break;
    }
    if (need[T] != 1 << 25)
        cout << need[T] << endl;
    else
        cout << -1 << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3460
final_rank: 9

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/all>
//using namespace atcoder;
//const int mod = 1e9 + 7;
const int mod = 998244353;

int main(){
    int n,e,t; cin >> n >> e >> t; t--;
    vl dp(n,inf);
    vb vi(n,false);
    rep(i,n){
        int w; cin >> w;
        if(w == 1) dp[i] = 1, vi[i] = true;
    }
    vector<vvl> G(n);
    rep(i,e){
        int g,c; cin >> g >> c; g--;
        vl v(c);
        rep(j,c) cin >> v[j], v[j]--;
        G[g].push_back(v);
    }
    rep(z,2020){
        rep(u,n){
            for(auto v : G[u]){
                vl costs;
                for(auto x : v){
                    costs.push_back(dp[x]);
                }
                sort(rall(costs));
                ll s = 0;
                rep(i,costs.size()) chmax(s,costs[i]+i);
                chmin(dp[u],s);
            }
        }
    }
    ll ans = dp[t];
    if(ans == inf) ans = -1;
    cout << ans << endl;
}
accuracy: 1
time_ms: 250
memory_kb: 3456
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N, E, T;
    cin >> N >> E >> T;
    T--;
    vector<int> W(N);
    for (auto&& w : W) {
        cin >> w;
    }
    vector<int> G(E), C(E);
    vector<vector<int>> S(E);
    for (int i = 0; i < E; i++) {
        cin >> G[i] >> C[i];
        G[i]--;
        S[i].resize(C[i]);
        for (auto&& s : S[i]) {
            cin >> s;
            s--;
        }
    }

    const int INF = 1e7;
    vector<int> cost(N, INF);
    vector<bool> seen(N, false);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> que;
    for (int i = 0; i < N; i++) {
        if (W[i]) {
            que.push({0, i});
            cost[i] = 1;
        }
    }
    while (!que.empty()) {
        auto [c, now] = que.top();
        que.pop();
        if (seen[now])
            continue;
        seen[now] = true;
        for (int i = 0; i < E; i++) {
            int g = G[i], c = C[i];
            auto& s = S[i];
            if (seen[g])
                continue;
            vector<int> V(c);
            for (int j = 0; j < c; j++) {
                V[j] = cost[s[j]];
            }
            sort(begin(V), end(V), greater{});
            int c_nxt = 0;
            for (int j = 0; j < c; j++) {
                c_nxt = max(c_nxt, V[j] + j);
            }
            if (cost[g] > c_nxt) {
                cost[g] = c_nxt;
                que.push({c_nxt, g});
            }
        }
    }
    cout << (cost[T] >= INF ? -1 : cost[T]) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3504
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
int main(){
  int N, E, T;
  cin >> N >> E >> T;
  T--;
  vector<int> W(N);
  for (int i = 0; i < N; i++){
    cin >> W[i];
  }
  vector<int> G(E), C(E);
  vector<vector<int>> S(E);
  for (int i = 0; i < E; i++){
    cin >> G[i] >> C[i];
    G[i]--;
    S[i] = vector<int>(C[i]);
    for (int j = 0; j < C[i]; j++){
      cin >> S[i][j];
      S[i][j]--;
    }
  }
  vector<int> d(N, INF);
  for (int i = 0; i < N; i++){
    if (W[i] == 1){
      d[i] = 1;
    }
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < E; j++){
      vector<int> cnt(C[j]);
      for (int k = 0; k < C[j]; k++){
        cnt[k] = d[S[j][k]];
      }
      sort(cnt.begin(), cnt.end(), greater<int>());
      int mx = 0;
      for (int k = 0; k < C[j]; k++){
        mx = max(mx, cnt[k] + k);
      }
      d[G[j]] = min(d[G[j]], mx);
    }
  }
  if (d[T] == INF){
    cout << -1 << endl;
  } else {
    cout << d[T] << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3172
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=305,INF=1<<30;

int dp[MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,M,X;cin>>N>>M>>X;
    X--;
    for(int i=0;i<N;i++){
        int x;cin>>x;
        dp[i]=INF;
        if(x==1) dp[i]=1;
    }
    
    vector<int> target(M);
    vector<vector<int>> need(M);
    
    for(int i=0;i<M;i++){
        cin>>target[i];
        target[i]--;
        
        int k;cin>>k;
        need[i].resize(k);
        for(int j=0;j<k;j++){
            cin>>need[i][j];
            need[i][j]--;
        }
    }
    
    for(int q=0;q<N;q++){
        for(int i=0;i<M;i++){
            vector<int> A;
            for(int a:need[i]) A.push_back(dp[a]);
            sort(all(A));
            reverse(all(A));
            if(A.front()==INF) continue;
            
            int ma=0;
            for(int j=0;j<si(A);j++){
                chmax(ma,A[j]+j);
            }
            chmin(dp[target[i]],ma);
        }
    }
    
    if(dp[X]==INF) dp[X]=-1;
    
    cout<<dp[X]<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3532
final_rank: 13

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}



//head



int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, e, t;
  cin >> n >> e >> t;
  vi dp(n+1);
  rep(i, n) {
    int w;
    cin >> w;
    if(w) dp[i+1] = 1;
    else dp[i+1] = INF;
  }
  vi g(e), c(e);
  vvi s(e);
  rep(i, e) {
    cin >> g[i] >> c[i];
    s[i].resize(c[i]);
    rep(j, c[i]) cin >> s[i][j];
  }
  PQ<int> q;
  rep(i, n-1) rep(j, e) {
    rep(k, c[j]) q.emplace(dp[s[j][k]]);
    int u = 0;
    int now = c[j];
    while(!q.empty()) {
      chmax(now, q.top()+(u++));
      q.pop();
    }
    chmin(dp[g[j]], now);
  }
  if(dp[t] == INF) puts("-1");
  else cout << dp[t] << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3492
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,r,a,b,d[350],e[1000],inf=1e8,cnt;
vector<int> v[1000],u;
int main(void){
    cin>>n>>m>>r;
    for(int i=0;i<n;i++){
        cin>>d[i];
        if(d[i]==0)d[i]=inf;
    }
    for(int i=0;i<m;i++){
        cin>>e[i]>>a;
        e[i]--;
        for(int j=0;j<a;j++){
            cin>>b;
            b--;
            v[i].push_back(b);
        }
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<m;j++){
            u={};
            for(int x:v[j]){
                u.push_back(d[x]);

            }
            sort(u.begin(),u.end());
            reverse(u.begin(),u.end());
            cnt=0;
            for(int k=0;k<u.size();k++){
                //cout<<u[k]<<endl;
                cnt=max(cnt,u[k]+k);
            }
            d[e[j]]=min(d[e[j]],cnt);
        }
    }
    r--;
    if(d[r]==inf)cout<<-1<<endl;
    else cout<<d[r]<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 3488
final_rank: 15

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i) os << ", ";
    os << v[i];
  }
  return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n, e, t; cin >> n >> e >> t;
  t--;
  vi w(n, INF);
  REP(i, n) {
    int now; cin >> now;
    if(now) w[i] = 1;
  }

  vector<vector<vector<int>>> g(n);

  REP(i, e) {
    int a; cin >> a;
    a--;
    int c; cin >> c;
    vi v;
    REP(j, c) {
      int s; cin >> s;
      s--;
      v.push_back(s);
    }
    g[a].emplace_back(v);
  }

  REP(i, n-1) {
    REP(j, n) {
      for(auto &v: g[j]) {
        int sz = SZ(v);
        vi v2(sz);
        bool ok = true;
        REP(k, sz) {
          if(v[k] == INF) {
            ok = false;
            break;
          }
          v2[k] = w[v[k]];
        }
        if(!ok) continue;
        sort(v2.rbegin(), v2.rend());
        int cost = 0;
        REP(k, sz) {
          chmax(cost, v2[k] + k);
        }
        chmin(w[j], cost);
      }
    }
  }

  cout << (w[t] == INF ? -1 : w[t]) << endl;

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3520
final_rank: 10

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}



int main(){
    int n,m,p;
    int i,j,k;
    int a,b,c;
    cin>>n>>m>>p;
    p--;
    vector<int> vs(n,INF),mak(m);
    vector<vector<int>> comp(m);
    for(i=0;i<n;i++){
        cin>>a;
        if(a)vs[i]=1;
    }
    for(i=0;i<m;i++){
        cin>>a>>j;
        mak[i]=a-1;
        while(j--){
            cin>>b;
            comp[i].push_back(b-1);
        }
    }
    vector<int> v1;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            v1.clear();
            for(auto num:comp[j])v1.push_back(vs[num]);
            sort(v1.rbegin(),v1.rend());
            a=vs[mak[j]],b=0;
            for(k=0;k<v1.size();k++){
                b=max(b,v1[k]+k);
            }
            vs[mak[j]]=min(a,b);
        }
    }
    a=vs[p];
    if(a>=INF)a=-1;
    cout<<a<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3248
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int INF = 1e+8;

signed main(){
	int n,e,t,w[310],g[1010],c[1010],s[1010][12];
	cin >> n >> e >> t; t--;
	int d[310];
	for(int i = 0;i < n;i++){
		cin >> w[i];
		d[i] = (w[i] ? 1 : INF);
	}
	for(int i = 0;i < e;i++){
		cin >> g[i] >> c[i]; g[i]--;
		for(int j = 0;j < c[i];j++){
			cin >> s[i][j]; s[i][j]--;
		}
	}
	for(int i = 0;i < n;i++){
		for(int j = 0;j < e;j++){
			int ma = 0;
			vector<int> vec;
			for(int k = 0;k < c[j];k++) vec.push_back(d[s[j][k]]);
			sort(vec.begin(),vec.end(),greater<int>());
			for(int k = 0;k < c[j];k++) ma = max(ma,vec[k] + k);
			d[g[j]] = min(d[g[j]],ma);
		}
	}
	if(d[t] != INF) cout << d[t] << endl;
	else cout << -1 << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3216
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int INF=1e8;
vector<int> cost;
struct synthesize{
  int goal;
  vector<int> start;
  synthesize(int g, vector<int> s):
    goal(g), start(s) {}
  void update(){
    int tot=0;
    for(int ii : start) tot=max(tot, cost[ii]);
    if(tot==INF) return;

    int c=start.size(), use=tot;
    priority_queue<int> pq;
    for(int ii : start) pq.push(cost[ii]);
    while(!pq.empty()){
      int v=pq.top(); pq.pop();
      if(use<v){
        tot++;
        use++;
      }
      use--;
    }
    cost[goal]=min(tot, cost[goal]);
  }
}; 

int main(){
	int n, e, t; cin >> n >> e >> t;
  cost.assign(n+1, INF);
  for(int i=1; i<=n; i++){
    int tmp; cin >> tmp;
    if(tmp==1) cost[i]=1;
  }
  vector<synthesize> vec;
  for(int i=0; i<e; i++){
    int g, c; cin >> g >> c;
    vector<int> s(c);
    for(int j=0; j<c; j++) cin >> s[j];
    vec.push_back(synthesize(g, s));
  }

  for(int i=0; i<n; i++){
    for(int j=0; j<e; j++){
      vec[j].update();
    }
  }

  cout << (cost[t] != INF ? cost[t] : -1) << endl; 
}
accuracy: 1
time_ms: 40
memory_kb: 3236
final_rank: 5

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int inf = 1012345678;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N, E, T;
	cin >> N >> E >> T; --T;
	vector<int> W(N);
	for (int i = 0; i < N; ++i) cin >> W[i];
	vector<int> G(E);
	vector<vector<int> > S(E);
	for (int i = 0; i < E; ++i) {
		int num;
		cin >> G[i] >> num; --G[i];
		S[i].resize(num);
		for (int j = 0; j < num; ++j) {
			cin >> S[i][j]; --S[i][j];
		}
	}
	vector<int> ans(N, inf);
	for (int i = 0; i < N; ++i) {
		if (W[i] == 1) ans[i] = 1;
	}
	while (true) {
		// at most N loops
		vector<int> nxt = ans;
		for (int i = 0; i < E; ++i) {
			vector<int> d;
			for (int j : S[i]) {
				d.push_back(ans[j]);
			}
			sort(d.begin(), d.end(), greater<int>());
			if (d[0] == inf) continue;
			int sub = 0;
			for (int j = 0; j < d.size(); ++j) {
				sub = max(sub, d[j] + j);
			}
			nxt[G[i]] = min(nxt[G[i]], sub);
		}
		if (ans == nxt) break;
		ans = nxt;
	}
	cout << (ans[T] != inf ? ans[T] : -1) << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3272
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N,E,T;
int W[300];
int G[1000],C[1000],S[1000][10];

int F[300];

signed main(){
    cin>>N>>E>>T;
    rep(i,N)cin>>W[i];
    rep(i,E){
        cin>>G[i]>>C[i];
        G[i]--;
        rep(j,C[i]){
            cin>>S[i][j];
            S[i][j]--;
        }
    }

    fill_n(F,N,1001001001);
    rep(i,N)if(W[i])F[i]=1;

    for(int i=0;i<N;i++){
        for(int j=0;j<E;j++){
            vint v;
            rep(k,C[j])v.pb(F[S[j][k]]);
            sort(all(v));
            reverse(all(v));
            int w=0;
            rep(k,C[j]){
                if(w-k<v[k]){
                    w=k+v[k];
                }
            }
            chmin(F[G[j]],w);
        }
    }
    T--;
    if(F[T]==1001001001)cout<<-1<<endl;
    else cout<<F[T]<<endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3240
final_rank: 6