Submission #70542863


Source Code Expand

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

// BFS(2つめのスタート地点まで記録するように改造した版)
void bfs(const vector<vector<int>>& graph, vector<int>& distances1, vector<int>& distances2, const vector<int>& start) {

  // BFS用のqueue(のつもりのdeque)
  // 「頂点、距離、スタート」の候補
  deque<tuple<int,int,int>> que;

  // スタート地点候補(安全地帯)を全部距離0で候補に入れる
  for (int v : start) {
    que.emplace_back(v,0,v);
  }

  // 各頂点から最寄りのスタート地点
  vector<int> roots(graph.size(),-1);

  // BFSのループを実行
  while (!que.empty()) {

    // 先頭を取り出す
    auto [v,d,r] = que.front();
    que.pop_front();

    // 最寄りのスタート地点と同じ情報だったら破棄
    if (roots.at(v)==r) continue;

    // 最寄り情報がなかったら、最寄り情報に入れる
    if (distances1.at(v)==-1) {
      distances1.at(v) = d;
      roots.at(v) = r;
    }

    // 最寄り情報があるが次点情報がなかったら、次点情報に入れる
    else if (distances2.at(v)==-1) {
      distances2.at(v) = d;
    }

    // どちらでもなければ、破棄
    else continue;

    // 有効な情報だったら、隣接頂点をqueueに入れる
    // (この時点で次点情報をチェックしてもいいが、
    //   しなくても間に合うし、コードがややこしくなるので、やらないことにする)
    for (int next : graph.at(v)) {
      que.emplace_back(next,d+1,r);
    }

  }
}

/////////////////// メイン ///////////////////

int main () {
  
  //////////////////// 入力 ////////////////////

  int n, m;
  cin >> n >> m;

  // 0始まりに直して、隣接リストに受け取る
  vector<int> u(m), v(m);
  vector<vector<int>> graph(n);
  for (int i=0; i<m; i++) {
    cin >> u.at(i) >> v.at(i);
    u.at(i)--;
    v.at(i)--;
    graph.at(u.at(i)).emplace_back(v.at(i));
    graph.at(v.at(i)).emplace_back(u.at(i));
  }

  string s;
  cin >> s;

  //////////////// 出力変数定義 ////////////////

  vector<int> result;
  result.reserve(n);

  //////////////////// 処理 ////////////////////

  // 最寄りと次点までの距離情報
  vector<int> distances1(n,-1);
  vector<int> distances2(n,-1);

  // スタート地点(安全地帯)情報
  vector<int> start;
  for (int i=0; i<n; i++) {
    if (s.at(i)=='S') start.emplace_back(i);
  }

  // BFSをする
  bfs(graph,distances1,distances2,start);

  // 危険地帯について、順番に、出力する値を求めていく
  for (int i=0; i<n; i++) {
    if (s.at(i)=='D') result.emplace_back(distances1.at(i)+distances2.at(i));
  }

  //////////////////// 出力 ////////////////////

  for (size_t i=0; i<result.size(); i++) {
    cout << result.at(i) << endl;
  }

  //////////////////// 終了 ////////////////////

  return 0;

}

Submission Info

Submission Time
Task E - Hit and Away
User wightou
Language C++ 23 (gcc 12.2)
Score 450
Code Size 3041 Byte
Status AC
Exec Time 319 ms
Memory 25556 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 2
AC × 54
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3476 KiB
example_01.txt AC 1 ms 3448 KiB
hand_00.txt AC 309 ms 18392 KiB
hand_01.txt AC 218 ms 21612 KiB
hand_02.txt AC 128 ms 25064 KiB
hand_03.txt AC 203 ms 25556 KiB
hand_04.txt AC 293 ms 23976 KiB
hand_05.txt AC 61 ms 17008 KiB
hand_06.txt AC 63 ms 17064 KiB
hand_07.txt AC 307 ms 18412 KiB
hand_08.txt AC 185 ms 18368 KiB
hand_09.txt AC 60 ms 13916 KiB
hand_10.txt AC 302 ms 18408 KiB
hand_11.txt AC 210 ms 21104 KiB
hand_12.txt AC 208 ms 21108 KiB
hand_13.txt AC 115 ms 24000 KiB
hand_14.txt AC 188 ms 25428 KiB
hand_15.txt AC 209 ms 25472 KiB
random_00.txt AC 319 ms 19604 KiB
random_01.txt AC 297 ms 19676 KiB
random_02.txt AC 290 ms 19704 KiB
random_03.txt AC 310 ms 19656 KiB
random_04.txt AC 297 ms 19648 KiB
random_05.txt AC 287 ms 19700 KiB
random_06.txt AC 307 ms 18396 KiB
random_07.txt AC 294 ms 18672 KiB
random_08.txt AC 279 ms 19264 KiB
random_09.txt AC 309 ms 18352 KiB
random_10.txt AC 296 ms 18516 KiB
random_11.txt AC 275 ms 19664 KiB
random_12.txt AC 307 ms 18340 KiB
random_13.txt AC 295 ms 18612 KiB
random_14.txt AC 277 ms 19384 KiB
random_15.txt AC 313 ms 18384 KiB
random_16.txt AC 300 ms 18628 KiB
random_17.txt AC 298 ms 18960 KiB
random_18.txt AC 35 ms 10520 KiB
random_19.txt AC 41 ms 12388 KiB
random_20.txt AC 51 ms 14364 KiB
random_21.txt AC 50 ms 14372 KiB
random_22.txt AC 47 ms 13232 KiB
random_23.txt AC 37 ms 11056 KiB
random_24.txt AC 99 ms 16288 KiB
random_25.txt AC 83 ms 17196 KiB
random_26.txt AC 95 ms 16736 KiB
random_27.txt AC 80 ms 17188 KiB
random_28.txt AC 107 ms 16660 KiB
random_29.txt AC 89 ms 16736 KiB
random_30.txt AC 298 ms 19388 KiB
random_31.txt AC 110 ms 20680 KiB
random_32.txt AC 220 ms 17892 KiB
random_33.txt AC 145 ms 24636 KiB
random_34.txt AC 213 ms 17232 KiB
random_35.txt AC 170 ms 19632 KiB