結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
nanophoto12
|
| 提出日時 | 2016-01-01 22:07:51 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,240 bytes |
| コンパイル時間 | 1,506 ms |
| コンパイル使用メモリ | 87,016 KB |
| 実行使用メモリ | 45,312 KB |
| 最終ジャッジ日時 | 2024-09-19 09:27:26 |
| 合計ジャッジ時間 | 3,201 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 WA * 26 |
ソースコード
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;
#define FOR(x,y) for(int x = 0;x < y;x++)
#define LLI long long int
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
template<int um> class UF {
public:
vector<int> _parent,_rank;
UF()
{
_parent=_rank=vector<int>(um,0);
for(int i=0;i<um;i++)
{
_parent[i]=i;
}
}
int Find(int x)
{
if(_parent[x] == x)
{
return x;
}
_parent[x] = Find(_parent[x]);
return _parent[x];
}
int Union(int x,int y)
{
int xRoot = Find(x);
int yRoot = Find(y);
if(_rank[xRoot]>_rank[yRoot])
{
_parent[xRoot] = yRoot;
return yRoot;
}
if(_rank[xRoot]<_rank[yRoot])
{
_parent[yRoot] = xRoot;
return xRoot;
}
if(xRoot != yRoot)
{
_parent[yRoot] = xRoot;
_rank[xRoot]++;
return xRoot;
}
return xRoot;
}
};
int H,W,Q;
int Board[201*201];
vector<int> S[201*201];
int GetIndex(int y,int x)
{
return y*W+x;
}
UF<5000000> uf;
int main() {
cin >> H >> W;
FOR(y, H)
{
FOR(x, W)
{
cin >> Board[GetIndex(y, x)];
}
}
FOR(y,H) FOR(x,W) {
if(y) {
if(Board[GetIndex(y,x)]==Board[GetIndex(y-1,x)]) uf.Union(GetIndex(y,x),GetIndex(y-1,x));
else S[GetIndex(y,x)].push_back(GetIndex(y-1,x));
}
if(y<H-1) {
if(Board[GetIndex(y,x)]==Board[GetIndex(y+1,x)]) uf.Union(GetIndex(y,x),GetIndex(y+1,x));
else S[GetIndex(y,x)].push_back(GetIndex(y+1,x));
}
if(x) {
if(Board[GetIndex(y,x)]==Board[GetIndex(y,x-1)]) uf.Union(GetIndex(y,x),GetIndex(y,x-1));
else S[GetIndex(y,x)].push_back(GetIndex(y,x-1));
}
if(x<W-1) {
if(Board[GetIndex(y,x)]==Board[GetIndex(y,x+1)]) uf.Union(GetIndex(y,x),GetIndex(y,x+1));
else S[GetIndex(y,x)].push_back(GetIndex(y,x+1));
}
}
FOR(x,H*W)
{
if(uf.Find(x)!=x)
{
FORR(r,S[x]) S[uf.Find(x)].push_back(r);
}
}
FOR(x,H*W)
{
if(uf.Find(x)==x)
{
sort(ALL(S[x]));
S[x].erase(unique(ALL(S[x])),S[x].end());
}
}
cin>>Q;
FOR(i,Q) {
int y, x, k;
cin>>y>>x>>k;
int parent =uf.Find(GetIndex(y-1,x-1));
if(Board[parent]==k) continue;
vector<int> NEW;
FORR(r,S[parent])
{
if(uf.Find(parent)!=uf.Find(r))
{
FORR(r2,S[uf.Find(r)]) if(uf.Find(r2)!=uf.Find(parent)) NEW.push_back(uf.Find(r2));
uf.Union(x,r);
}
}
sort(ALL(NEW));
NEW.erase(unique(ALL(NEW)),NEW.end());
S[uf.Find(parent)]=NEW;
Board[uf.Find(parent)]=k;
}
FOR(y,H) FOR(x,W) printf("%d%c",Board[uf.Find(GetIndex(y,x))],(x==W-1)?'\n':' ');
return 0;
}
nanophoto12