結果
問題 |
No.697 池の数はいくつか
|
ユーザー |
![]() |
提出日時 | 2020-02-26 22:19:20 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,409 bytes |
コンパイル時間 | 1,105 ms |
コンパイル使用メモリ | 104,892 KB |
実行使用メモリ | 628,848 KB |
最終ジャッジ日時 | 2024-11-25 15:57:51 |
合計ジャッジ時間 | 40,758 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 MLE * 1 |
other | AC * 29 TLE * 2 MLE * 1 |
ソースコード
#include<algorithm> #include<cmath> #include<cstdio> #include<functional> #include<iomanip> #include<iostream> #include<map> #include<numeric> #include<queue> #include<set> #include<string> #include<utility> #include<vector> using namespace std; typedef long long ll; typedef unsigned long long ull; const ll MOD = 1000000007; #define rep(i,n) for(int i=0;i<n;i++) #define repl(i,s,e) for(int i=s;i<e;i++) #define reple(i,s,e) for(int i=s;i<=e;i++) #define revrep(i,n) for(int i=n-1;i>=0;i--) #define all(x) (x).begin(),(x).end() int H, W; class UnionFind { //par[i]:ノードiの親 //count[i]:iが親の時、iが属するグループのノード数 vector<int> par; int m_groupCount = 0; public: UnionFind(int N) { par.push_back(0); m_groupCount = N; for (int i = 1; i <= N; i++) { //最初は全てが根であるとして初期化 par.push_back(i); } } //データxが属する木の根を再帰で得る:root(x) = {xの木の根} int root(int x) { if (par[x] == x) return x; //ついでに親を張り替え return par[x] = root(par[x]); } //xとyの木を併合 void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける par[rx] = ry; //木の併合によってグループが1つ減る m_groupCount--; } // 2つのデータx, yが属する木が同じならtrueを返す bool same(int x, int y) { return root(x) == root(y); } //UnionFindのグループの個数を返す int groupCount() { return m_groupCount; } }; bool IsInRange(int w, int h) { return 0 <= w && w <= W - 1 && 0 <= h && h <= H - 1; } int ToIndex(int w, int h) { return h * W + w; } int main() { cin >> H >> W; UnionFind uf(H * W); set<int> s; rep(h, H) { rep(w, W) { int a; cin >> a; if (a != 1) continue; int idx = ToIndex(w, h); //1の場合setで保存 s.insert(idx); //上と左だけ結合判定 if (IsInRange(w - 1, h)) { int i = ToIndex(w - 1, h); if (s.count(i) > 0) uf.unite(idx, i); } if (IsInRange(w, h - 1)) { int i = ToIndex(w, h - 1); if (s.count(i) > 0) uf.unite(idx, i); } } } //個数確認 //親の個数をカウント ll count = 0; for (auto x : s) { if (uf.root(x) == x) count++; } cout << count << endl; return 0; }