結果
| 問題 |
No.1266 7 Colors
|
| コンテスト | |
| ユーザー |
KoD
|
| 提出日時 | 2020-10-23 22:21:45 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 210 ms / 3,000 ms |
| コード長 | 5,870 bytes |
| コンパイル時間 | 3,839 ms |
| コンパイル使用メモリ | 125,528 KB |
| 最終ジャッジ日時 | 2025-01-15 13:19:41 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#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;
}
KoD