結果

問題 No.1868 Teleporting Cyanmond
ユーザー SSRSSSRS
提出日時 2022-03-11 21:22:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 881 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 207,872 KB
実行使用メモリ 9,652 KB
最終ジャッジ日時 2023-10-14 06:00:30
合計ジャッジ時間 4,029 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 35 ms
8,804 KB
testcase_04 AC 26 ms
6,948 KB
testcase_05 AC 3 ms
4,352 KB
testcase_06 AC 7 ms
4,352 KB
testcase_07 AC 18 ms
6,120 KB
testcase_08 AC 12 ms
4,900 KB
testcase_09 AC 16 ms
5,656 KB
testcase_10 AC 32 ms
8,412 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 10 ms
4,404 KB
testcase_13 AC 19 ms
5,888 KB
testcase_14 AC 37 ms
8,912 KB
testcase_15 AC 25 ms
6,824 KB
testcase_16 AC 6 ms
4,352 KB
testcase_17 AC 9 ms
4,348 KB
testcase_18 AC 41 ms
9,408 KB
testcase_19 AC 41 ms
9,408 KB
testcase_20 AC 41 ms
9,320 KB
testcase_21 AC 41 ms
9,436 KB
testcase_22 AC 41 ms
9,392 KB
testcase_23 AC 41 ms
9,416 KB
testcase_24 AC 41 ms
9,652 KB
testcase_25 AC 42 ms
9,316 KB
testcase_26 AC 41 ms
9,368 KB
testcase_27 AC 42 ms
9,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<int> R(N - 1);
  for (int i = 0; i < N - 1; i++){
    cin >> R[i];
    R[i]--;
  }
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++){
    E[i + 1].push_back(make_pair(0, i));
  }
  for (int i = 0; i < N - 1; i++){
    E[i].push_back(make_pair(1, R[i]));
  }
  vector<int> d(N, -1);
  deque<pair<int, int>> dq;
  dq.push_back(make_pair(0, 0));
  while (!dq.empty()){
    int c = dq.front().first;
    int v = dq.front().second;
    dq.pop_front();
    if (d[v] == -1){
      d[v] = c;
      for (auto P : E[v]){
        int w = P.second;
        if (d[w] == -1){
          if (P.first == 0){
            dq.push_front(make_pair(c, w));
          } else {
            dq.push_back(make_pair(c + 1, w));
          }
        }
      }
    }
  }
  cout << d[N - 1] << endl;
}
0