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 <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
class data{
public:
int x, y, rk;
data(){}
data(int _x, int _y, int _r):x(_x),y(_y),rk(_r){}
};
int arr[2005][2005], rarr[2005][2005], par[100010], psz[100010], pos[2][4] = {{0, 0, 1, -1}, {1, -1, 0, 0}};
queue<data> que;
int getPar(int x){
if(par[x] == x) return x;
return par[x] = getPar(par[x]);
}
int main(){
int n, k, a, b;
scanf("%d %d", &n, &k);
for(int i=1; i<=k; i++){
scanf("%d %d", &a, &b);
par[i] = i;
psz[i] = 1;
arr[a][b] = i;
rarr[a][b] = 1;
que.push({a, b, 1});
}
while(!que.empty()){
int x = que.front().x, y = que.front().y, rk = que.front().rk;
que.pop();
for(int i=0; i<4; i++){
int X = x+pos[0][i], Y = y+pos[1][i];
if(X < 1 || X > n || Y < 1 || Y > n) continue;
if(rarr[X][Y] == 0){
arr[X][Y] = arr[x][y];
rarr[X][Y] = rk+1;
que.push({X, Y, rk+1});
}else{
int t = getPar(arr[X][Y]), p = getPar(arr[x][y]);
if(t != p){
int sz = psz[t] + psz[p];
par[t] = p;
psz[p] += psz[t];
psz[t] = 0;
if(sz == k){
long long ans = max(rarr[X][Y], rarr[x][y])-1;
printf("%lld", ans);
return 0;
}
}
}
}
}
return 0;
}
Compilation message (stderr)
civilization.cpp: In function 'int main()':
civilization.cpp:21: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:23: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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |