結果

問題 No.1266 7 Colors
ユーザー nawawannawawan
提出日時 2020-10-23 23:27:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 260 ms / 3,000 ms
コード長 2,460 bytes
コンパイル時間 771 ms
コンパイル使用メモリ 76,808 KB
実行使用メモリ 17,484 KB
最終ジャッジ日時 2023-09-28 18:42:27
合計ジャッジ時間 6,423 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 172 ms
6,008 KB
testcase_04 AC 240 ms
13,072 KB
testcase_05 AC 177 ms
6,524 KB
testcase_06 AC 247 ms
14,196 KB
testcase_07 AC 252 ms
15,708 KB
testcase_08 AC 237 ms
13,340 KB
testcase_09 AC 223 ms
10,972 KB
testcase_10 AC 216 ms
10,200 KB
testcase_11 AC 185 ms
7,524 KB
testcase_12 AC 193 ms
8,328 KB
testcase_13 AC 199 ms
9,132 KB
testcase_14 AC 174 ms
6,632 KB
testcase_15 AC 260 ms
15,908 KB
testcase_16 AC 197 ms
8,636 KB
testcase_17 AC 244 ms
15,396 KB
testcase_18 AC 194 ms
17,484 KB
testcase_19 AC 73 ms
17,264 KB
testcase_20 AC 73 ms
17,176 KB
testcase_21 AC 164 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct UnionFind{
    vector<int> par;//親(根)
    vector<int> rank;//木の深さ

    UnionFind(int n){//初期化関数
        par.resize(n);
        rank.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;//初めはノード一個の木なので根は自身
            rank[i] = 1;
        }
    }

    int root(int x){//木の根を求める
    if(par[x] == x) return x;
    else return par[x] = root(par[x]);
    }

    bool same(int x, int y){//同じ木かどうか判定
        return root(x) == root(y);//同じ木ならtrue
    }

    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x == y) return;//もし同じ木に属していたら何もしない
        if(rank[x] < rank[y]) swap(x, y);//数が大きい方に小さい方を結合させる
        par[y] = x;
        rank[x] += rank[y];//深さが同じ時だけ結合後深さが増える
    }

    int size(int x) {//深さを返す関数
        return rank[root(x)];
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M, Q;
    cin >> N >> M >> Q;
    UnionFind U(7 * N);
    vector<string> s(N);
    vector<vector<int>> G(N);
    for(int i = 0; i < N; i++) {
        cin >> s[i]; 
        for(int j = 0; j < 7; j++){
            if(s[i][j] == '1' && s[i][j] == s[i][(j + 1) % 7]){
                U.unite(i * 7 + j, i * 7 + (j + 1) % 7);
            }
        }
    }
    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);
        for(int j = 0; j < 7; j++){
            if(s[u][j] == '1' && s[u][j] == s[v][j]){
                U.unite(u * 7 + j, v * 7 + j);
            }
        }
    }
    for(int i = 0; i < Q; i++){
        int f;
        cin >> f;
        if(f == 1){
            int x, y;
            cin >> x >> y;
            x--;
            y--;
            s[x][y] = '1';
            if(s[x][(y - 1 + 7) % 7] == '1') U.unite(x * 7 + y, x * 7 + (y - 1 + 7) % 7);
            if(s[x][(y + 1) % 7] == '1') U.unite(x * 7 + y, x * 7 + (y + 1) % 7);
            for(int v: G[x]){
                if(s[v][y] == '1') U.unite(x * 7 + y, v * 7 + y);
            }
        }
        else{
            int x, y;
            cin >> x >> y;
            x--;
            cout << U.size(x * 7) << endl;
        }
    }
}
0