結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
moti
|
| 提出日時 | 2015-11-28 03:29:50 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,138 bytes |
| コンパイル時間 | 1,481 ms |
| コンパイル使用メモリ | 121,280 KB |
| 実行使用メモリ | 14,008 KB |
| 最終ジャッジ日時 | 2024-09-14 02:19:11 |
| 合計ジャッジ時間 | 8,692 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 TLE * 1 -- * 10 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:86:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
86 | scanf("%d%d", &H, &W);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:89:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
89 | scanf("%d", &A[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:115:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
115 | scanf("%d", &Q);
| ~~~~~^~~~~~~~~~
main.cpp:118:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
118 | scanf("%d%d%d", &r, &c, &x); r--, c--;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>
using namespace std;
#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define minus(c) memset(c, -1, sizeof c)
struct UnionFind
{
// vector<int> rank;
// vector<int> rid;
int rank[200*200+1];
int rid[200*200+1];
UnionFind(int n) {
rep(i, n) rid[i] = -1;
}
void unite(int u, int v) {
u = root(u), v = root(v);
if(u == v) { return ; }
if(rank[u] < rank[v]) {
rid[u] = v;
}
else {
rid[v] = u;
if(rank[u] == rank[v]) {
rank[u]++;
}
}
}
bool same(int u, int v) {
return root(u) == root(v);
}
inline int root(int x) {
if(rid[x] < 0) return x;
else return rid[x] = root(rid[x]);
}
inline int operator[](int x) { return root(x); }
};
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
int H, W;
int A[201][201];
int Q;
bool rootCol[200*200+1];
vector<int> outers[200*200+1]; // []はroot
bool inrange(int y, int x) {
return 0<=y&&y<H && 0<=x&&x<W;
}
typedef pair<int, int> pii;
inline int chpos(int y, int x) { return y*W+x; }
inline pii coord(int p) { return std::move(make_pair(p/W, p%W)); }
int main() {
scanf("%d%d", &H, &W);
rep(i, H) rep(j, W) {
scanf("%d", &A[i][j]);
rootCol[chpos(i, j)] = A[i][j];
}
UnionFind uf(H * W + 1);
rep(i, H) rep(j, W) rep(k, 4) {
int ni = i+dy[k], nj = j+dx[k];
if(!inrange(ni, nj)) { continue; }
if(A[i][j] == A[ni][nj]) {
uf.unite(chpos(i, j), chpos(ni, nj));
rootCol[uf.root(chpos(i, j))] = A[i][j];
}
}
rep(i, H) rep(j, W) rep(k, 4) {
int ni = i+dy[k], nj = j+dx[k];
if(!inrange(ni, nj)) { continue; }
if(A[i][j] != A[ni][nj]) {
outers[uf[chpos(i, j)]].push_back(uf[chpos(ni, nj)]);
int rt = uf[chpos(i, j)];
sort(all(outers[rt]));
outers[rt].erase(unique(all(outers[rt])), outers[rt].end());
}
}
scanf("%d", &Q);
rep(_, Q) {
int r, c, x;
scanf("%d%d%d", &r, &c, &x); r--, c--;
int const RawPos = uf.root(chpos(r, c));
if(rootCol[RawPos] != x) {
vector<int> os; for(auto& e: outers[RawPos]) os.push_back(e);
for(auto& e: os) {
if(uf.same(e, RawPos)) { continue; }
if(rootCol[uf[e]] != rootCol[RawPos]) {
copy(all(outers[uf[e]]), back_inserter(outers[RawPos]));
uf.unite(e, RawPos);
}
}
int rt = uf.root(RawPos);
if(RawPos != rt) {
swap(outers[rt], outers[RawPos]);
}
stable_sort(all(outers[rt]));
outers[rt].erase(unique(all(outers[rt])), outers[rt].end());
rootCol[rt] = x;
}
}
rep(i, H) {
rep(j, W) {
if(j) printf(" ");
printf("%d", rootCol[uf.root(chpos(i, j))]);
}
puts("");
}
return 0;
}
moti