結果
問題 |
No.697 池の数はいくつか
|
ユーザー |
![]() |
提出日時 | 2018-06-12 20:25:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2,361 ms / 6,000 ms |
コード長 | 1,216 bytes |
コンパイル時間 | 2,043 ms |
コンパイル使用メモリ | 173,020 KB |
実行使用メモリ | 75,264 KB |
最終ジャッジ日時 | 2024-11-08 07:51:35 |
合計ジャッジ時間 | 18,126 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 32 |
コンパイルメッセージ
main.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] 30 | main(){ | ^~~~
ソースコード
#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; }