結果

問題 No.13 囲みたい!
ユーザー cureskolcureskol
提出日時 2020-03-27 11:22:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,533 bytes
コンパイル時間 6,609 ms
コンパイル使用メモリ 169,244 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-30 16:21:45
合計ジャッジ時間 2,814 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int d[100][100];
//BEGIN CUT HERE
struct UnionFind{
  int num;//連結成分の数
  vector<int> r,p;//そのグループのサイズ,自分の親っぽいやつ
  UnionFind(){}
  UnionFind(int n):num(n),r(n,1),p(n,0){iota(p.begin(),p.end(),0);}

  int find(int x){//どのグループに所属するか
    return (x==p[x]?x:p[x]=find(p[x]));//xがグループの名前と一致するまでxを親にする
  }

  bool same(int x,int y){//同じグループかどうか
    return find(x)==find(y);
  }

  void unite(int x,int y){//xとyを同じグループにする
    x=find(x);y=find(y);//xとyのグループの名前をどっちかが変える
    if(x==y) return;
    if(r[x]<r[y]) swap(x,y);//サイズが大きい方をxとする
    r[x]+=r[y];//yの親をxにする(今までyだったグループ名がxになる)
    p[y]=x;
    num--;
  }

  int size(int x){//グループの大きさ
    return r[find(x)];
  }

  int count() const{//グループの数
    return num;
  }
};
//END CUT HERE
template<typename T>
void fin(T a){
  cout<<a<<endl;
  exit(0);
}

signed main(){
  int w,h;cin>>w>>h;
  for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin>>d[i][j];
  UnionFind uf(10000);
  for(int i=1;i<h;i++)for(int j=0;j<w;j++)if(d[i][j]==d[i-1][j])uf.unite(i*w+j,(i-1)*w+j);
  for(int j=1;j<w;j++){
    for(int i=0;i<h;i++){
      if(d[i][j]!=d[i][j-1])continue;
      if(uf.same(i*w+j,i*w+j-1))fin("possible");
      uf.unite(i*w+j,i*w+j-1);
    }
  }
  fin("impossible");
}
0