結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
T1610
|
| 提出日時 | 2020-06-30 19:26:43 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,590 bytes |
| コンパイル時間 | 1,892 ms |
| コンパイル使用メモリ | 180,576 KB |
| 実行使用メモリ | 14,008 KB |
| 最終ジャッジ日時 | 2024-09-13 04:50:09 |
| 合計ジャッジ時間 | 12,313 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 TLE * 1 -- * 16 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}
#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\n'
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << " "
#define dumpVL(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x)
#define dumpL(x)
#define LINE
#define LINEL
#define dumpV(v)
#define dumpVL(v)
#define STOP assert(false)
#endif
#define mp make_pair
namespace std {
template<class S, class T>
ostream &operator <<(ostream& out, const pair<S, T>& a) {
out << '(' << a.fi << ", " << a.se << ')';
return out;
}
}
struct UF{ //O(loga(n))
int n;
int m;
vi d, c;
vector<vi> st;
UF(int n) : n(n), m(n), d(n, -1), c(n), st(n){};
int root(int i){
if(d[i] < 0) return i;
return d[i] = root(d[i]);
}
bool same(int x, int y){
return root(x) == root(y);
}
bool unite(int x, int y){
x = root(x);
y = root(y);
if(x == y) return false;
if(st[x].size() < st[y].size()) swap(x, y);
d[x] += d[y];
d[y] = x;
--m;
while(st[y].size()) {
int r = root(st[y].back());
if(r != x) st[x].emplace_back(r);
st[y].pop_back();
}
sort(all(st[x]));
st[x].erase(unique(all(st[x])),st[x].end());
return true;
}
int size(int i){
return -d[root(i)];
}
int size() {
return m;
}
};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int h, w;
cin >> h >> w;
UF uf(h*w);
vector<vi> a(h, vi(w));
rep(i, h) rep(j, w) cin >> a[i][j];
rep(i, h) rep(j, w) {
uf.c[i * w + j] = a[i][j];
rep(k, 4) {
int y = i + dy[k];
int x = j + dx[k];
if(y < 0 || x < 0 || y >= h || x >= w) continue;
int r1 = uf.root(i * w + j);
int r2 = uf.root(y * w + x);
if(a[i][j] == a[y][x]) {
uf.unite(r1, r2);
}
else {
uf.st[r1].emplace_back(r2);
uf.st[r2].emplace_back(r1);
}
}
}
int q;
cin >> q;
rep(_, q) {
int y, x, c;
cin >> y >> x >> c;
--y;
--x;
int r1 = uf.root(y * w + x);
if(c == uf.c[r1]) continue;
uf.c[r1] = c;
{
auto st = uf.st[r1];
for(auto&& r2: st) {
uf.unite(r1, r2);
}
}
{
auto st = uf.st[r1];
for(auto&& r2: st) if(c == uf.c[r2]){
uf.unite(r1, r2);
}
}
}
rep(i, h) rep(j, w) {
cout << uf.c[(uf.root(i*w+j))] << (j+1 == (int)a[i].size() ? '\n' : ' ');
}
return 0;
}
T1610