結果

問題 No.1266 7 Colors
ユーザー KoDKoD
提出日時 2020-10-23 22:21:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 3,000 ms
コード長 5,870 bytes
コンパイル時間 920 ms
コンパイル使用メモリ 88,088 KB
実行使用メモリ 20,992 KB
最終ジャッジ日時 2023-09-28 16:15:55
合計ジャッジ時間 5,077 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 86 ms
7,788 KB
testcase_04 AC 154 ms
16,128 KB
testcase_05 AC 97 ms
8,344 KB
testcase_06 AC 163 ms
17,360 KB
testcase_07 AC 174 ms
19,432 KB
testcase_08 AC 161 ms
16,300 KB
testcase_09 AC 143 ms
13,484 KB
testcase_10 AC 139 ms
12,512 KB
testcase_11 AC 110 ms
9,324 KB
testcase_12 AC 122 ms
10,444 KB
testcase_13 AC 128 ms
11,420 KB
testcase_14 AC 95 ms
8,340 KB
testcase_15 AC 172 ms
19,652 KB
testcase_16 AC 120 ms
10,616 KB
testcase_17 AC 172 ms
18,892 KB
testcase_18 AC 76 ms
20,992 KB
testcase_19 AC 78 ms
20,196 KB
testcase_20 AC 76 ms
20,124 KB
testcase_21 AC 40 ms
4,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"

/**
 * @title Template
 */

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

#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/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 17 "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() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  usize N, M, Q;
  std::cin >> N >> M >> Q;
  std::vector<std::array<bool, 7>> color(N);
  for (auto &arr: color) {
    std::string s;
    std::cin >> s;
    for (auto i: range(0, 7)) {
      arr[i] = (s[i] == '1');
    }
  }
  std::vector<std::vector<usize>> graph(N);
  for (auto i: range(0, M)) {
    usize a, b;
    std::cin >> a >> b;
    --a; --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  union_find dsu(N * 7);
  for (auto u: range(0, N)) {
    for (auto j: range(0, 7)) {
      const auto k = (j == 6 ? 0 : j + 1);
      if (color[u][j] && color[u][k]) {
        dsu.unite(N * j + u, N * k + u);
      }
    }
    for (auto v: graph[u]) {
      for (auto j: range(0, 7)) {
        if (color[u][j] && color[v][j]) {
          dsu.unite(N * j + u, N * j + v);
        }
      }
    }
  }
  while (Q--) {
    usize type;
    std::cin >> type;
    if (type == 1) {
      usize u, c;
      std::cin >> u >> c;
      --u; --c;
      color[u][c] = true;
      usize k = (c + 6) % 7;
      if (color[u][k]) {
        dsu.unite(N * k + u, N * c + u);
      }
      k = (c + 1) % 7;
      if (color[u][k]) {
        dsu.unite(N * k + u, N * c + u);
      }
      for (auto v: graph[u]) {
        if (color[v][c]) {
          dsu.unite(N * c + u, N * c + v);
        }
      }
    }
    else {
      usize u, dump;
      std::cin >> u >> dump;
      --u;
      std::cout << dsu.component_size(u) << '\n';
    }
  }
  return 0;
}
0