Advertisement
999ms

Untitled

Jan 24th, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) (x).begin(), (x).end()
  3.  
  4. using namespace std;
  5. using ll = long long;
  6. vector<vector<int>> g;
  7. vector<bool> used;
  8.  
  9. void Dfs(int from) {
  10.     used[from] = true;
  11.     for (int to : g[from]) {
  12.         if (!used[to]) {
  13.             Dfs(to);
  14.         }
  15.     }
  16. }
  17.  
  18. int main() {
  19.     ios_base::sync_with_stdio(false);
  20.     cin.tie(nullptr);
  21.     cout.tie(nullptr);
  22.     int n, t;
  23.     cin >> n >> t;
  24.     g.resize(n);
  25.     used.assign(n, false);
  26.     for (int i = 0; i < n - 1; i++) {
  27.         int from = i;
  28.         int a;
  29.         cin >> a;
  30.         int to = i + a;
  31.         g[from].push_back(to);
  32.     }
  33.     Dfs(0);
  34.     if (used[t - 1] == true) {
  35.         cout << "YES\n";
  36.     } else {
  37.         cout << "NO\n";
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement