結果

問題 No.1023 Cyclic Tour
ユーザー noshi91noshi91
提出日時 2020-04-10 22:56:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,868 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 92,680 KB
実行使用メモリ 30,324 KB
最終ジャッジ日時 2023-10-14 03:47:32
合計ジャッジ時間 15,839 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 104 ms
9,756 KB
testcase_05 AC 104 ms
9,680 KB
testcase_06 AC 111 ms
10,152 KB
testcase_07 AC 107 ms
9,932 KB
testcase_08 AC 159 ms
22,344 KB
testcase_09 AC 153 ms
21,876 KB
testcase_10 AC 146 ms
21,840 KB
testcase_11 AC 165 ms
23,160 KB
testcase_12 AC 151 ms
23,440 KB
testcase_13 AC 135 ms
22,272 KB
testcase_14 AC 140 ms
22,020 KB
testcase_15 AC 149 ms
22,640 KB
testcase_16 AC 276 ms
23,616 KB
testcase_17 AC 281 ms
23,936 KB
testcase_18 AC 282 ms
21,932 KB
testcase_19 AC 263 ms
22,048 KB
testcase_20 AC 321 ms
22,904 KB
testcase_21 AC 339 ms
23,376 KB
testcase_22 AC 301 ms
24,120 KB
testcase_23 AC 308 ms
23,980 KB
testcase_24 AC 279 ms
25,684 KB
testcase_25 AC 310 ms
23,020 KB
testcase_26 AC 303 ms
23,248 KB
testcase_27 AC 270 ms
22,824 KB
testcase_28 AC 279 ms
24,668 KB
testcase_29 AC 252 ms
24,496 KB
testcase_30 AC 289 ms
25,360 KB
testcase_31 AC 277 ms
25,060 KB
testcase_32 AC 271 ms
24,540 KB
testcase_33 AC 293 ms
25,268 KB
testcase_34 AC 74 ms
8,568 KB
testcase_35 AC 169 ms
9,840 KB
testcase_36 AC 302 ms
23,312 KB
testcase_37 AC 280 ms
24,308 KB
testcase_38 AC 255 ms
23,808 KB
testcase_39 AC 287 ms
23,684 KB
testcase_40 AC 281 ms
23,916 KB
testcase_41 AC 285 ms
24,056 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 191 ms
22,112 KB
testcase_45 AC 176 ms
30,324 KB
testcase_46 AC 170 ms
25,288 KB
testcase_47 AC 207 ms
25,192 KB
testcase_48 AC 228 ms
24,608 KB
testcase_49 AC 232 ms
24,852 KB
testcase_50 AC 224 ms
24,380 KB
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
  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);
    } break;
    }
  }
  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