#include <bits/stdc++.h>
using namespace std;
#define sp " "
//#define endl "\n"
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 200005
int root[N], col[N], sz[N];
vector<int> edges[N];
int find(int node){
//cout<<"! "<<node<<sp<<root[node]<<endl;
if (root[node] == node) return node;
return root[node] = find(root[node]);
}
void uni(int a,int b){
a = find(a), b = find(b);
//cout<<a<<sp<<b<<endl;
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
root[b] = a;
sz[a] += sz[b];
for (auto i : edges[a]) if (find(i) != a) edges[b].pb(i);
edges[a].clear();
for (auto i : edges[b]) if (find(i) != a) edges[a].pb(i);
}
void change_color(int node, int c){
node = find(node);
int prv = col[node];
if (prv == c) return;
vector<int> merge = edges[node];
for (auto i : merge) if (col[find(i)] == c) uni(i, node);
col[find(node)] = c;
}
int32_t main(){
//fileio();
fastio();
vector<pii> dir;
dir.pb({0, 1});
dir.pb({1, 0});
int cntr = 1;
int r, s;
cin>>r>>s;
int arr[r + 5][s + 5], ind[r + 5][s + 5];
memset(arr, -1, sizeof(arr));
memset(ind, 0, sizeof(ind));
for (int i = 1; i <= r; i++){
for (int j = 1; j <= s; j++)
{
cin>>arr[i][j];
root[cntr] = cntr;
sz[cntr] = 1;
col[cntr] = arr[i][j];
ind[i][j] = cntr;
cntr++;
//cout<<i<<sp<<j<<sp<<ind[i][j]<<endl;
}
}
for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= s; j++){
for (auto k : dir){
int a = i + k.st, b = j + k.nd;
if (ind[a][b] == 0) continue;
if (arr[a][b] == arr[i][j]) uni(ind[a][b], ind[i][j]);
else {
int u = find(ind[i][j]), v = find(ind[a][b]);
edges[u].pb(v);
edges[v].pb(u);
}
}
}
}
int q;
cin>>q;
while(q--){
int r, c, x;
cin>>r>>c>>x;
change_color(ind[r][c], x);
}
for (int i = 1; i <= r; i++){
for(int j = 1; j <= s; j++)
cout<<col[find(ind[i][j])]<<sp;
cout<<endl;
}
//cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
5 ms |
5588 KB |
Output is correct |
4 |
Correct |
8 ms |
7124 KB |
Output is correct |
5 |
Correct |
208 ms |
159156 KB |
Output is correct |
6 |
Correct |
51 ms |
38092 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
9524 KB |
Output is correct |
2 |
Correct |
52 ms |
14156 KB |
Output is correct |
3 |
Correct |
65 ms |
24140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
528 ms |
385936 KB |
Output is correct |
2 |
Correct |
448 ms |
348480 KB |
Output is correct |
3 |
Correct |
651 ms |
504036 KB |
Output is correct |
4 |
Correct |
176 ms |
59664 KB |
Output is correct |
5 |
Correct |
120 ms |
31224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
715 ms |
508756 KB |
Output is correct |
2 |
Runtime error |
455 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |