#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
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 |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
3 ms |
888 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
900 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
888 KB |
Output is correct |
12 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
3 ms |
888 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
900 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
888 KB |
Output is correct |
12 |
Correct |
56 ms |
18296 KB |
Output is correct |
13 |
Correct |
45 ms |
14712 KB |
Output is correct |
14 |
Correct |
52 ms |
17784 KB |
Output is correct |
15 |
Correct |
40 ms |
13304 KB |
Output is correct |
16 |
Correct |
11 ms |
3448 KB |
Output is correct |
17 |
Correct |
113 ms |
20192 KB |
Output is correct |
18 |
Correct |
122 ms |
20856 KB |
Output is correct |
19 |
Correct |
124 ms |
20904 KB |
Output is correct |
20 |
Correct |
125 ms |
20956 KB |
Output is correct |
21 |
Correct |
123 ms |
20856 KB |
Output is correct |
22 |
Correct |
6 ms |
504 KB |
Output is correct |
23 |
Correct |
55 ms |
19960 KB |
Output is correct |
24 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
3 ms |
888 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
900 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
888 KB |
Output is correct |
12 |
Correct |
56 ms |
18296 KB |
Output is correct |
13 |
Correct |
45 ms |
14712 KB |
Output is correct |
14 |
Correct |
52 ms |
17784 KB |
Output is correct |
15 |
Correct |
40 ms |
13304 KB |
Output is correct |
16 |
Correct |
11 ms |
3448 KB |
Output is correct |
17 |
Correct |
113 ms |
20192 KB |
Output is correct |
18 |
Correct |
122 ms |
20856 KB |
Output is correct |
19 |
Correct |
124 ms |
20904 KB |
Output is correct |
20 |
Correct |
125 ms |
20956 KB |
Output is correct |
21 |
Correct |
123 ms |
20856 KB |
Output is correct |
22 |
Correct |
6 ms |
504 KB |
Output is correct |
23 |
Correct |
55 ms |
19960 KB |
Output is correct |
24 |
Correct |
172 ms |
42592 KB |
Output is correct |
25 |
Correct |
226 ms |
51160 KB |
Output is correct |
26 |
Correct |
149 ms |
34400 KB |
Output is correct |
27 |
Correct |
188 ms |
42456 KB |
Output is correct |
28 |
Correct |
111 ms |
27860 KB |
Output is correct |
29 |
Correct |
359 ms |
61532 KB |
Output is correct |
30 |
Correct |
462 ms |
62496 KB |
Output is correct |
31 |
Correct |
365 ms |
62540 KB |
Output is correct |
32 |
Correct |
365 ms |
61424 KB |
Output is correct |
33 |
Correct |
382 ms |
63032 KB |
Output is correct |
34 |
Correct |
163 ms |
51320 KB |
Output is correct |
35 |
Correct |
10 ms |
504 KB |
Output is correct |
36 |
Correct |
3 ms |
888 KB |
Output is correct |