AtCoder Beginner Contest 096

Submission #2461779

Source codeソースコード

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

#ifdef DEBUG
#define NDEBUG
#include "cout11.h"
#endif
#undef NDEBUG
#include <cassert>

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))

class UnionFind {
  vector<int> data;
 public:
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};



bool solve(int H,int W,vector<string>&m) {
    UnionFind uf(H*W);

    rep(r,H)rep(c,W){
        if (m[r][c] == '.') continue;

        int a = r*W+c;
        if (r != H-1 && m[r+1][c] == '#') {
            uf.unionSet(a, a+W);
        }
        if (c != W-1 && m[r][c+1] == '#') {
            uf.unionSet(a, a+1);
        }
    }
    rep(r,H)rep(c,W){
        if (m[r][c] == '.') continue;

        int a = r*W+c;
        if (uf.size(a) == 1) return false;
    }
    return true;
}

int main() {
    int H,W;cin>>H>>W;
    vector<string>m(H);
    rep(i,H) cin>>m[i];
    cout << (solve(H,W,m) ? "Yes" : "No") << endl;
    return 0;
}

Submission

Task問題 C - Grid Repainting 2
User nameユーザ名 naoya_t
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 300
Source lengthソースコード長 2218 Byte
File nameファイル名
Exec time実行時間 1 ms
Memory usageメモリ使用量 256 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - sample_01.txt,sample_02.txt,sample_03.txt
All 300 / 300 in01.txt,in02.txt,in03.txt,in04.txt,in05.txt,in06.txt,in07.txt,in08.txt,in09.txt,in10.txt,sample_01.txt,sample_02.txt,sample_03.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
in01.txt AC 1 ms 256 KB
in02.txt AC 1 ms 256 KB
in03.txt AC 1 ms 256 KB
in04.txt AC 1 ms 256 KB
in05.txt AC 1 ms 256 KB
in06.txt AC 1 ms 256 KB
in07.txt AC 1 ms 256 KB
in08.txt AC 1 ms 256 KB
in09.txt AC 1 ms 256 KB
in10.txt AC 1 ms 256 KB
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB