結果

問題 No.13 囲みたい!
ユーザー ChanyuhChanyuh
提出日時 2019-10-03 16:06:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,676 bytes
コンパイル時間 1,042 ms
コンパイル使用メモリ 107,504 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 09:27:09
合計ジャッジ時間 1,835 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct uf {
private:
    vector<int> par,ran;
public:
    vector<int> edge,size;
    void init(int n) {
        par.resize(n,0);
        ran.resize(n,0);
        size.resize(n,1);
        edge.resize(n,0);
        rep(i,n) {
            par[i]=i;
        }
    }
    int find(int x) {
        if (par[x]==x)return x;
        else return par[x]=find(par[x]);
    }
    void unite(int x, int y) {
        x=find(x),y=find(y);
        if (x==y){
            edge[x]++;
            return;
        }
        if (ran[x]<ran[y]) {
            par[x]=y;
            size[y]+=size[x];
            edge[y]+=edge[x]+1;
        }
        else {
            par[y]=x;
            size[x]+=size[y];
            edge[x]+=edge[y]+1;
            if (ran[x]==ran[y])ran[x]++;
        }
    }
    bool same(int x,int y) {
        return find(x)==find(y);
    }
};

int h,w;
int m[110][110];

void solve(){
    cin >> w >> h;
    rep(i,h){
        rep(j,w){
            cin >> m[i][j];
        }
    }
    uf u;u.init(h*w);
    rep(i,h){
        rep(j,w-1){
            if (m[i][j]==m[i][j+1]) u.unite(i*w+j,i*w+j+1);
        }
    }
    
    rep(j,w){
        rep(i,h-1){
            if (m[i][j]==m[i+1][j]) u.unite(i*w+j,(i+1)*w+j);
        }
    }

    rep(i,h){
        rep(j,w){
            //cout << i << " " << j << endl;
            //cout << "size:" << u.size[u.find(i*w+j)] << " edge:" << u.edge[u.find(i*w+j)] << endl;
            if (u.size[u.find(i*w+j)]<=u.edge[u.find(i*w+j)]) {
                cout << "possible" << endl;
                return;
            }
        }
    }
    cout << "impossible" << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0