結果

問題 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,125 ms
コンパイル使用メモリ 92,136 KB
実行使用メモリ 30,744 KB
最終ジャッジ日時 2024-09-15 23:18:03
合計ジャッジ時間 14,306 ms
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 105 ms
9,856 KB
testcase_05 AC 111 ms
9,984 KB
testcase_06 AC 111 ms
10,368 KB
testcase_07 AC 108 ms
10,112 KB
testcase_08 AC 159 ms
22,640 KB
testcase_09 AC 155 ms
21,932 KB
testcase_10 AC 150 ms
22,060 KB
testcase_11 AC 163 ms
23,068 KB
testcase_12 AC 147 ms
23,624 KB
testcase_13 AC 136 ms
22,320 KB
testcase_14 AC 135 ms
22,152 KB
testcase_15 AC 143 ms
22,612 KB
testcase_16 AC 269 ms
23,908 KB
testcase_17 AC 258 ms
23,928 KB
testcase_18 AC 250 ms
21,940 KB
testcase_19 AC 235 ms
22,204 KB
testcase_20 AC 278 ms
23,004 KB
testcase_21 AC 284 ms
23,620 KB
testcase_22 AC 267 ms
24,196 KB
testcase_23 AC 269 ms
24,248 KB
testcase_24 AC 253 ms
25,588 KB
testcase_25 AC 280 ms
23,152 KB
testcase_26 AC 316 ms
23,564 KB
testcase_27 AC 257 ms
23,076 KB
testcase_28 AC 264 ms
24,832 KB
testcase_29 AC 253 ms
24,800 KB
testcase_30 AC 282 ms
25,764 KB
testcase_31 AC 257 ms
25,124 KB
testcase_32 AC 246 ms
24,384 KB
testcase_33 AC 278 ms
25,664 KB
testcase_34 AC 73 ms
8,704 KB
testcase_35 AC 170 ms
10,240 KB
testcase_36 AC 281 ms
23,436 KB
testcase_37 AC 275 ms
24,620 KB
testcase_38 AC 239 ms
23,916 KB
testcase_39 AC 262 ms
24,000 KB
testcase_40 AC 250 ms
24,104 KB
testcase_41 AC 249 ms
23,988 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 170 ms
22,400 KB
testcase_45 AC 169 ms
30,744 KB
testcase_46 AC 161 ms
25,472 KB
testcase_47 AC 166 ms
25,472 KB
testcase_48 AC 224 ms
24,588 KB
testcase_49 AC 230 ms
24,696 KB
testcase_50 AC 225 ms
24,760 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