結果

問題 No.1266 7 Colors
ユーザー trineutrontrineutron
提出日時 2020-10-23 23:55:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 3,000 ms
コード長 2,064 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 208,280 KB
実行使用メモリ 18,996 KB
最終ジャッジ日時 2023-09-28 19:15:52
合計ジャッジ時間 9,649 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 264 ms
6,040 KB
testcase_04 AC 338 ms
13,148 KB
testcase_05 AC 266 ms
6,620 KB
testcase_06 AC 350 ms
15,588 KB
testcase_07 AC 355 ms
15,836 KB
testcase_08 AC 337 ms
13,120 KB
testcase_09 AC 320 ms
11,068 KB
testcase_10 AC 311 ms
10,264 KB
testcase_11 AC 274 ms
7,516 KB
testcase_12 AC 281 ms
8,224 KB
testcase_13 AC 300 ms
9,292 KB
testcase_14 AC 260 ms
6,396 KB
testcase_15 AC 358 ms
16,288 KB
testcase_16 AC 294 ms
8,696 KB
testcase_17 AC 362 ms
16,292 KB
testcase_18 AC 278 ms
18,492 KB
testcase_19 AC 170 ms
17,448 KB
testcase_20 AC 169 ms
18,996 KB
testcase_21 AC 221 ms
4,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class unionfind {
    vector<int> par, s;

public:
    unionfind(int n) {
        for (int i = 0; i < n; i++) {
            par.push_back(i);
            s.push_back(1);
        }
    }

    int find(int n) {
        if (par.at(n) == n) return n;
        return find(par.at(n));
    }

    void merge(int m, int n) {
        m = find(m);
        n = find(n);
        if (m == n) return;
        if (s.at(m) > s.at(n)) swap(m, n);
        par.at(m) = n;
        s.at(n) += s.at(m);
    }

    int size(int n) {
        return s.at(find(n));
    }
};

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<string> s(n);
    for (int i = 0; i < n; i++) {
        cin >> s.at(i);
    }
    unionfind t(7 * n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 7; j++) {
            if (s.at(i).at(j) == '1' and s.at(i).at((j + 1) % 7) == '1') {
                t.merge(7 * i + j, 7 * i + (j + 1) % 7);
            }
        }
    }
    vector<vector<int>> to(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        to.at(u).push_back(v);
        to.at(v).push_back(u);
        for (int j = 0; j < 7; j++) {
            if (s.at(u).at(j) == '1' and s.at(v).at(j) == '1') {
                t.merge(7 * u + j, 7 * v + j);
            }
        }
    }
    for (int i = 0; i < q; i++) {
        int w, x, y;
        cin >> w >> x >> y;
        x--;
        y--;
        if (w == 1) {
            assert(s.at(x).at(y) == '0');
            s.at(x).at(y) = '1';
            if (s.at(x).at((y + 1) % 7) == '1') {
                t.merge(7 * x + y, 7 * x + (y + 1) % 7);
            }
            if (s.at(x).at((y + 6) % 7) == '1') {
                t.merge(7 * x + y, 7 * x + (y + 6) % 7);
            }
            for (auto next : to.at(x)) {
                if (s.at(next).at(y) == '1') {
                    t.merge(7 * x + y, 7 * next + y);
                }
            }
        } else {
            cout << t.size(7 * x) << endl;
        }
    }
}
0