結果

問題 No.1266 7 Colors
ユーザー MisterMister
提出日時 2020-10-23 22:27:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 3,000 ms
コード長 4,369 bytes
コンパイル時間 889 ms
コンパイル使用メモリ 87,768 KB
実行使用メモリ 15,672 KB
最終ジャッジ日時 2023-09-28 16:29:11
合計ジャッジ時間 5,008 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 87 ms
8,344 KB
testcase_04 AC 147 ms
12,768 KB
testcase_05 AC 98 ms
8,608 KB
testcase_06 AC 156 ms
13,608 KB
testcase_07 AC 165 ms
14,620 KB
testcase_08 AC 154 ms
13,280 KB
testcase_09 AC 127 ms
11,536 KB
testcase_10 AC 125 ms
10,976 KB
testcase_11 AC 103 ms
9,168 KB
testcase_12 AC 114 ms
9,668 KB
testcase_13 AC 119 ms
10,188 KB
testcase_14 AC 96 ms
8,324 KB
testcase_15 AC 167 ms
14,876 KB
testcase_16 AC 115 ms
9,928 KB
testcase_17 AC 160 ms
14,344 KB
testcase_18 AC 81 ms
15,672 KB
testcase_19 AC 83 ms
14,304 KB
testcase_20 AC 83 ms
14,352 KB
testcase_21 AC 43 ms
6,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

}  // namespace atcoder


template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;

    Edge() = default;
    Edge(int src, int dst, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return cost > e.cost; }
};

template <class Cost = int>
struct Graph : public std::vector<std::vector<Edge<Cost>>> {
    Graph(int n = 0) : std::vector<std::vector<Edge<Cost>>>(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        (*this)[src].emplace_back(src, dst, cost);
        if (!direct) (*this)[dst].emplace_back(dst, src, cost);
    }
};

namespace ac = atcoder;

void solve() {
    int n, m, q;
    std::cin >> n >> m >> q;

    std::vector<std::string> css(n);
    for (auto& cs : css) std::cin >> cs;

    Graph<> graph(n);
    while (m--) {
        int u, v;
        std::cin >> u >> v;
        graph.span(false, --u, --v);
    }

    ac::dsu dsu(n * 7);
    for (int v = 0; v < n; ++v) {
        const auto& cs = css[v];
        for (int c = 0; c < 7; ++c) {
            if (cs[c] == '0') continue;
            if (cs[(c + 1) % 7] == '1') {
                dsu.merge(v * 7 + c, v * 7 + (c + 1) % 7);
            }

            for (auto e : graph[v]) {
                int u = e.dst;
                if (css[u][c] == '1') {
                    dsu.merge(v * 7 + c, u * 7 + c);
                }
            }
        }
    }

    while (q--) {
        int t, v, c;
        std::cin >> t >> v >> c;
        --v, --c;

        switch (t) {
            case 1: {
                auto& cs = css[v];

                cs[c] = '1';
                for (int d : {1, 6}) {
                    if (cs[(c + d) % 7] == '1') {
                        dsu.merge(v * 7 + c, v * 7 + (c + d) % 7);
                    }
                }

                for (auto e : graph[v]) {
                    int u = e.dst;
                    if (css[u][c] == '1') {
                        dsu.merge(v * 7 + c, u * 7 + c);
                    }
                }
                break;
            }
            case 2: {
                std::cout << dsu.size(v * 7) << "\n";
                break;
            }
        }
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0