結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
vain0
|
| 提出日時 | 2015-11-28 01:23:49 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,715 bytes |
| コンパイル時間 | 1,372 ms |
| コンパイル使用メモリ | 99,332 KB |
| 実行使用メモリ | 20,512 KB |
| 最終ジャッジ日時 | 2024-09-14 01:34:00 |
| 合計ジャッジ時間 | 7,994 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 TLE * 1 -- * 27 |
ソースコード
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <functional>
#include <cmath>
#include <memory>
#ifndef ONLINE_JUDGE
# include <complex>
# include <random>
# include <array>
# include <unordered_map>
# define mkt make_tuple
#endif
#ifdef _LOCAL
# include "local/local.hpp"
#else
# define assert(...) ((void)0)
# define ifdebug if (false)
# define echo(...) ((void)0)
#endif
using namespace std;
typedef long long ll; typedef unsigned long long ull;
#define repi(_I, _B, _E) for(int _I = (_B); (_I) < (_E); ++ (_I))
#define rep(_I, _N) for(int _I = 0; (_I) < (_N); ++ (_I))
#define all(_X) (_X).begin(), (_X).end()
static int const dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
int h, w;
int pt(int i, int j)
{
return i * w + j;
}
set<int> neighbor_ids(int k)
{
set<int> res;
int const
i0 = k / w,
j0 = k % w;
rep(d, 4) {
int const
i = i0 + dx[d],
j = j0 + dy[d];
if ( 0 <= i && i < h && 0 <= j && j < w ) {
res.emplace(pt(i, j));
}
}
return res;
}
struct Group
{
set<int> neighbors;
size_t size;
bool color;
};
struct UnionFindForest
{
using gr_t = shared_ptr<Group>;
vector<gr_t> groups;
void init(int n)
{
groups.resize(n);
rep(k, n)
{
auto& g =
groups[k] = std::make_shared<Group>();
g->size = 1;
g->neighbors = neighbor_ids(k);
}
}
bool connects(int k, int l) const
{
return groups[k] == groups[l];
}
void merge(int k, int l)
{
gr_t g1 = groups[k];
gr_t g2 = groups[l];
if ( g1 == g2 ) return;
if ( g1->size < g2->size ) {
swap(g1, g2);
}
g1->size += g2->size;
for ( int u : g2->neighbors ) {
if ( connects(u, k) || connects(u, l) ) continue;
g1->neighbors.insert(u);
}
for ( auto& g : groups ) {
if ( g == g2 ) {
g = g1;
}
}
}
void fill(int k, bool b)
{
colorAt(k) = b;
auto neis = groups[k]->neighbors;
for ( int l : neis ) {
if ( groups[k]->color == groups[l]->color ) {
merge(k, l);
}
}
}
bool& colorAt(int k)
{
return groups[k]->color;
}
};
UnionFindForest uff;
void show_board()
{
rep(i, h)
{
rep(j, w)
{
if ( j != 0 ) cout << ' ';
cout << (uff.colorAt(pt(i, j)) ? 1 : 0);
}
cout << endl;
}
}
int main()
{
cin >> h >> w;
uff.init(h * w);
rep(i, h) rep(j, w)
{
int a;
cin >> a;
uff.colorAt(pt(i, j)) = (a != 0);
}
// 隣接近傍の結合
rep(i, h) rep(j, w)
{
uff.fill(pt(i, j), uff.colorAt(pt(i, j)));
}
int q;
cin >> q;
rep(_, q)
{
ifdebug { show_board(); };
int r, c, a;
cin >> r >> c >> a;
r--; c--;
uff.fill(pt(r, c), a != 0);
}
show_board();
return 0;
}
vain0