結果

問題 No.1254 補強への架け橋
ユーザー KoDKoD
提出日時 2020-10-09 21:31:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 6,271 bytes
コンパイル時間 1,141 ms
コンパイル使用メモリ 103,860 KB
実行使用メモリ 26,448 KB
最終ジャッジ日時 2023-09-27 14:48:16
合計ジャッジ時間 13,067 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 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,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 3 ms
4,376 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 2 ms
4,376 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 1 ms
4,376 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 12 ms
4,748 KB
testcase_64 AC 3 ms
4,376 KB
testcase_65 AC 8 ms
4,380 KB
testcase_66 AC 6 ms
4,380 KB
testcase_67 AC 4 ms
4,380 KB
testcase_68 AC 7 ms
4,380 KB
testcase_69 AC 9 ms
4,392 KB
testcase_70 AC 4 ms
4,376 KB
testcase_71 AC 3 ms
4,376 KB
testcase_72 AC 8 ms
4,444 KB
testcase_73 AC 4 ms
4,376 KB
testcase_74 AC 9 ms
4,540 KB
testcase_75 AC 7 ms
4,380 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 6 ms
4,376 KB
testcase_78 AC 10 ms
4,628 KB
testcase_79 AC 10 ms
4,748 KB
testcase_80 AC 8 ms
4,512 KB
testcase_81 AC 10 ms
4,616 KB
testcase_82 AC 10 ms
4,588 KB
testcase_83 AC 115 ms
18,940 KB
testcase_84 AC 107 ms
18,632 KB
testcase_85 AC 68 ms
12,884 KB
testcase_86 AC 94 ms
16,628 KB
testcase_87 AC 107 ms
17,888 KB
testcase_88 AC 15 ms
5,392 KB
testcase_89 AC 122 ms
19,004 KB
testcase_90 AC 67 ms
13,448 KB
testcase_91 AC 52 ms
11,300 KB
testcase_92 AC 27 ms
7,268 KB
testcase_93 AC 84 ms
15,704 KB
testcase_94 AC 79 ms
14,724 KB
testcase_95 AC 77 ms
14,604 KB
testcase_96 AC 105 ms
18,036 KB
testcase_97 AC 45 ms
9,912 KB
testcase_98 AC 108 ms
18,316 KB
testcase_99 AC 64 ms
12,324 KB
testcase_100 AC 114 ms
19,360 KB
testcase_101 AC 24 ms
7,060 KB
testcase_102 AC 13 ms
5,108 KB
testcase_103 AC 26 ms
7,228 KB
testcase_104 AC 37 ms
8,924 KB
testcase_105 AC 88 ms
15,756 KB
testcase_106 AC 51 ms
10,768 KB
testcase_107 AC 112 ms
18,948 KB
testcase_108 AC 110 ms
18,896 KB
testcase_109 AC 85 ms
15,780 KB
testcase_110 AC 78 ms
14,508 KB
testcase_111 AC 82 ms
15,252 KB
testcase_112 AC 38 ms
8,532 KB
testcase_113 AC 76 ms
14,072 KB
testcase_114 AC 48 ms
10,444 KB
testcase_115 AC 17 ms
5,732 KB
testcase_116 AC 56 ms
11,504 KB
testcase_117 AC 36 ms
8,596 KB
testcase_118 AC 103 ms
18,040 KB
testcase_119 AC 61 ms
12,420 KB
testcase_120 AC 101 ms
17,620 KB
testcase_121 AC 30 ms
7,864 KB
testcase_122 AC 59 ms
11,172 KB
testcase_123 AC 2 ms
4,376 KB
testcase_124 AC 169 ms
24,232 KB
testcase_125 AC 170 ms
26,448 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