結果

問題 No.1254 補強への架け橋
ユーザー simansiman
提出日時 2024-01-08 20:16:20
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 2,083 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 124,356 KB
実行使用メモリ 26,424 KB
最終ジャッジ日時 2024-01-08 20:16:36
合計ジャッジ時間 15,485 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
testcase_36 AC 1 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 2 ms
6,676 KB
testcase_41 AC 2 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
testcase_43 AC 2 ms
6,676 KB
testcase_44 AC 2 ms
6,676 KB
testcase_45 AC 2 ms
6,676 KB
testcase_46 AC 2 ms
6,676 KB
testcase_47 AC 2 ms
6,676 KB
testcase_48 AC 2 ms
6,676 KB
testcase_49 AC 2 ms
6,676 KB
testcase_50 AC 2 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 2 ms
6,676 KB
testcase_53 AC 2 ms
6,676 KB
testcase_54 AC 2 ms
6,676 KB
testcase_55 AC 2 ms
6,676 KB
testcase_56 AC 2 ms
6,676 KB
testcase_57 AC 2 ms
6,676 KB
testcase_58 AC 2 ms
6,676 KB
testcase_59 AC 2 ms
6,676 KB
testcase_60 AC 2 ms
6,676 KB
testcase_61 AC 2 ms
6,676 KB
testcase_62 AC 2 ms
6,676 KB
testcase_63 AC 10 ms
6,676 KB
testcase_64 AC 3 ms
6,676 KB
testcase_65 AC 6 ms
6,676 KB
testcase_66 AC 6 ms
6,676 KB
testcase_67 AC 3 ms
6,676 KB
testcase_68 AC 6 ms
6,676 KB
testcase_69 AC 8 ms
6,676 KB
testcase_70 AC 4 ms
6,676 KB
testcase_71 AC 4 ms
6,676 KB
testcase_72 AC 8 ms
6,676 KB
testcase_73 AC 4 ms
6,676 KB
testcase_74 AC 8 ms
6,676 KB
testcase_75 AC 6 ms
6,676 KB
testcase_76 AC 2 ms
6,676 KB
testcase_77 AC 5 ms
6,676 KB
testcase_78 AC 9 ms
6,676 KB
testcase_79 AC 9 ms
6,676 KB
testcase_80 AC 8 ms
6,676 KB
testcase_81 AC 10 ms
6,676 KB
testcase_82 AC 8 ms
6,676 KB
testcase_83 AC 98 ms
17,136 KB
testcase_84 AC 114 ms
16,792 KB
testcase_85 AC 57 ms
11,564 KB
testcase_86 AC 84 ms
15,228 KB
testcase_87 AC 89 ms
16,124 KB
testcase_88 AC 13 ms
6,676 KB
testcase_89 AC 98 ms
17,112 KB
testcase_90 AC 70 ms
12,020 KB
testcase_91 AC 47 ms
10,368 KB
testcase_92 AC 25 ms
7,000 KB
testcase_93 AC 80 ms
14,568 KB
testcase_94 AC 71 ms
13,680 KB
testcase_95 AC 72 ms
13,540 KB
testcase_96 AC 93 ms
16,444 KB
testcase_97 AC 39 ms
9,360 KB
testcase_98 AC 112 ms
16,604 KB
testcase_99 AC 57 ms
11,364 KB
testcase_100 AC 108 ms
17,452 KB
testcase_101 AC 22 ms
6,676 KB
testcase_102 AC 12 ms
6,676 KB
testcase_103 AC 24 ms
6,992 KB
testcase_104 AC 34 ms
8,500 KB
testcase_105 AC 80 ms
14,572 KB
testcase_106 AC 44 ms
9,876 KB
testcase_107 AC 103 ms
17,116 KB
testcase_108 AC 99 ms
16,940 KB
testcase_109 AC 78 ms
14,408 KB
testcase_110 AC 72 ms
13,520 KB
testcase_111 AC 75 ms
14,200 KB
testcase_112 AC 32 ms
7,936 KB
testcase_113 AC 68 ms
12,712 KB
testcase_114 AC 43 ms
9,700 KB
testcase_115 AC 15 ms
6,676 KB
testcase_116 AC 48 ms
10,692 KB
testcase_117 AC 32 ms
8,308 KB
testcase_118 AC 94 ms
16,420 KB
testcase_119 AC 54 ms
11,208 KB
testcase_120 AC 88 ms
15,940 KB
testcase_121 AC 27 ms
7,296 KB
testcase_122 AC 49 ms
10,532 KB
testcase_123 AC 2 ms
6,676 KB
testcase_124 AC 132 ms
26,424 KB
testcase_125 AC 129 ms
26,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

class LowLink {
public:
  std::vector<int> ord;
  std::vector<int> low;
  std::vector <std::pair<int, int>> bridge;
  std::vector<int> joint_list;
  std::vector<bool> joint_or_not;
  std::vector <std::vector<int>> G;

  LowLink(const std::vector <std::vector<int>> &G) {
    ord.resize(G.size(), -1);
    low.resize(G.size(), -1);
    this->G = G;

    k = 0;
    joint_or_not.resize(G.size(), false);
    for (int v = 0; v < (int) G.size(); ++v) {
      if (ord[v] == -1) {
        dfs(v);
      }
    }
  }

  bool is_bridge(int u, int v) {
    if (ord[u] > ord[v]) {
      std::swap(u, v);
    }
    return ord[u] < low[v];
  }

  bool is_joint(int v) {
    return std::find(joint_list.begin(), joint_list.end(), v) != joint_list.end();
  }

private:
  int k;

  void dfs(int v, int parent = -1) {
    ord[v] = k++;
    low[v] = ord[v];
    bool is_joint = false;
    int cnt = 0;

    for (int u : G[v]) {
      if (u == parent) {
        continue;
      }
      if (ord[u] == -1) {
        ++cnt;
        dfs(u, v);
        low[v] = std::min(low[v], low[u]);
        if (parent != -1 && ord[v] <= low[u]) {
          is_joint = true;
        }
        if (ord[v] < low[u]) {
          bridge.emplace_back(std::minmax(u, v));
        }
      } else {
        low[v] = std::min(low[v], ord[u]);
      }
    }
    if (parent == -1 && cnt > 1) {
      is_joint = true;
    }
    joint_or_not[v] = is_joint;
    if (is_joint) {
      joint_list.push_back(v);
    }
  }
};

int main() {
  int N;
  cin >> N;
  vector <vector<int>> G(N + 1);
  vector<tuple<int, int, int>> edges;
  for (int i = 1; i <= N; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
    edges.emplace_back(a, b, i);
  }

  LowLink lowlink(G);
  vector<int> ans;

  for (auto e : edges) {
    if (not lowlink.is_bridge(get<0>(e), get<1>(e))) {
      ans.push_back(get<2>(e));
    }
  }

  cout << ans.size() << endl;
  for (int a : ans) {
    cout << a << " ";
  }
  cout << endl;

  return 0;
}
0