#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
queue<pair<pair<int, int>, pair<int, int> > > q;
int pa[100001], sz[100001], rk[100001], ar[2001][2001];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int find(int u)
{
if(pa[u] == u) return u;
return pa[u] = find(pa[u]);
}
void merge(int u, int v)
{
u = find(u); v = find(v);
if(u == v) return;
if(rk[u] > rk[v]) swap(u, v);
pa[u] = v;
sz[v] += sz[u];
if(rk[u] == rk[v]) rk[v]++;
}
int main()
{
int n, k;
scanf("%d %d", &n, &k);
for(int i = 1;i <= k;i++)
{
int x, y;
scanf("%d %d", &x, &y);
q.push({{x, y}, {i, 0}});
pa[i] = i;
rk[i] = 1;
sz[i] = 1;
}
while(!q.empty())
{
int x = q.front().first.first;
int y = q.front().first.second;
int s = q.front().second.first;
int t = q.front().second.second;
q.pop();
if(ar[x][y] == 0)
{
ar[x][y] = s;
for(int i = 0;i < 4;i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 1 || nx > n || ny < 1 || ny > n) continue;
if(ar[nx][ny] == 0) q.push({{nx, ny}, {s, t + 1}});
else merge(ar[nx][ny], s);
}
}
else
{
merge(ar[x][y], s);
}
if(sz[find(1)] == k)
{
printf("%d", t);
return 0;
}
}
}
Compilation message
civilization.cpp: In function 'int main()':
civilization.cpp:29: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:33:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
976 KB |
Output is correct |
4 |
Correct |
3 ms |
976 KB |
Output is correct |
5 |
Correct |
3 ms |
992 KB |
Output is correct |
6 |
Correct |
3 ms |
992 KB |
Output is correct |
7 |
Correct |
4 ms |
992 KB |
Output is correct |
8 |
Correct |
3 ms |
992 KB |
Output is correct |
9 |
Correct |
3 ms |
1072 KB |
Output is correct |
10 |
Correct |
2 ms |
1072 KB |
Output is correct |
11 |
Correct |
3 ms |
1072 KB |
Output is correct |
12 |
Correct |
3 ms |
1072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
976 KB |
Output is correct |
4 |
Correct |
3 ms |
976 KB |
Output is correct |
5 |
Correct |
3 ms |
992 KB |
Output is correct |
6 |
Correct |
3 ms |
992 KB |
Output is correct |
7 |
Correct |
4 ms |
992 KB |
Output is correct |
8 |
Correct |
3 ms |
992 KB |
Output is correct |
9 |
Correct |
3 ms |
1072 KB |
Output is correct |
10 |
Correct |
2 ms |
1072 KB |
Output is correct |
11 |
Correct |
3 ms |
1072 KB |
Output is correct |
12 |
Correct |
71 ms |
8424 KB |
Output is correct |
13 |
Correct |
53 ms |
8424 KB |
Output is correct |
14 |
Correct |
63 ms |
8424 KB |
Output is correct |
15 |
Correct |
51 ms |
8424 KB |
Output is correct |
16 |
Correct |
11 ms |
8424 KB |
Output is correct |
17 |
Correct |
197 ms |
22200 KB |
Output is correct |
18 |
Correct |
227 ms |
22236 KB |
Output is correct |
19 |
Correct |
240 ms |
22244 KB |
Output is correct |
20 |
Correct |
244 ms |
22288 KB |
Output is correct |
21 |
Correct |
212 ms |
22288 KB |
Output is correct |
22 |
Correct |
2 ms |
22288 KB |
Output is correct |
23 |
Correct |
65 ms |
22288 KB |
Output is correct |
24 |
Correct |
3 ms |
1072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
976 KB |
Output is correct |
4 |
Correct |
3 ms |
976 KB |
Output is correct |
5 |
Correct |
3 ms |
992 KB |
Output is correct |
6 |
Correct |
3 ms |
992 KB |
Output is correct |
7 |
Correct |
4 ms |
992 KB |
Output is correct |
8 |
Correct |
3 ms |
992 KB |
Output is correct |
9 |
Correct |
3 ms |
1072 KB |
Output is correct |
10 |
Correct |
2 ms |
1072 KB |
Output is correct |
11 |
Correct |
3 ms |
1072 KB |
Output is correct |
12 |
Correct |
71 ms |
8424 KB |
Output is correct |
13 |
Correct |
53 ms |
8424 KB |
Output is correct |
14 |
Correct |
63 ms |
8424 KB |
Output is correct |
15 |
Correct |
51 ms |
8424 KB |
Output is correct |
16 |
Correct |
11 ms |
8424 KB |
Output is correct |
17 |
Correct |
197 ms |
22200 KB |
Output is correct |
18 |
Correct |
227 ms |
22236 KB |
Output is correct |
19 |
Correct |
240 ms |
22244 KB |
Output is correct |
20 |
Correct |
244 ms |
22288 KB |
Output is correct |
21 |
Correct |
212 ms |
22288 KB |
Output is correct |
22 |
Correct |
2 ms |
22288 KB |
Output is correct |
23 |
Correct |
65 ms |
22288 KB |
Output is correct |
24 |
Correct |
188 ms |
22288 KB |
Output is correct |
25 |
Correct |
320 ms |
22288 KB |
Output is correct |
26 |
Correct |
136 ms |
22288 KB |
Output is correct |
27 |
Correct |
200 ms |
22288 KB |
Output is correct |
28 |
Correct |
126 ms |
22288 KB |
Output is correct |
29 |
Correct |
759 ms |
34700 KB |
Output is correct |
30 |
Correct |
691 ms |
34700 KB |
Output is correct |
31 |
Correct |
812 ms |
42696 KB |
Output is correct |
32 |
Correct |
904 ms |
42696 KB |
Output is correct |
33 |
Correct |
922 ms |
43716 KB |
Output is correct |
34 |
Correct |
234 ms |
43716 KB |
Output is correct |
35 |
Correct |
3 ms |
43716 KB |
Output is correct |
36 |
Correct |
3 ms |
1072 KB |
Output is correct |