結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 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 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 AC 1 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 1 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 1 ms
6,676 KB
testcase_33 AC 1 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 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 1 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 1 ms
6,676 KB
testcase_60 AC 2 ms
6,676 KB
testcase_61 AC 2 ms
6,676 KB
testcase_62 AC 1 ms
6,676 KB
testcase_63 AC 6 ms
6,676 KB
testcase_64 AC 3 ms
6,676 KB
testcase_65 AC 4 ms
6,676 KB
testcase_66 AC 4 ms
6,676 KB
testcase_67 AC 2 ms
6,676 KB
testcase_68 AC 4 ms
6,676 KB
testcase_69 AC 5 ms
6,676 KB
testcase_70 AC 3 ms
6,676 KB
testcase_71 AC 3 ms
6,676 KB
testcase_72 AC 5 ms
6,676 KB
testcase_73 AC 3 ms
6,676 KB
testcase_74 AC 7 ms
6,676 KB
testcase_75 AC 4 ms
6,676 KB
testcase_76 AC 2 ms
6,676 KB
testcase_77 AC 3 ms
6,676 KB
testcase_78 AC 6 ms
6,676 KB
testcase_79 AC 6 ms
6,676 KB
testcase_80 AC 5 ms
6,676 KB
testcase_81 AC 6 ms
6,676 KB
testcase_82 AC 6 ms
6,676 KB
testcase_83 AC 58 ms
17,032 KB
testcase_84 AC 55 ms
16,688 KB
testcase_85 AC 32 ms
11,460 KB
testcase_86 AC 47 ms
15,124 KB
testcase_87 AC 57 ms
16,020 KB
testcase_88 AC 9 ms
6,676 KB
testcase_89 AC 58 ms
17,008 KB
testcase_90 AC 35 ms
11,916 KB
testcase_91 AC 27 ms
10,264 KB
testcase_92 AC 15 ms
6,896 KB
testcase_93 AC 44 ms
14,464 KB
testcase_94 AC 40 ms
13,576 KB
testcase_95 AC 40 ms
13,436 KB
testcase_96 AC 51 ms
16,340 KB
testcase_97 AC 24 ms
9,256 KB
testcase_98 AC 53 ms
16,500 KB
testcase_99 AC 32 ms
11,260 KB
testcase_100 AC 77 ms
17,348 KB
testcase_101 AC 13 ms
6,676 KB
testcase_102 AC 8 ms
6,676 KB
testcase_103 AC 15 ms
6,888 KB
testcase_104 AC 20 ms
8,396 KB
testcase_105 AC 46 ms
14,468 KB
testcase_106 AC 27 ms
9,772 KB
testcase_107 AC 59 ms
17,012 KB
testcase_108 AC 56 ms
16,836 KB
testcase_109 AC 46 ms
14,304 KB
testcase_110 AC 41 ms
13,416 KB
testcase_111 AC 45 ms
14,096 KB
testcase_112 AC 20 ms
7,888 KB
testcase_113 AC 41 ms
12,608 KB
testcase_114 AC 27 ms
9,596 KB
testcase_115 AC 10 ms
6,676 KB
testcase_116 AC 30 ms
10,588 KB
testcase_117 AC 21 ms
8,204 KB
testcase_118 AC 57 ms
16,316 KB
testcase_119 AC 33 ms
11,104 KB
testcase_120 AC 52 ms
15,836 KB
testcase_121 AC 16 ms
7,236 KB
testcase_122 AC 28 ms
10,428 KB
testcase_123 AC 1 ms
6,676 KB
testcase_124 AC 80 ms
26,320 KB
testcase_125 AC 80 ms
26,316 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() {
  std::cin.tie(0)->sync_with_stdio(0);

  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