結果

問題 No.1266 7 Colors
ユーザー oevloevl
提出日時 2020-10-27 04:22:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 332 ms / 3,000 ms
コード長 1,791 bytes
コンパイル時間 3,603 ms
コンパイル使用メモリ 206,320 KB
実行使用メモリ 14,640 KB
最終ジャッジ日時 2023-09-29 03:09:59
合計ジャッジ時間 11,761 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 252 ms
5,684 KB
testcase_04 AC 314 ms
11,008 KB
testcase_05 AC 257 ms
6,216 KB
testcase_06 AC 326 ms
12,104 KB
testcase_07 AC 332 ms
13,192 KB
testcase_08 AC 322 ms
11,192 KB
testcase_09 AC 309 ms
9,348 KB
testcase_10 AC 298 ms
8,768 KB
testcase_11 AC 270 ms
6,708 KB
testcase_12 AC 273 ms
7,496 KB
testcase_13 AC 282 ms
8,164 KB
testcase_14 AC 256 ms
5,864 KB
testcase_15 AC 329 ms
13,196 KB
testcase_16 AC 284 ms
7,536 KB
testcase_17 AC 332 ms
12,708 KB
testcase_18 AC 265 ms
14,640 KB
testcase_19 AC 161 ms
14,372 KB
testcase_20 AC 161 ms
14,368 KB
testcase_21 AC 220 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

class UnionFind {
public:
    vector<int> uni;
    UnionFind(int s) : uni(s, -1) {}
    int root(int a) {
        return uni[a] < 0 ? a : uni[a] = root(uni[a]);
    }
    bool merge(int a, int b) {
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(uni[a] > uni[b]) swap(a, b);
        uni[a] = uni[a] + uni[b];
        uni[b] = a;
        return true;
    }
    bool same(int a, int b) {
        return root(a) == root(b);
    }
    int size(int a) {
        return -uni[root(a)];
    }
};

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<string> s(n);
    for(auto &e : s) {
        cin >> e;
    }
    UnionFind uf(7 * n);
    vector<vector<int>> G(n);

    auto connect = [&](int i, int j, int k, int l) -> void {
        j = (j + 7) % 7;
        l = (l + 7) % 7;
        if(s[i][j] == '0' || s[k][l] == '0') {
            return;
        }
        uf.merge(7 * i + j, 7 * k + l);
    };

    auto add = [&](int x, int y) -> void {
        s[x][y] = '1';
        connect(x, y, x, y - 1);
        connect(x, y, x, y + 1);
        for(auto &to : G[x]) {
            connect(x, y, to, y);
        }
    };

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < 7; ++j) {
            connect(i, j, i, j + 1);
        }
    }

    for(int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        for(int j = 0; j < 7; ++j) {
            connect(a, j, b, j);
        }
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    while(q--) {
        int t, x, y;
        cin >> t >> x >> y;
        x--, y--;
        if(t == 1) {
            add(x, y);
        } else {
            cout << uf.size(7 * x) << '\n';
        }
    }
    return 0;
}
0