# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
152344 | arnold518 | 문명 (KOI17_civilization) | C++14 | 824 ms | 43068 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2000;
const int INF = 2e9;
const int dy[]={-1, 1, 0, 0};
const int dx[]={0, 0, -1, 1};
int N, K;
int A[MAXN+10][MAXN+10];
bool vis[MAXN+10][MAXN+10];
int par[MAXN*MAXN+10];
int f(int y, int x) { y--; x--; return y*N+x; }
int Find(int x) { return x==par[x]?x:(par[x]=Find(par[x])); }
int main()
{
int i, j;
scanf("%d%d", &N, &K);
for(i=0; i<N*N; i++) par[i]=i;
for(i=1; i<=N; i++) for(j=1; j<=N; j++) A[i][j]=INF;
queue<pii> Q;
for(i=1; i<=K; i++)
{
int y, x;
scanf("%d%d", &y, &x);
A[y][x]=0; Q.push({y, x});
}
K=0;
int ans=0;
for(ans=0; ; ans++)
{
int sz=Q.size();
while(sz--)
{
pii now=Q.front(); Q.pop();
vis[now.first][now.second]=1; K++;
for(i=0; i<4; i++)
{
pii nxt={now.first+dy[i], now.second+dx[i]};
if(!(1<=nxt.first && nxt.first<=N && 1<=nxt.second && nxt.second<=N)) continue;
if(vis[nxt.first][nxt.second])
{
int a=Find(f(now.first, now.second)), b=Find(f(nxt.first, nxt.second));
if(a!=b) K--;
par[a]=b;
}
else if(A[nxt.first][nxt.second]==INF)
{
A[nxt.first][nxt.second]=A[now.first][now.second]+1;
Q.push(nxt);
}
}
}
if(K==1) return !printf("%d", ans);
}
}
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... |