結果

問題 No.1266 7 Colors
ユーザー KoDKoD
提出日時 2020-10-23 22:21:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 3,000 ms
コード長 5,870 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 88,924 KB
実行使用メモリ 21,180 KB
最終ジャッジ日時 2024-07-21 11:00:00
合計ジャッジ時間 4,831 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 91 ms
7,808 KB
testcase_04 AC 152 ms
16,308 KB
testcase_05 AC 95 ms
8,576 KB
testcase_06 AC 155 ms
17,740 KB
testcase_07 AC 166 ms
19,476 KB
testcase_08 AC 158 ms
16,556 KB
testcase_09 AC 136 ms
13,784 KB
testcase_10 AC 125 ms
12,768 KB
testcase_11 AC 106 ms
9,600 KB
testcase_12 AC 116 ms
10,584 KB
testcase_13 AC 123 ms
11,476 KB
testcase_14 AC 105 ms
8,448 KB
testcase_15 AC 169 ms
19,712 KB
testcase_16 AC 114 ms
10,624 KB
testcase_17 AC 164 ms
18,944 KB
testcase_18 AC 79 ms
21,180 KB
testcase_19 AC 81 ms
20,316 KB
testcase_20 AC 83 ms
20,344 KB
testcase_21 AC 43 ms
5,376 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