結果

問題 No.1266 7 Colors
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-12-23 00:37:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 3,000 ms
コード長 2,180 bytes
コンパイル時間 2,321 ms
コンパイル使用メモリ 210,840 KB
実行使用メモリ 14,784 KB
最終ジャッジ日時 2023-12-23 00:37:45
合計ジャッジ時間 9,037 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 164 ms
6,676 KB
testcase_04 AC 215 ms
11,136 KB
testcase_05 AC 169 ms
6,676 KB
testcase_06 AC 216 ms
12,032 KB
testcase_07 AC 235 ms
13,184 KB
testcase_08 AC 217 ms
11,392 KB
testcase_09 AC 193 ms
9,600 KB
testcase_10 AC 182 ms
8,960 KB
testcase_11 AC 181 ms
7,040 KB
testcase_12 AC 171 ms
7,680 KB
testcase_13 AC 183 ms
8,320 KB
testcase_14 AC 165 ms
6,676 KB
testcase_15 AC 232 ms
13,440 KB
testcase_16 AC 178 ms
7,808 KB
testcase_17 AC 231 ms
12,928 KB
testcase_18 AC 209 ms
14,784 KB
testcase_19 AC 90 ms
14,380 KB
testcase_20 AC 89 ms
14,380 KB
testcase_21 AC 161 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct UnionFind {
    int ngroup, N;
    vector<int> par, siz;

    UnionFind(int _N) : N(_N), par(_N), siz(_N){
        ngroup = _N;
        for(int i = 0; i < N; i++) par[i] = i, siz[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    int unite(int x, int y) {
        int rx = root(x), ry = root(y);
        if (rx == ry) return rx;
        ngroup--;
        if (siz[rx] > siz[ry]) swap(rx, ry);
        par[rx] = ry; siz[ry] += siz[rx];
        return ry;
    }

    bool same(int x, int y) {
        int rx = root(x), ry = root(y);
        return rx == ry;
    }

    int size(int x) {return siz[root(x)];}
    int group_count() {return ngroup;}

    vector<vector<int>> groups(){
        vector<int> rev(N); int nrt=0;
        for (int i=0; i<N; i++) if (root(i) == i) rev[i] = nrt, nrt++;
        vector<vector<int>> res(nrt);
        for (int i=0; i<N; i++) res[rev[root(i)]].push_back(i);

        return res;
    }
};

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

    int N, M, Q, ans=0, t, x, y;
    cin >> N >> M >> Q;
    vector<vector<int>> E(N);
    vector<bool> ok(7*N+7);

    UnionFind tree(7*N+7);
    string s;
    for (int i=0; i<N; i++){
        cin >> s;
        for (int j=0; j<7; j++) if (s[j] == '1') ok[7*i+j] = 1;
    }

    for (int i=0; i<M; i++){
        cin >> x >> y; x--; y--;
        E[x].push_back(y);
        E[y].push_back(x);
    }

    auto f=[&](int i, int j)->void{
        if (ok[7*i+j]){
            if (ok[7*i+(j-1+7)%7]) tree.unite(7*i+j, 7*i+(j-1+7)%7);
            if (ok[7*i+(j+1)%7]) tree.unite(7*i+j, 7*i+(j+1)%7);
            for (auto to : E[i]){
                if (ok[7*to+j]) tree.unite(7*i+j, 7*to+j);
            }
        }
    };

    for (int i=0; i<N; i++){
        for (int j=0; j<7; j++) f(i, j);
    }

    while(Q){
        Q--;
        cin >> t >> x >> y;
        x--; y--;
        if (t == 1){
            ok[7*x+y] = 1;
            f(x, y);
        }
        else{
            cout << tree.size(7*x) << endl;
        }
    }
 
    return 0;
}
0