結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 91 ms
8,336 KB
testcase_04 AC 153 ms
12,992 KB
testcase_05 AC 98 ms
8,600 KB
testcase_06 AC 161 ms
13,632 KB
testcase_07 AC 173 ms
14,612 KB
testcase_08 AC 163 ms
13,092 KB
testcase_09 AC 143 ms
11,500 KB
testcase_10 AC 127 ms
10,972 KB
testcase_11 AC 103 ms
9,200 KB
testcase_12 AC 112 ms
9,748 KB
testcase_13 AC 119 ms
10,348 KB
testcase_14 AC 98 ms
8,340 KB
testcase_15 AC 174 ms
14,920 KB
testcase_16 AC 121 ms
9,996 KB
testcase_17 AC 171 ms
14,416 KB
testcase_18 AC 82 ms
15,648 KB
testcase_19 AC 83 ms
14,424 KB
testcase_20 AC 84 ms
14,500 KB
testcase_21 AC 43 ms
5,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <atcoder/dsu>

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