#include <bits/stdc++.h>
int N, K;
int stk[4000010][2];
int chk[2010][2010];
int xx[]={0,0,1,-1}, yy[]={1,-1,0,0};
struct Union_Find
{
std::vector<int> parent, rank;
Union_Find() { for(int i=0; i<100010; i++) parent.push_back(i), rank.push_back(1);}
int find(int u) { return u==parent[u]?u:parent[u]=find(parent[u]);}
int merge(int u, int v) {
u=find(u), v=find(v);
if(u==v)return 0;
if(rank[u]>rank[v]) std::swap(u,v);
parent[u]=v;
if(rank[u]==rank[v]) ++rank[v];
return 1;
}
};
int check(int x, int y)
{
if (!x||!y||x>N||y>N) return 1;
return 0;
}
int main()
{
scanf("%d %d", &N, &K);
Union_Find civilization;
int i, j, x, y;
int top=0;
for (i=0; i<K; i++) {
scanf("%d %d", &x, &y);
chk[x][y]=i+1;
stk[top][0]=x, stk[top++][1]=y;
}
int fr, re;
fr=0, re=top;
int cnt=K, k=0, l;
for(i=0; i<re; i++) {
for (j=0; j<4; j++) {
if (check(stk[i][0]+xx[j],stk[i][1]+yy[j])) continue;
if (chk[stk[i][0]+xx[j]][stk[i][1]+yy[j]]) cnt-=civilization.merge(chk[stk[i][0]+xx[j]][stk[i][1]+yy[j]],chk[stk[i][0]][stk[i][1]]);
}
}
if (cnt==1) {printf("0"); return 0;}
while(fr<re) {
k++;
for (i=fr; i<re; i++) {
for (j=0; j<4; j++) {
if (check(stk[i][0]+xx[j],stk[i][1]+yy[j])) continue;
if (chk[stk[i][0]+xx[j]][stk[i][1]+yy[j]]) {
cnt-=civilization.merge(chk[stk[i][0]][stk[i][1]], chk[stk[i][0]+xx[j]][stk[i][1]+yy[j]]);
}
else {
chk[stk[i][0]+xx[j]][stk[i][1]+yy[j]]=chk[stk[i][0]][stk[i][1]];
stk[top][0]=stk[i][0]+xx[j], stk[top][1]=stk[i][1]+yy[j];
for (l=0; l<4; l++) {
if (check(stk[top][0]+xx[l],stk[top][1]+yy[l])) continue;
if (chk[stk[top][0]+xx[l]][stk[top][1]+yy[l]]) {
cnt-=civilization.merge(chk[stk[top][0]][stk[top][1]], chk[stk[top][0]+xx[l]][stk[top][1]+yy[l]]);
}
}
top++;
}
}
}
if (cnt==1) {
printf("%d", k);
return 0;
}
fr=re; re=top;
}
return 0;
}
Compilation message
civilization.cpp: In function 'int main()':
civilization.cpp:27:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~~
civilization.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1772 KB |
Output is correct |
2 |
Correct |
4 ms |
1772 KB |
Output is correct |
3 |
Correct |
5 ms |
1916 KB |
Output is correct |
4 |
Correct |
5 ms |
1916 KB |
Output is correct |
5 |
Correct |
4 ms |
2068 KB |
Output is correct |
6 |
Correct |
5 ms |
2092 KB |
Output is correct |
7 |
Correct |
5 ms |
2092 KB |
Output is correct |
8 |
Correct |
6 ms |
2092 KB |
Output is correct |
9 |
Correct |
6 ms |
2092 KB |
Output is correct |
10 |
Correct |
3 ms |
2092 KB |
Output is correct |
11 |
Correct |
4 ms |
2136 KB |
Output is correct |
12 |
Correct |
6 ms |
2136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1772 KB |
Output is correct |
2 |
Correct |
4 ms |
1772 KB |
Output is correct |
3 |
Correct |
5 ms |
1916 KB |
Output is correct |
4 |
Correct |
5 ms |
1916 KB |
Output is correct |
5 |
Correct |
4 ms |
2068 KB |
Output is correct |
6 |
Correct |
5 ms |
2092 KB |
Output is correct |
7 |
Correct |
5 ms |
2092 KB |
Output is correct |
8 |
Correct |
6 ms |
2092 KB |
Output is correct |
9 |
Correct |
6 ms |
2092 KB |
Output is correct |
10 |
Correct |
3 ms |
2092 KB |
Output is correct |
11 |
Correct |
4 ms |
2136 KB |
Output is correct |
12 |
Correct |
72 ms |
16180 KB |
Output is correct |
13 |
Correct |
50 ms |
16180 KB |
Output is correct |
14 |
Correct |
62 ms |
16180 KB |
Output is correct |
15 |
Correct |
49 ms |
16180 KB |
Output is correct |
16 |
Correct |
10 ms |
16180 KB |
Output is correct |
17 |
Correct |
185 ms |
17512 KB |
Output is correct |
18 |
Correct |
176 ms |
18936 KB |
Output is correct |
19 |
Correct |
272 ms |
19700 KB |
Output is correct |
20 |
Correct |
293 ms |
20448 KB |
Output is correct |
21 |
Correct |
257 ms |
21112 KB |
Output is correct |
22 |
Correct |
5 ms |
21112 KB |
Output is correct |
23 |
Correct |
72 ms |
21272 KB |
Output is correct |
24 |
Correct |
6 ms |
2136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1772 KB |
Output is correct |
2 |
Correct |
4 ms |
1772 KB |
Output is correct |
3 |
Correct |
5 ms |
1916 KB |
Output is correct |
4 |
Correct |
5 ms |
1916 KB |
Output is correct |
5 |
Correct |
4 ms |
2068 KB |
Output is correct |
6 |
Correct |
5 ms |
2092 KB |
Output is correct |
7 |
Correct |
5 ms |
2092 KB |
Output is correct |
8 |
Correct |
6 ms |
2092 KB |
Output is correct |
9 |
Correct |
6 ms |
2092 KB |
Output is correct |
10 |
Correct |
3 ms |
2092 KB |
Output is correct |
11 |
Correct |
4 ms |
2136 KB |
Output is correct |
12 |
Correct |
72 ms |
16180 KB |
Output is correct |
13 |
Correct |
50 ms |
16180 KB |
Output is correct |
14 |
Correct |
62 ms |
16180 KB |
Output is correct |
15 |
Correct |
49 ms |
16180 KB |
Output is correct |
16 |
Correct |
10 ms |
16180 KB |
Output is correct |
17 |
Correct |
185 ms |
17512 KB |
Output is correct |
18 |
Correct |
176 ms |
18936 KB |
Output is correct |
19 |
Correct |
272 ms |
19700 KB |
Output is correct |
20 |
Correct |
293 ms |
20448 KB |
Output is correct |
21 |
Correct |
257 ms |
21112 KB |
Output is correct |
22 |
Correct |
5 ms |
21112 KB |
Output is correct |
23 |
Correct |
72 ms |
21272 KB |
Output is correct |
24 |
Correct |
203 ms |
38956 KB |
Output is correct |
25 |
Correct |
229 ms |
44720 KB |
Output is correct |
26 |
Correct |
121 ms |
44720 KB |
Output is correct |
27 |
Correct |
170 ms |
44720 KB |
Output is correct |
28 |
Correct |
101 ms |
44720 KB |
Output is correct |
29 |
Correct |
732 ms |
52048 KB |
Output is correct |
30 |
Correct |
664 ms |
52848 KB |
Output is correct |
31 |
Correct |
915 ms |
53432 KB |
Output is correct |
32 |
Correct |
842 ms |
54216 KB |
Output is correct |
33 |
Correct |
981 ms |
55116 KB |
Output is correct |
34 |
Correct |
221 ms |
55116 KB |
Output is correct |
35 |
Correct |
6 ms |
55116 KB |
Output is correct |
36 |
Correct |
6 ms |
2136 KB |
Output is correct |