結果

問題 No.1266 7 Colors
ユーザー mencottonmencotton
提出日時 2020-11-02 15:54:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 337 ms / 3,000 ms
コード長 1,997 bytes
コンパイル時間 792 ms
コンパイル使用メモリ 78,712 KB
実行使用メモリ 14,744 KB
最終ジャッジ日時 2023-09-29 11:57:09
合計ジャッジ時間 8,063 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 279 ms
5,660 KB
testcase_04 AC 322 ms
10,884 KB
testcase_05 AC 278 ms
6,136 KB
testcase_06 AC 315 ms
12,016 KB
testcase_07 AC 327 ms
13,248 KB
testcase_08 AC 315 ms
11,160 KB
testcase_09 AC 299 ms
9,352 KB
testcase_10 AC 295 ms
8,784 KB
testcase_11 AC 285 ms
6,712 KB
testcase_12 AC 286 ms
7,796 KB
testcase_13 AC 296 ms
8,032 KB
testcase_14 AC 279 ms
5,920 KB
testcase_15 AC 334 ms
13,200 KB
testcase_16 AC 291 ms
7,560 KB
testcase_17 AC 337 ms
12,784 KB
testcase_18 AC 272 ms
14,744 KB
testcase_19 AC 170 ms
14,416 KB
testcase_20 AC 170 ms
14,420 KB
testcase_21 AC 229 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct UnionFind {
    vector<int> data;

    UnionFind(int sz) {
        data.assign(sz, -1);
    }

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return (false);
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return (true);
    }

    int find(int k) {
        if (data[k] < 0) return (k);
        return (data[k] = find(data[k]));
    }

    int size(int k) {
        return (-data[find(k)]);
    }
};

int main() {
    int n, m, q;
    cin >> n >> m >> q;

    UnionFind uf(n * 7);
    vector<string> s(n);
    for (int i = 0; i < n; i++) {
        cin >> s[i];
        for (int j = 0; j < 7; j++) {
            if (s[i][j] == '1' && s[i][(j + 1) % 7] == '1') {
                uf.unite(j * n + i, ((j + 1) % 7) * n + i);
            }
        }
    }

    vector<vector<int>> graph(n);

    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v, u--, v--;
        graph[u].push_back(v), graph[v].push_back(u);

        for (int j = 0; j < 7; j++) {
            if (s[u][j] == '1' && s[v][j] == '1') {
                uf.unite(j * n + u, j * n + v);
            }
        }
    }

    for (int i = 0; i < q; i++) {
        int query, x, y;
        cin >> query >> x >> y, x--, y--;
        if (query == 1) {
            s[x][y] = '1';

            for (int j = 0; j < 7; j++) {
                if (s[x][j] == '1' && s[x][(j + 1) % 7] == '1') {
                    uf.unite(j * n + x, ((j + 1) % 7) * n + x);
                }
            }

            for (auto to:graph[x]) {
                for (int j = 0; j < 7; j++) {
                    if (s[x][j] == '1' && s[to][j] == '1') {
                        uf.unite(j * n + x, j * n + to);
                    }
                }
            }
        } else {
            cout << uf.size(x) << "\n";
        }
    }
    cout << flush;
    return 0;
}
0