結果

問題 No.1266 7 Colors
ユーザー finefine
提出日時 2020-10-23 22:33:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 3,000 ms
コード長 2,469 bytes
コンパイル時間 1,878 ms
コンパイル使用メモリ 173,660 KB
実行使用メモリ 14,688 KB
最終ジャッジ日時 2023-09-28 16:40:39
合計ジャッジ時間 6,087 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 77 ms
5,644 KB
testcase_04 AC 138 ms
10,932 KB
testcase_05 AC 84 ms
6,224 KB
testcase_06 AC 148 ms
11,960 KB
testcase_07 AC 161 ms
13,016 KB
testcase_08 AC 138 ms
11,196 KB
testcase_09 AC 124 ms
9,340 KB
testcase_10 AC 109 ms
8,788 KB
testcase_11 AC 88 ms
6,752 KB
testcase_12 AC 99 ms
7,612 KB
testcase_13 AC 104 ms
8,016 KB
testcase_14 AC 82 ms
5,900 KB
testcase_15 AC 156 ms
13,544 KB
testcase_16 AC 100 ms
7,540 KB
testcase_17 AC 157 ms
12,688 KB
testcase_18 AC 69 ms
14,688 KB
testcase_19 AC 73 ms
14,272 KB
testcase_20 AC 73 ms
14,400 KB
testcase_21 AC 39 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

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

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

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

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n, m, q;
    cin >> n >> m >> q;

    vector<string> s(n);
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }

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

    UnionFind uf(7 * n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 7; j++) {
            if (s[i][j] == '0') continue;
            for (int nex : g[i]) {
                if (s[nex][j] == '0') continue;
                uf.unite(j * n + i, j * n + nex);
            }
            int k = (j + 1) % 7;
            if (s[i][k] == '0') continue;

            uf.unite(j * n + i, k * n + i);
        }
    }

    for (int i = 0; i < q; i++) {
        int com, x, y;
        cin >> com >> x >> y;
        --x; --y;

        if (com == 1) {
            s[x][y] = '1';
            int jl = (y + 6) % 7;
            if (s[x][jl] == '1') uf.unite(jl * n + x, y * n + x);
            int jr = (y + 1) % 7;
            if (s[x][jr] == '1') uf.unite(jr * n + x, y * n + x);

            for (int nex : g[x]) {
                if (s[nex][y] == '0') continue;
                uf.unite(y * n + x, y * n + nex);
            }
        } else {
            cout << uf.size(x) << newl;
        }
    }

    return 0;
}
0