#include<bits/stdc++.h>
using namespace std;
#define N 200010
#define M 50
#define INFLL 2000000000000000020
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> pll;
ll h;
ll n,m;
ll head[N];
ll color[N];
bool marc[N];
vector<ll>reach[N];
vector<ll>chega[N];
set<ll>aux;
ll transform(ll i,ll j)
{
return i*m+j-m;
}
ll Dx[10]={-1,0,1,0};
ll Dy[10]={0,1,0,-1};
bool valid(ll i,ll j)
{
return (bool)(i>0 && i<=n && j>0 && j<=m);
}
void DFS(ll i,ll j)
{
ll x=transform(i,j),k,ni,nj,y;
marc[x]=true;
head[x]=h;
for(k=0;k<4;k++)
{
ni=i+Dx[k];
nj=j+Dy[k];
y=transform(ni,nj);
if(!valid(ni,nj))
continue;
if(color[y]==color[x])
{
if(!marc[y])
DFS(ni,nj);
}else
chega[h].pb(y);
}
return;
}
ll Find(ll u)
{
if(head[u]==u)
return u;
return head[u]=Find(head[u]);
}
void merge(ll u,ll v)
{
aux.clear();
for(auto x : reach[u])
{
aux.insert(x);
}
for(auto x : reach[v])
{
aux.insert(x);
}
reach[u].clear();
for(auto x : aux)
{
reach[u].pb(x);
}
head[v]=u;
return;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll q,x,i,j,c,u,v;
cin >> n >> m;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
x=transform(i,j);
cin >> color[x];
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
x=transform(i,j);
if(marc[x])
continue;
h=x;
DFS(i,j);
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
x=transform(i,j);
aux.clear();
for(auto c : chega[x])
{
u=head[c];
if(aux.find(u)!=aux.end())
continue;
aux.insert(u);
reach[x].pb(u);
}
}
}
/*for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
x=transform(i,j);
if(reach[x].empty())
continue;
cout << x << ": ";
for(auto u : reach[x])
cout << u << " ";
cout << endl;
}
}*/
cin >> q;
while(q)
{
q--;
cin >> i >> j >> c;
x=transform(i,j);
u=Find(x);
for(auto r : reach[u])
{
v=Find(r);
if(u==v)
continue;
if(color[v]==c)
{
merge(u,v);
}
}
color[u]=c;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
x=transform(i,j);
u=Find(x);
cout << color[u] << ((j==m) ? '\n' : ' ');
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
10 ms |
25944 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
22 ms |
31576 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
89 ms |
63828 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
53 ms |
51024 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |