# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
59109 | andy627 | 문명 (KOI17_civilization) | C++17 | 1080 ms | 55988 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 <stdio.h>
#include <queue>
#include <algorithm>
#define INF 2e9
using namespace std;
int par[2002*2002];
int dist[2002][2002],num[2002][2002];
bool is_gone[2002][2002];
queue<int> qx,qy;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int find_(int x){
if(par[x]==x) return x;
return par[x]=find_(par[x]);
}
int main(){
int n,k;
scanf("%d %d",&n,&k);
int pos=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) dist[i][j]=INF,num[i][j]=++pos;
for(int i=1;i<=pos;i++) par[i]=i;
for(int i=1;i<=k;i++){
int a,b;
scanf("%d %d",&a,&b);
dist[a][b]=0;
qx.push(a); qy.push(b);
}
int ans=0,cnt=0;
for(;;ans++){
while(!qx.empty()){
int px=qx.front(); qx.pop();
int py=qy.front(); qy.pop();
if(dist[px][py]!=ans){
qx.push(px); qy.push(py);
break;
}
is_gone[px][py]=1; cnt++;
for(int d=0;d<4;d++){
int nx=px+dx[d];
int ny=py+dy[d];
if(nx<1 || nx>n || ny<1 || ny>n) continue;
if(is_gone[nx][ny]){
int root_a=find_(num[nx][ny]);
int root_b=find_(num[px][py]);
if(root_a!=root_b) cnt--;
par[root_b]=root_a;
}
else if(dist[nx][ny]==INF){
dist[nx][ny]=dist[px][py]+1;
qx.push(nx); qy.push(ny);
}
}
}
if(cnt==1) break;
}
printf("%d\n",ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |