結果

問題 No.179 塗り分け
ユーザー t98slidert98slider
提出日時 2022-06-22 10:11:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,967 bytes
コンパイル時間 1,668 ms
コンパイル使用メモリ 175,308 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-05 16:30:56
合計ジャッジ時間 4,911 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,356 KB
testcase_04 AC 2 ms
4,360 KB
testcase_05 AC 7 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 WA -
testcase_08 AC 7 ms
4,356 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,356 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 25 ms
4,352 KB
testcase_13 AC 2 ms
4,356 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,360 KB
testcase_16 AC 1 ms
4,356 KB
testcase_17 WA -
testcase_18 AC 2 ms
4,360 KB
testcase_19 AC 2 ms
4,356 KB
testcase_20 AC 16 ms
4,360 KB
testcase_21 AC 9 ms
4,352 KB
testcase_22 AC 10 ms
4,356 KB
testcase_23 AC 2 ms
4,356 KB
testcase_24 AC 9 ms
4,352 KB
testcase_25 AC 2 ms
4,356 KB
testcase_26 WA -
testcase_27 AC 31 ms
4,356 KB
testcase_28 WA -
testcase_29 AC 42 ms
4,352 KB
testcase_30 WA -
testcase_31 AC 46 ms
4,360 KB
testcase_32 AC 15 ms
4,352 KB
testcase_33 AC 40 ms
4,356 KB
testcase_34 WA -
testcase_35 AC 45 ms
4,356 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 1 ms
4,360 KB
testcase_38 AC 2 ms
4,356 KB
testcase_39 AC 2 ms
4,352 KB
testcase_40 AC 2 ms
4,356 KB
testcase_41 AC 2 ms
4,352 KB
testcase_42 AC 2 ms
4,356 KB
testcase_43 AC 4 ms
4,360 KB
testcase_44 AC 9 ms
4,356 KB
testcase_45 AC 3 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//重み付きUnion_Find(和)
struct Weighted_dsu {
  public:
    Weighted_dsu() : _n(0) {}
    Weighted_dsu(int n) : _n(n), parent_or_size(n, -1),diff_weight(n,0) {}

    bool merge(int a, int b, long long w) {
        int x = leader(a), y = leader(b);
        if(x==y){
            if(diff(a,b)==w)return true;
            return false;
        }
        w += diff_weight[a]; w -= diff_weight[b];
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y),w*=-1;
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        diff_weight[y] = w;
        return true;
    }
    long long diff(int x, int y) {
        leader(x),leader(y);
        return diff_weight[y] - diff_weight[x];
    }
    bool same(int a, int b) {
        return leader(a) == leader(b);
    }
    int leader(int a) {
        if (parent_or_size[a] < 0) return a;
        int r = leader(parent_or_size[a]);
        diff_weight[a] += diff_weight[parent_or_size[a]];
        return parent_or_size[a] = r;
    }
    int size(int a) {
        return -parent_or_size[leader(a)];
    }
    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }
    private:
        int _n;
        std::vector<int> parent_or_size;
        std::vector<long long> diff_weight;
};

int main(){
    int H, W;
    cin >> H >> W;
    vector<string> A(H);
    for(auto &&s:A)cin >> s;
    for(int my = 0; my <= 50; my++){
        for(int mx = 0; mx <= 50; mx++){
            if(my == 0 && mx == 0)continue;
            bool flag = true;
            Weighted_dsu uf(H * W);
            for(int y = 0; y < H; y++){
                for(int x = 0; x < W; x++){
                    if(A[y][x] == '#' && uf.size(y * W + x) == 1){
                        if(y + my >= H || x + mx >= W){
                            flag = false;
                            continue;
                        }
                        if(A[y + my][x + mx] == '#'){
                            uf.merge(y * W + x, (y + my) * W + x + mx, 1);
                        }else{
                            flag = false;
                        }
                    }
                }
            }
            if(flag){
                cout << "YES" << '\n';
                return 0;
            }
        }
    }
    cout << "NO" << '\n';
}
0