Submission #59104

#TimeUsernameProblemLanguageResultExecution timeMemory
59104andy627문명 (KOI17_civilization)C++17
54 / 100
1092 ms64324 KiB
#include <stdio.h> #include <queue> #include <algorithm> #define INF 2e9 using namespace std; int par[2222*2222]; int dist[2222][2222],num[2222][2222]; bool is_gone[2222][2222]; 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)

civilization.cpp: In function 'int main()':
civilization.cpp:23: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:32:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&a,&b);
         ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...