結果

問題 No.697 池の数はいくつか
ユーザー BantakoBantako
提出日時 2018-06-12 20:25:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,234 ms / 6,000 ms
コード長 1,216 bytes
コンパイル時間 1,966 ms
コンパイル使用メモリ 170,248 KB
実行使用メモリ 75,392 KB
最終ジャッジ日時 2024-04-25 20:06:42
合計ジャッジ時間 16,593 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 232 ms
17,348 KB
testcase_25 AC 230 ms
16,272 KB
testcase_26 AC 234 ms
16,512 KB
testcase_27 AC 231 ms
16,984 KB
testcase_28 AC 231 ms
15,572 KB
testcase_29 AC 1,532 ms
47,604 KB
testcase_30 AC 2,186 ms
75,392 KB
testcase_31 AC 1,520 ms
47,400 KB
testcase_32 AC 2,163 ms
75,316 KB
testcase_33 AC 2,234 ms
75,356 KB
testcase_34 AC 2,158 ms
75,300 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   30 | main(){
      | ^~~~

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
int MOD = 1e9+7;
const int MAX_N = 9000000;
int par[MAX_N];
bool V[3000][3000];
struct UnionFind{
    //int par[MAX_N];
    void init(int n){
        for(int i = 0;i < n;i++)par[i] = i;
    }
    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);
    }
    void unite(int x,int y){
        x = root(x);
        y = root(y);
        if(x == y)return;
        par[y] = x;
    }
};

main(){
    int H,W;    
    int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1};
    
    cin >> H >> W;
    rep(i,0,H)rep(j,0,W)cin >> V[i][j];

    UnionFind uf;
    uf.init(H*W);//(i,j) → i*W + j

    rep(i,0,H)rep(j,0,W){
        if(V[i][j]){
            rep(k,0,4){
                int x = dx[k] + j, y = dy[k] + i;
                if(x < 0 || x >= W || y < 0 || y >= H)continue;
                if(V[y][x])uf.unite(i * W + j, y * W + x);
            }
        }
    }
    set<int> st;
    rep(i,0,H)rep(j,0,W)if(V[i][j])st.insert(uf.root(i * W + j));
    cout << st.size() << endl;
}
0