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<bits/stdc++.h>
const int LM=2005;
const int KLM=(int)1e5+1;
int N,K,G[LM][LM],gCnt,group[KLM];
int ans, now;
int fr,re;
int dr[]={1,-1,0,0},dc[]={0,0,1,-1};
struct Civil{
int r,c,lev;
}que[LM*LM];
void ffill(int r,int c){
if(G[r][c]>=0)return;
G[r][c]=gCnt;
que[re++]={r,c,0};
for(int k=0;k<4;k++){
ffill(r+dr[k],c+dc[k]);
}
}
void input(){
scanf("%d%d",&N,&K);
int i,j,r,c;
for(i=0;i<K;i++){
scanf("%d%d",&r,&c);
G[r][c]=-1;
}
for(i=1;i<=N;i++)for(j=1;j<=N;j++){
if(G[i][j]<0){
gCnt++;
ffill(i,j);
}
}
for(i=1;i<=gCnt;i++)group[i]=i;
}
int Find(int n){
if(n==group[n])return n;
return group[n]=Find(group[n]);
}
int push(int r,int c,int lev){
if(r<1||r>N||c<1||c>N||G[r][c])return 0;
G[r][c]=now;
que[re++]={r,c,lev};
for(int k=0;k<4;k++){
int nr=r+dr[k],nc=c+dc[k];
if(nr<1||nr>N||nc<1||nc>N)continue;
if(G[nr][nc]==0)continue;
int next=Find(G[nr][nc]);
if(now==next)continue;
group[next]=now;
gCnt--;
}
if(gCnt==1)ans=lev;
return gCnt==1;
}
int BFS(){
if(gCnt==1)return ans;
while(fr<re){
Civil&t=que[fr++];
now=Find(G[t.r][t.c]);
for(int k=0;k<4;k++){
if(push(t.r+dr[k],t.c+dc[k],t.lev+1)){
return ans;
}
}
}
}
int main(){
input();
printf("%d\n",BFS());
}
Compilation message (stderr)
civilization.cpp: In function 'void input()':
civilization.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&N,&K);
~~~~~^~~~~~~~~~~~~~
civilization.cpp:25:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&r,&c);
~~~~~^~~~~~~~~~~~~~
civilization.cpp: In function 'int BFS()':
civilization.cpp:67:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |