結果

問題 No.1254 補強への架け橋
ユーザー KoDKoD
提出日時 2020-10-09 21:31:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 6,271 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 104,196 KB
実行使用メモリ 26,436 KB
最終ジャッジ日時 2024-07-20 08:52:13
合計ジャッジ時間 12,139 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 3 ms
5,376 KB
testcase_44 AC 3 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 3 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 1 ms
5,376 KB
testcase_50 AC 3 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 3 ms
5,376 KB
testcase_53 AC 3 ms
5,376 KB
testcase_54 AC 3 ms
5,376 KB
testcase_55 AC 3 ms
5,376 KB
testcase_56 AC 2 ms
5,376 KB
testcase_57 AC 2 ms
5,376 KB
testcase_58 AC 2 ms
5,376 KB
testcase_59 AC 2 ms
5,376 KB
testcase_60 AC 2 ms
5,376 KB
testcase_61 AC 3 ms
5,376 KB
testcase_62 AC 2 ms
5,376 KB
testcase_63 AC 11 ms
5,376 KB
testcase_64 AC 4 ms
5,376 KB
testcase_65 AC 7 ms
5,376 KB
testcase_66 AC 6 ms
5,376 KB
testcase_67 AC 3 ms
5,376 KB
testcase_68 AC 8 ms
5,376 KB
testcase_69 AC 9 ms
5,376 KB
testcase_70 AC 5 ms
5,376 KB
testcase_71 AC 5 ms
5,376 KB
testcase_72 AC 9 ms
5,376 KB
testcase_73 AC 5 ms
5,376 KB
testcase_74 AC 8 ms
5,376 KB
testcase_75 AC 7 ms
5,376 KB
testcase_76 AC 2 ms
5,376 KB
testcase_77 AC 7 ms
5,376 KB
testcase_78 AC 11 ms
5,376 KB
testcase_79 AC 10 ms
5,376 KB
testcase_80 AC 10 ms
5,376 KB
testcase_81 AC 10 ms
5,376 KB
testcase_82 AC 10 ms
5,376 KB
testcase_83 AC 112 ms
19,200 KB
testcase_84 AC 119 ms
18,688 KB
testcase_85 AC 65 ms
12,928 KB
testcase_86 AC 103 ms
16,640 KB
testcase_87 AC 117 ms
18,048 KB
testcase_88 AC 16 ms
5,760 KB
testcase_89 AC 114 ms
19,072 KB
testcase_90 AC 71 ms
13,440 KB
testcase_91 AC 55 ms
11,392 KB
testcase_92 AC 30 ms
7,552 KB
testcase_93 AC 94 ms
16,000 KB
testcase_94 AC 81 ms
14,848 KB
testcase_95 AC 79 ms
14,720 KB
testcase_96 AC 108 ms
18,304 KB
testcase_97 AC 47 ms
10,112 KB
testcase_98 AC 116 ms
18,432 KB
testcase_99 AC 62 ms
12,544 KB
testcase_100 AC 122 ms
19,456 KB
testcase_101 AC 26 ms
7,168 KB
testcase_102 AC 14 ms
5,376 KB
testcase_103 AC 30 ms
7,552 KB
testcase_104 AC 38 ms
9,088 KB
testcase_105 AC 91 ms
16,000 KB
testcase_106 AC 50 ms
10,880 KB
testcase_107 AC 116 ms
19,072 KB
testcase_108 AC 113 ms
18,816 KB
testcase_109 AC 85 ms
15,872 KB
testcase_110 AC 83 ms
14,592 KB
testcase_111 AC 90 ms
15,488 KB
testcase_112 AC 35 ms
8,832 KB
testcase_113 AC 82 ms
14,336 KB
testcase_114 AC 47 ms
10,624 KB
testcase_115 AC 18 ms
5,888 KB
testcase_116 AC 59 ms
11,776 KB
testcase_117 AC 43 ms
8,832 KB
testcase_118 AC 105 ms
18,176 KB
testcase_119 AC 62 ms
12,416 KB
testcase_120 AC 102 ms
17,664 KB
testcase_121 AC 30 ms
7,936 KB
testcase_122 AC 55 ms
11,520 KB
testcase_123 AC 2 ms
5,376 KB
testcase_124 AC 174 ms
24,460 KB
testcase_125 AC 167 ms
26,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"

/**
 * @title Template
 */

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>
#include <map>

#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/chmin_chmax.cpp"

template <class T, class U>
constexpr bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
constexpr bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

/**
 * @title Chmin/Chmax
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"

class range {
public:
  class iterator {
  private:
    int64_t M_position;

  public:
    constexpr iterator(int64_t position) noexcept: M_position(position) { }
    constexpr void operator ++ () noexcept { ++M_position; }
    constexpr bool operator != (iterator other) const noexcept { return M_position != other.M_position; }
    constexpr int64_t operator * () const noexcept { return M_position; }
  };

  class reverse_iterator {
  private:
    int64_t M_position;
  
  public:
    constexpr reverse_iterator(int64_t position) noexcept: M_position(position) { }
    constexpr void operator ++ () noexcept { --M_position; }
    constexpr bool operator != (reverse_iterator other) const noexcept { return M_position != other.M_position; }
    constexpr int64_t operator * () const noexcept { return M_position; }
  };
  
private:
  const iterator M_first, M_last;

public:
  constexpr range(int64_t first, int64_t last) noexcept: M_first(first), M_last(std::max(first, last)) { }
  constexpr iterator begin() const noexcept { return M_first; }
  constexpr iterator end() const noexcept { return M_last; }
  constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(*M_last - 1); } 
  constexpr reverse_iterator rend() const noexcept { return reverse_iterator(*M_first - 1); } 
};

/**
 * @title Range
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/fix_point.cpp"

#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/fix_point.cpp"

template <class Func>
struct fix_point_impl: private Func {
  explicit constexpr fix_point_impl(Func &&func): Func(std::forward<Func>(func)) { }
  template <class... Args>
  constexpr decltype(auto) operator () (Args &&... args) const {
    return Func::operator()(*this, std::forward<Args>(args)...);
  }
};

template <class Func>
constexpr decltype(auto) fix_point(Func &&func) {
  return fix_point_impl<Func>(std::forward<Func>(func));
}

/**
 * @title Lambda Recursion
 */
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/graph/union_find.cpp"

#include <cstddef>
#line 7 "/Users/kodamankod/Desktop/cpp_programming/Library/graph/union_find.cpp"

class union_find {
public:
  using size_type = size_t;

private:
  class node_type {
  public:
    size_type parent, size;
    node_type(size_type parent): 
      parent(parent), size(1) 
    { }
  };

  size_type M_components;
  std::vector<node_type> M_forest;

public:
  union_find() = default;
  explicit union_find(const size_type size) { initialize(size); }

  void initialize(const size_type size) {
    clear();
    M_components = size;
    M_forest.reserve(size);
    for (size_type index = 0; index < size; ++index) {
      M_forest.emplace_back(index);
    }
  }

  size_type find_parent(const size_type node) {
    assert(node < size());
    size_type &parent = M_forest[node].parent;
    if (node == parent) return node;
    return parent = find_parent(parent);
  }
  size_type count_components() const { 
    return M_components; 
  }
  size_type component_size(const size_type node) { 
    assert(node < size());
    return M_forest[find_parent(node)].size;
  }

  bool unite(size_type node1, size_type node2) {
    assert(node1 < size());
    assert(node2 < size());
    node1 = find_parent(node1);
    node2 = find_parent(node2);
    if (node1 == node2) return false;
    if (M_forest[node1].size < M_forest[node2].size) {
      std::swap(node1, node2);
    }
    M_forest[node1].size += M_forest[node2].size;
    M_forest[node2].parent = node1;
    --M_components;
    return true;
  }
  bool same_component(const size_type node1, const size_type node2) { 
    assert(node1 < size());
    assert(node2 < size());
    return find_parent(node1) == find_parent(node2); 
  }

  size_type size() const {
    return M_forest.size();
  }
  bool empty() const {
    return M_forest.empty();
  }
  void clear() {
    M_components = 0;
    M_forest.clear();
    M_forest.shrink_to_fit();
  }
};

/**
 * @title Disjoint Set Union
 */
#line 19 "main.cpp"

using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  usize N;
  std::cin >> N;
  std::vector<usize> A(N), B(N);
  std::map<std::pair<usize, usize>, usize> Id;
  for (auto i: range(0, N)) {
    std::cin >> A[i] >> B[i];
    --A[i]; --B[i];
    Id[std::minmax(A[i], B[i])] = i;
  }
  std::vector<std::vector<usize>> graph(N);
  union_find dsu(N);
  usize st, en;
  for (auto i: range(0, N)) {
    if (dsu.unite(A[i], B[i])) {
      graph[A[i]].push_back(B[i]);
      graph[B[i]].push_back(A[i]);
    }
    else {
      st = A[i];
      en = B[i];
    }
  }
  std::vector<usize> parent(N), depth(N);
  fix_point([&](auto dfs, usize u, usize p, usize d) -> void {
    parent[u] = p;
    depth[u] = d;
    for (auto v: graph[u]) {
      if (v != p) {
        dfs(v, u, d + 1);
      }
    }
  })(0, 0, 0);
  std::vector<usize> ans;
  ans.push_back(Id[std::minmax(st, en)]);
  while (st != en) {
    if (depth[st] > depth[en]) {
      ans.push_back(Id[std::minmax(st, parent[st])]);
      st = parent[st];
    }
    else {
      ans.push_back(Id[std::minmax(en, parent[en])]);
      en = parent[en];
    }
  }
  std::cout << ans.size() << '\n';
  std::sort(ans.begin(), ans.end());
  for (auto i: range(0, ans.size())) {
    std::cout << ans[i] + 1 << (i + 1 == (isize) ans.size() ? '\n' : ' '); 
  }
  return 0;
}
0