제출 #109285

#제출 시각아이디문제언어결과실행 시간메모리
109285aer0park문명 (KOI17_civilization)C++14
19 / 100
619 ms35332 KiB
#include <bits/stdc++.h> #define f first #define s second using namespace std; typedef long long ll; typedef pair<ll,ll> pi; ll n,k,p[4000004],transp[2004][2004],cnt,coj,anw,vis[4000004]; bool ck[4000004]; ll dx[]={1,0,-1,0},dy[]={0,1,0,-1}; vector<pi> ar; queue<pi> q,wq; ll par(int a) { if(p[a]==a) return a; return p[a]=par(p[a]); } bool merge(ll a,ll b) { a=par(a),b=par(b); if(a!=b) { p[a]=b; return 1; } return 0; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin>>n>>k; for(int i=0;i<k;i++) { ll a,b;cin>>a>>b; ar.push_back({a,b}); } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) transp[i][j]=++cnt; for(int i=1;i<=n*n;i++) p[i]=i,vis[i]=-1; for(int i=0;i<k;i++) { wq.push(ar[i]); vis[transp[ar[i].f][ar[i].s]]=0; coj++; } while(1) { bool flag=1; anw++; while(!wq.empty()) { pi a=wq.front(); q.push(a); wq.pop(); } while(!q.empty()) { ll nwx=q.front().f,nwy=q.front().s; q.pop(); for(int i=0;i<4;i++) { ll ntx=nwx+dx[i],nty=nwy+dy[i]; if(ntx<1||ntx>n||nty<1||nty>n) continue; if(vis[transp[ntx][nty]]==-1) vis[transp[ntx][nty]]=anw,coj++; if(vis[p[transp[ntx][nty]]]==0) { if(merge(transp[ntx][nty],transp[nwx][nwy])) { if(vis[transp[ntx][nty]]==anw) flag=0; coj--; } } else { if(merge(transp[ntx][nty],transp[nwx][nwy])) wq.push({ntx,nty}),coj--; } } } if(coj==1) { if(flag) cout<<anw-1; else cout<<anw; break; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...