結果

問題 No.1266 7 Colors
ユーザー trineutrontrineutron
提出日時 2020-10-23 23:55:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 411 ms / 3,000 ms
コード長 2,064 bytes
コンパイル時間 2,835 ms
コンパイル使用メモリ 203,736 KB
最終ジャッジ日時 2025-01-15 14:24:46
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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