# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
22759 | 퍼플달고싶다 (#40) | Young Zebra (KRIII5_YZ) | C++14 | 59 ms | 27512 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <numeric>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <queue>
#include <numeric>
#include <iomanip>
#define ll long long
using namespace std;
const int MAXN=2000001;
int parent[2000000],sz[2000000];
int m,n;
int ccn[2000001]={0,};
string board[1500];
int find(int u){
return u==parent[u] ? u : parent[u]=find(parent[u]);
}
int getId(int r,int c){
return r*3*m+c;
}
void mrg(int u,int v){
u=find(u); v=find(v);
if(u==v)return;
parent[u]=v;
sz[v]+=sz[u];
}
int dy[4]={0,0,1,-1};
int dx[4]={1,-1,0,0};
int ddy[8];
int ddx[8];
bool checkInf(int y,int x){
int root=find(getId(y,x));
for(int k=0;k<8;k++){
int yyy=y+ddy[k];
int xxx=x+ddx[k];
int node=find(getId(y+ddy[k],x+ddx[k]));
if(node==root)return true;
}
return false;
}
bool isCan(int y,int x,char word){
if(y>=0 && y<3*n && x>=0 && x<3*m && word==board[y][x])return true;
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
for(int i=0;i<2000000;i++)parent[i]=i,sz[i]=1;
cin>>n>>m;
ddx[0]=-m; ddx[1]=0; ddx[2]=m; ddx[3]=-m; ddx[4]=m; ddx[5]=-m; ddx[6]=0; ddx[7]=m;
ddy[0]=-n; ddy[1]=-n; ddy[2]=n; ddy[3]=0; ddy[4]=0; ddy[5]=n; ddy[6]=n; ddy[7]=n;
for(int i=0;i<n;i++){
cin>>board[i];
board[i]+=board[i];
board[i]+=board[i];
}
for(int i=0;i<n;i++){
board[i+n]=board[i];
}
for(int i=0;i<n;i++){
board[i+2*n]=board[i];
}
for(int i=0;i<3*n;i++){
for(int j=0;j<3*m;j++){
int y=i,x=j;
for(int k=0;k<4;k++){
int ny=y+dy[k];
int nx=x+dx[k];
if(isCan(ny,nx,board[y][x])){
mrg(getId(ny,nx),getId(y,x));
}
}
}
}
for(int i=n;i<n+n;i++){
for(int j=m;j<m+m;j++){
if(checkInf(i,j))cout<<-1<<' ';
else cout<<sz[find(getId(i,j))]<<' ';
}
cout<<"\n";
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |