結果

問題 No.13 囲みたい!
ユーザー Unbakedbread
提出日時 2025-08-14 12:53:14
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 769 bytes
コンパイル時間 3,621 ms
コンパイル使用メモリ 280,108 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2025-08-14 12:53:19
合計ジャッジ時間 4,154 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

const int DX[2]={0,1},DY[2]={1,0};

struct dsu{
	vector<int> p;
	dsu(int _n):p(_n,-1){}
	int root(int x){return (p[x]<0?x:(p[x]=root(p[x])));}
	void unite(int x,int y){
		x=root(x),y=root(y);
		if(p[x]>p[y]) swap(x,y);
		p[x]+=p[y]; p[y]=x;
	}bool same(int x,int y){return root(x)==root(y);}
};

int main(){
	int H,W;cin>>W>>H;
	vector<vector<int>> M(H,vector<int>(W));
	rep(i,H) rep(j,W) cin>>M[i][j];
	
	dsu uf(H*W);
	rep(i,H-1) rep(j,W) if(M[i][j]==M[i+1][j]) uf.unite(i*W+j,(i+1)*W+j);
	rep(i,H) rep(j,W-1){
		if(M[i][j]!=M[i][j+1]) continue;
		if(uf.same(i*W+j,i*W+j+1)){
			cout<<"possible\n";
			return 0;
		}
		uf.unite(i*W+j,i*W+j+1);
	}
	
	cout<<"impossible\n";
	return 0;
}
0