結果

問題 No.1023 Cyclic Tour
ユーザー noshi91noshi91
提出日時 2020-04-10 22:58:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 324 ms / 2,000 ms
コード長 7,078 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 95,320 KB
実行使用メモリ 31,104 KB
最終ジャッジ日時 2023-10-14 04:01:40
合計ジャッジ時間 15,373 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 106 ms
9,796 KB
testcase_05 AC 105 ms
9,664 KB
testcase_06 AC 110 ms
10,140 KB
testcase_07 AC 106 ms
9,804 KB
testcase_08 AC 94 ms
9,984 KB
testcase_09 AC 157 ms
22,396 KB
testcase_10 AC 155 ms
22,376 KB
testcase_11 AC 166 ms
23,584 KB
testcase_12 AC 151 ms
23,992 KB
testcase_13 AC 141 ms
22,848 KB
testcase_14 AC 143 ms
22,684 KB
testcase_15 AC 147 ms
23,028 KB
testcase_16 AC 263 ms
25,012 KB
testcase_17 AC 262 ms
25,324 KB
testcase_18 AC 269 ms
23,444 KB
testcase_19 AC 263 ms
23,312 KB
testcase_20 AC 320 ms
23,592 KB
testcase_21 AC 314 ms
24,024 KB
testcase_22 AC 297 ms
25,132 KB
testcase_23 AC 311 ms
25,316 KB
testcase_24 AC 288 ms
26,780 KB
testcase_25 AC 324 ms
23,732 KB
testcase_26 AC 304 ms
24,360 KB
testcase_27 AC 278 ms
23,612 KB
testcase_28 AC 277 ms
25,868 KB
testcase_29 AC 258 ms
25,936 KB
testcase_30 AC 290 ms
27,016 KB
testcase_31 AC 275 ms
26,028 KB
testcase_32 AC 291 ms
25,660 KB
testcase_33 AC 288 ms
26,728 KB
testcase_34 AC 74 ms
9,168 KB
testcase_35 AC 169 ms
10,740 KB
testcase_36 AC 298 ms
24,036 KB
testcase_37 AC 282 ms
25,304 KB
testcase_38 AC 253 ms
25,228 KB
testcase_39 AC 289 ms
24,572 KB
testcase_40 AC 290 ms
25,120 KB
testcase_41 AC 296 ms
24,788 KB
testcase_42 AC 199 ms
11,216 KB
testcase_43 AC 166 ms
11,016 KB
testcase_44 AC 193 ms
22,208 KB
testcase_45 AC 178 ms
31,104 KB
testcase_46 AC 173 ms
26,364 KB
testcase_47 AC 202 ms
25,152 KB
testcase_48 AC 242 ms
25,512 KB
testcase_49 AC 237 ms
25,796 KB
testcase_50 AC 235 ms
25,672 KB
testcase_51 AC 176 ms
11,268 KB
testcase_52 AC 183 ms
11,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define NDEBUG
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

namespace n91 {

using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  return a < b ? b - a : a - b;
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class F> class rec_lambda {
  F f;

public:
  rec_lambda(F &&f) : f(std::move(f)) {}
  template <class... Args> auto operator()(Args &&... args) const {
    return f(*this, std::forward<Args>(args)...);
  }
};
template <class F> auto make_rec(F &&f) { return rec_lambda<F>(std::move(f)); }
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}
constexpr char eoln = '\n';

} // namespace n91

namespace ei1333 {
using namespace std;

template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

template <typename G> struct StronglyConnectedComponents {
  const G &g;
  UnWeightedGraph gg, rg;
  vector<int> comp, order, used;

  StronglyConnectedComponents(G &g)
      : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
    for (int i = 0; i < g.size(); i++) {
      for (auto e : g[i]) {
        gg[i].emplace_back((int)e);
        rg[(int)e].emplace_back(i);
      }
    }
  }

  int operator[](int k) { return comp[k]; }

  void dfs(int idx) {
    if (used[idx])
      return;
    used[idx] = true;
    for (int to : gg[idx])
      dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
    if (comp[idx] != -1)
      return;
    comp[idx] = cnt;
    for (int to : rg[idx])
      rdfs(to, cnt);
  }

  void build(UnWeightedGraph &t) {
    for (int i = 0; i < gg.size(); i++)
      dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for (int i : order)
      if (comp[i] == -1)
        rdfs(i, ptr), ptr++;

    t.resize(ptr);
    for (int i = 0; i < g.size(); i++) {
      for (auto &to : g[i]) {
        int x = comp[i], y = comp[to];
        if (x == y)
          continue;
        t[x].push_back(y);
      }
    }
  }
};

} // namespace ei1333
#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>

namespace n91 {

class incremental_connectivity {
protected:
  class node_type;

public:
  using container_type = std::vector<node_type>;
  using size_type = typename container_type::size_type;
  class connected_component {
    friend incremental_connectivity;
    const node_type &root;
    constexpr connected_component(const node_type &root) noexcept
        : root(root) {}

  public:
    constexpr size_type representative() const noexcept { return root.parent; }
    constexpr size_type size() const noexcept { return root.size; }
    constexpr bool operator==(const connected_component &rhs) const noexcept {
      return &root == &rhs.root;
    }
    constexpr bool operator!=(const connected_component &rhs) const noexcept {
      return &root != &rhs.root;
    }
  };

protected:
  class node_type {
  public:
    size_type parent, size;
  };
  container_type tree;

public:
  incremental_connectivity() : tree() {}
  explicit incremental_connectivity(const size_type size) : tree(size, {0, 1}) {
    for (size_type i = 0; i < size; ++i)
      tree[i].parent = i;
  }

  bool empty() const { return tree.empty(); }
  size_type size() const { return tree.size(); }

  connected_component find_cc(size_type x) {
    while (tree[x].parent != x) {
      x = tree[x].parent = tree[tree[x].parent].parent;
    }
    return connected_component(tree[x]);
  }

  std::pair<size_type, size_type> unite(size_type x, size_type y) {
    assert(x < size());
    assert(y < size());
    x = find_cc(x).representative();
    y = find_cc(y).representative();
    if (x != y) {
      if (tree[x].size < tree[y].size)
        std::swap(x, y);
      tree[x].size += tree[y].size;
      tree[y].parent = x;
    }
    return {x, y};
  }
};

} // namespace n91

#include <cassert>
#include <list>

namespace n91 {

void main_() {
  /*
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //*/
  const usize n = scan<usize>();
  const usize m = scan<usize>();
  incremental_connectivity ic(n);
  ei1333::UnWeightedGraph g(n), buff;
  usize ccn = n;
  std::vector<std::pair<int, int>> de;
  for (const usize i : rep(0, m)) {
    const usize a = scan<usize>() - 1;
    const usize b = scan<usize>() - 1;
    switch (scan<int>()) {
    case 1: {
      if (ic.find_cc(a) == ic.find_cc(b)) {
        std::cout << "Yes" << eoln;
        return;
      }
      ic.unite(a, b);
      ccn -= 1;
      g[a].push_back(b);
      g[b].push_back(a);
    } break;
    case 2: {
      g[a].push_back(b);
      de.emplace_back(a, b);
    } break;
    }
  }
  for (const auto &e : de) {
    if (ic.find_cc(e.first) == ic.find_cc(e.second)) {
      std::cout << "Yes" << eoln;
      return;
    }
  }
  ei1333::StronglyConnectedComponents<ei1333::UnWeightedGraph> scc(g);
  scc.build(buff);
  std::cout << (ccn == buff.size() ? "No" : "Yes") << eoln;
}

} // namespace n91

int main() {
  n91::main_();
  return 0;
}
0