#include<bits/stdc++.h>
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define pb push_back
#define mkp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
vector < vector < int > > g;
const int N = (int)2e5 + 10;
vector < int > st[N];
int p[N], cur_color[N];
void make(int v, int C){
p[v] = v;
cur_color[v] = C;
}
int fin(int v){
if(p[v] == v)return v;
return p[v] = fin(p[v]);
}
void un(int a , int b){
a = fin(a) , b = fin(b);
if(a == b)
return;
if(sz(st[a]) < sz(st[b]))
swap(a , b);
p[b] = a;
for(auto &u : st[b]){
if(fin(u) != a)
st[a].pb(u);
}
}
vector < pair < int , int > > dir = {{-1,0},{+1,0},{0,-1},{0,+1}};
int r , s;
bool valid(int x , int y){
if(x >= 0 && x < r && y >= 0 && y < s)
return true;
return false;
}
bool onezero = 1;
void makeit(int cell, int clr){
cell = fin(cell);
if(cur_color[cell] == clr)
return;
while(!st[cell].empty()){
int u = st[cell].back();
st[cell].pop_back();
int t = cur_color[fin(u)];
if(t == clr){
un(cell , fin(u));
}
cell = fin(cell);
}
cur_color[fin(cell)] = clr;
}
signed main(){
ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
cin >> r >> s;
g.resize(r , vector < int > (s));
onezero = 1;
for(int i = 0; i < r; ++i)
for(int j = 0; j < s; ++j){
cin >> g[i][j];
if(g[i][j] > 1)
onezero = 0;
}
for(int i = 0; i < r * s; ++i)
make(i, g[i / s][i % s]);
for(int i = 0; i < r; ++i)
for(int j = 0; j < s; ++j){
for(auto &u : dir){
int i1 = i + u.fi , j1 = j + u.se;
if(valid(i1, j1)){
if(g[i1][j1] == g[i][j])
un(i1*s + j1, i*s + j);
}
}
}
for(int i = 0; i < r; ++i)
for(int j = 0; j < s; ++j){
for(auto &u : dir){
int i1 = i + u.fi , j1 = j + u.se;
if(valid(i1, j1)){
if(g[i1][j1] != g[i][j]){
st[fin(i*s+j)].pb(fin(i1*s+j1));
}
}
}
}
int q;
cin >> q;
for(int i = 0; i < q; ++i){
/// online lol
int x , y, c;
cin >> x >> y >> c;
--x,--y;
makeit(x * s + y, c);
if(c > 1){
onezero = 0;
}
}
for(int i = 0; i < r; ++i){
for(int j = 0; j < s; ++j){
cout << cur_color[fin(i * s + j)] << ' ';
}
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
7028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
71 ms |
14440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
11348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |