Submission #942185

# Submission time Handle Problem Language Result Execution time Memory
942185 2024-03-10T10:36:12 Z vjudge1 Paint (COI20_paint) C++17
0 / 100
31 ms 37904 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define OYY 100000000000005
#define mod 1000000007
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR for(int i=1;i<=n;i++)
#define mid (start+end)/2
#define lim 1005
#define fi first
#define se second

int r,s;
int dizi[lim][lim],vis[lim][lim];
int old_par[lim][lim];
int color[4*lim];
int xd[4]={0,0,-1,1},yd[4]={1,-1,0,0};
int par[lim],siz[lim];

set<int> st[lim];

inline bool cont(int x,int y){
	if(x<1 || y<1 || x>r || y>s)return 0;
	return 1;
}

inline void dfs(int x,int y,int renk,int now){
	if(!cont(x,y))return;
	if(dizi[x][y]!=now)return ;
	if(vis[x][y])return ;
	vis[x][y]=1;
	old_par[x][y]=renk;
	for(int i=0;i<4;i++){
		dfs(x+xd[i],y+yd[i],renk,now);
	}
}

inline void dfs2(int x,int y){
	if(!cont(x,y))return;
	if(vis[x][y])return ;
	vis[x][y]=1;
	for(int i=0;i<4;i++){
		int nx=x+xd[i];
		int ny=y+yd[i];
		if(cont(nx,ny) && dizi[x][y]!=dizi[nx][ny]){
			st[old_par[nx][ny]].insert(old_par[x][y]);
			st[old_par[x][y]].insert(old_par[nx][ny]);
		}
		dfs2(nx,ny);
	}
}

inline int find(int node){
	if(par[node]==node)return node;
	return par[node]=find(par[node]);
}

inline void unite(int node1,int node2){
	node1=find(node1);
	node2=find(node2);
	if(siz[node1]<siz[node2])swap(node1,node2);
	par[node2]=par[node1];
	siz[node1]+=siz[node2];
	for(auto x:st[node2]){
		if(find(x)!=node1)st[node1].insert(find(x));
	}
	st[node1].erase(node2);
}

int32_t main(){
	faster
	cin>>r>>s;
	for(int i=1;i<=r;i++){
		for(int j=1;j<=s;j++){
			cin>>dizi[i][j];
		}
	}
	int cur=1;
	for(int i=1;i<=r;i++){
		for(int j=1;j<=s;j++){
			if(vis[i][j]==0){
				dfs(i,j,cur,dizi[i][j]);
				color[cur]=dizi[i][j];
				cur++; // node olarak tanımlama
			}
		}
	}
	memset(vis,0,sizeof(vis));
	/*for(int i=1;i<=r;i++){
		for(int j=1;j<=s;j++){
			cout<<old_par[i][j]<<" ";
		}
		cout<<endl;
	}
	*/
	dfs2(1,1);
	/*
	for(int i=1;i<cur;i++){
		cout<<i<<" "<<st[i].size()<<endl;
		for(auto x:st[i])cout<<x<<" ";
		cout<<endl<<endl;
	}*/
	
	////////////////////////////////////////////////////////////////////
	
	for(int i=1;i<cur;i++){
		siz[i]=1;
		par[i]=i;
	}
	
	int q;cin>>q;
	while(q--){
		int a,b,val;cin>>a>>b>>val;
		int nod=find(old_par[a][b]);
		for(auto komsu:st[nod]){
			if(find(komsu)==nod)continue;
			if(color[find(komsu)]==val){
				unite(komsu,nod);
			}
		}
		color[nod]=val;
		st[nod].erase(nod);
	}
	
	
	for(int i=1;i<=r;i++){
		for(int j=1;j<=s;j++){
			cout<<color[find(old_par[i][j])]<<" ";
		}
		cout<<'\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11608 KB Output is correct
2 Runtime error 12 ms 22876 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 22 ms 27220 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 37904 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 30 ms 37380 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -