#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
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);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1144 KB |
Output is correct |
2 |
Correct |
2 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1272 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
2 ms |
1272 KB |
Output is correct |
8 |
Correct |
2 ms |
1272 KB |
Output is correct |
9 |
Correct |
3 ms |
1272 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
1144 KB |
Output is correct |
12 |
Correct |
3 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1144 KB |
Output is correct |
2 |
Correct |
2 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1272 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
2 ms |
1272 KB |
Output is correct |
8 |
Correct |
2 ms |
1272 KB |
Output is correct |
9 |
Correct |
3 ms |
1272 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
1144 KB |
Output is correct |
12 |
Correct |
57 ms |
15780 KB |
Output is correct |
13 |
Correct |
41 ms |
14200 KB |
Output is correct |
14 |
Correct |
52 ms |
15068 KB |
Output is correct |
15 |
Correct |
40 ms |
13016 KB |
Output is correct |
16 |
Correct |
8 ms |
4216 KB |
Output is correct |
17 |
Correct |
206 ms |
21800 KB |
Output is correct |
18 |
Correct |
216 ms |
21872 KB |
Output is correct |
19 |
Correct |
193 ms |
21880 KB |
Output is correct |
20 |
Correct |
181 ms |
21752 KB |
Output is correct |
21 |
Correct |
219 ms |
21856 KB |
Output is correct |
22 |
Correct |
3 ms |
508 KB |
Output is correct |
23 |
Correct |
43 ms |
16124 KB |
Output is correct |
24 |
Correct |
3 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1144 KB |
Output is correct |
2 |
Correct |
2 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1272 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
2 ms |
1272 KB |
Output is correct |
8 |
Correct |
2 ms |
1272 KB |
Output is correct |
9 |
Correct |
3 ms |
1272 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
1144 KB |
Output is correct |
12 |
Correct |
57 ms |
15780 KB |
Output is correct |
13 |
Correct |
41 ms |
14200 KB |
Output is correct |
14 |
Correct |
52 ms |
15068 KB |
Output is correct |
15 |
Correct |
40 ms |
13016 KB |
Output is correct |
16 |
Correct |
8 ms |
4216 KB |
Output is correct |
17 |
Correct |
206 ms |
21800 KB |
Output is correct |
18 |
Correct |
216 ms |
21872 KB |
Output is correct |
19 |
Correct |
193 ms |
21880 KB |
Output is correct |
20 |
Correct |
181 ms |
21752 KB |
Output is correct |
21 |
Correct |
219 ms |
21856 KB |
Output is correct |
22 |
Correct |
3 ms |
508 KB |
Output is correct |
23 |
Correct |
43 ms |
16124 KB |
Output is correct |
24 |
Correct |
149 ms |
30840 KB |
Output is correct |
25 |
Correct |
204 ms |
31792 KB |
Output is correct |
26 |
Correct |
123 ms |
22392 KB |
Output is correct |
27 |
Correct |
143 ms |
31224 KB |
Output is correct |
28 |
Correct |
93 ms |
20464 KB |
Output is correct |
29 |
Correct |
789 ms |
39264 KB |
Output is correct |
30 |
Correct |
550 ms |
34808 KB |
Output is correct |
31 |
Correct |
895 ms |
42640 KB |
Output is correct |
32 |
Correct |
930 ms |
42508 KB |
Output is correct |
33 |
Correct |
838 ms |
42632 KB |
Output is correct |
34 |
Correct |
147 ms |
31732 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
3 ms |
1272 KB |
Output is correct |