結果

問題 No.1254 補強への架け橋
ユーザー kyo1kyo1
提出日時 2020-11-11 04:03:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 2,692 bytes
コンパイル時間 3,644 ms
コンパイル使用メモリ 231,308 KB
実行使用メモリ 54,384 KB
最終ジャッジ日時 2023-09-30 00:20:43
合計ジャッジ時間 23,198 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 3 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 3 ms
4,376 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 3 ms
4,380 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 3 ms
4,380 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 3 ms
4,376 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 19 ms
6,708 KB
testcase_64 AC 6 ms
4,464 KB
testcase_65 AC 12 ms
5,352 KB
testcase_66 AC 10 ms
5,132 KB
testcase_67 AC 5 ms
4,376 KB
testcase_68 AC 12 ms
5,192 KB
testcase_69 AC 13 ms
5,800 KB
testcase_70 AC 7 ms
4,488 KB
testcase_71 AC 6 ms
4,380 KB
testcase_72 AC 14 ms
5,736 KB
testcase_73 AC 7 ms
4,376 KB
testcase_74 AC 13 ms
5,560 KB
testcase_75 AC 10 ms
5,348 KB
testcase_76 AC 3 ms
4,376 KB
testcase_77 AC 9 ms
5,132 KB
testcase_78 AC 17 ms
6,116 KB
testcase_79 AC 17 ms
6,480 KB
testcase_80 AC 15 ms
5,836 KB
testcase_81 AC 17 ms
6,464 KB
testcase_82 AC 16 ms
6,252 KB
testcase_83 AC 279 ms
36,576 KB
testcase_84 AC 263 ms
35,776 KB
testcase_85 AC 145 ms
23,488 KB
testcase_86 AC 227 ms
31,580 KB
testcase_87 AC 248 ms
33,988 KB
testcase_88 AC 29 ms
8,096 KB
testcase_89 AC 276 ms
36,296 KB
testcase_90 AC 161 ms
24,632 KB
testcase_91 AC 119 ms
20,216 KB
testcase_92 AC 54 ms
12,256 KB
testcase_93 AC 207 ms
29,876 KB
testcase_94 AC 191 ms
27,532 KB
testcase_95 AC 182 ms
27,328 KB
testcase_96 AC 256 ms
34,812 KB
testcase_97 AC 97 ms
17,708 KB
testcase_98 AC 262 ms
34,972 KB
testcase_99 AC 142 ms
22,888 KB
testcase_100 AC 298 ms
37,608 KB
testcase_101 AC 47 ms
11,360 KB
testcase_102 AC 24 ms
7,512 KB
testcase_103 AC 52 ms
12,160 KB
testcase_104 AC 79 ms
15,476 KB
testcase_105 AC 211 ms
29,824 KB
testcase_106 AC 108 ms
19,172 KB
testcase_107 AC 281 ms
36,344 KB
testcase_108 AC 276 ms
36,080 KB
testcase_109 AC 206 ms
29,620 KB
testcase_110 AC 186 ms
27,280 KB
testcase_111 AC 198 ms
29,048 KB
testcase_112 AC 72 ms
14,792 KB
testcase_113 AC 181 ms
26,448 KB
testcase_114 AC 101 ms
18,412 KB
testcase_115 AC 31 ms
8,484 KB
testcase_116 AC 121 ms
21,132 KB
testcase_117 AC 72 ms
14,876 KB
testcase_118 AC 258 ms
34,456 KB
testcase_119 AC 142 ms
22,384 KB
testcase_120 AC 247 ms
33,576 KB
testcase_121 AC 59 ms
13,396 KB
testcase_122 AC 123 ms
20,560 KB
testcase_123 AC 1 ms
4,376 KB
testcase_124 AC 261 ms
54,384 KB
testcase_125 AC 255 ms
54,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename T>
class Edge {
 public:
  std::size_t from;
  std::size_t to;
  T cost;

  Edge(const std::size_t from, const std::size_t to, const T cost = 0)
      : from(from), to(to), cost(cost){};

  bool operator<(const Edge<T> &rhs) const { return cost < rhs.cost; }
  bool operator>(const Edge<T> &rhs) const { return cost > rhs.cost; }
};

template <typename T>
class Graph {
 private:
  std::vector<std::vector<Edge<T>>> graph;

 public:
  explicit Graph(const std::size_t n) : graph(n) {}

  std::vector<Edge<T>> operator[](const std::size_t i) const { return graph[i]; }
  std::vector<Edge<T>> &operator[](const std::size_t i) { return graph[i]; }

  std::size_t size() const { return graph.size(); }

  void add(const std::size_t u, const std::size_t v, const T c = 0) {
    graph[u].emplace_back(Edge(u, v, c));
  }
};

template <typename T>
class LowLink {
 public:
  const Graph<T> graph;
  std::vector<bool> used;
  std::vector<int> ord, low;
  std::vector<int> aps;
  std::vector<std::pair<int, int>> bridges;

  explicit LowLink(const Graph<T> &g)
      : graph(g), used(g.size(), false), ord(g.size(), 0), low(g.size(), 0) {
    int k = 0;
    for (int i = 0; i < (int)g.size(); i++) {
      if (!used[i]) k = dfs(i, k, -1);
    }
    sort(aps.begin(), aps.end());
    sort(bridges.begin(), bridges.end());
  }

  int dfs(int id, int k, int par) {
    used[id] = true;
    ord[id] = k++;
    low[id] = ord[id];
    bool is_aps = false;
    int count = 0;
    for (auto &e : graph[id]) {
      if (!used[e.to]) {
        count++;
        k = dfs(e.to, k, id);
        low[id] = min(low[id], low[e.to]);
        if (par != -1 && ord[id] <= low[e.to]) is_aps = true;
        if (ord[id] < low[e.to])
          bridges.emplace_back(make_pair(min(id, (int)e.to), max(id, (int)e.to)));
      } else if (e.to != par) {
        low[id] = min(low[id], ord[e.to]);
      }
    }
    if (par == -1 && count >= 2) is_aps = true;
    if (is_aps) aps.emplace_back(id);
    return k;
  };
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  Graph<int> G(N);
  map<pair<int, int>, int> mp;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G.add(a, b);
    G.add(b, a);
    mp[make_pair(a, b)] = i;
    mp[make_pair(b, a)] = i;
  }
  LowLink ll(G);
  set<int> st;
  for (auto b : ll.bridges) {
    st.insert(mp[b]);
  }
  cout << N - st.size() << '\n';
  vector<int> res;
  for (int i = 0; i < N; i++) {
    if (st.find(i) == st.end()) res.emplace_back(i);
  }
  for (const auto &e : res) {
    cout << e + 1 << (&e == &res.back() ? '\n' : ' ');
  }
  return 0;
}
0