답안 #394829

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
394829 2021-04-27T10:43:59 Z jk410 문명 (KOI17_civilization) C++17
100 / 100
813 ms 24392 KB
#include <bits/stdc++.h>
using namespace std;
struct point{
    int x,y;
};
int N,K,cnt;
int A[2001][2001],Par[100001],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
point Point[100001];
queue<point> Q;
int getpar(int v){
    if (v==Par[v])
        return v;
    return Par[v]=getpar(Par[v]);
}
void unite(int a,int b){
    a=getpar(a);
    b=getpar(b);
    if (a!=b){
        Par[b]=a;
        cnt--;
    }
}
bool f(int x,int y){
    return 0<x&&x<=N&&0<y&&y<=N;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>N>>K;
    cnt=K;
    for (int i=1; i<=K; i++){
        cin>>Point[i].x>>Point[i].y;
        A[Point[i].x][Point[i].y]=i;
        Q.push({Point[i].x,Point[i].y});
        Par[i]=i;
    }
    for (int ans=0;;ans++){
        int sz=Q.size();
        while (sz--){
            point t=Q.front();
            Q.pop();
            for (int i=0; i<4; i++){
                int x=t.x+dx[i],y=t.y+dy[i];
                if (f(x,y)&&A[x][y]){
                    unite(A[t.x][t.y],A[x][y]);
                    if (cnt==1){
                        cout<<ans;
                        return 0;
                    }
                }
            }
            Q.push(t);
        }
        sz=Q.size();
        while (sz--){
            point t=Q.front();
            Q.pop();
            for (int i=0; i<4; i++){
                int x=t.x+dx[i],y=t.y+dy[i];
                if (f(x,y)&&!A[x][y]){
                    A[x][y]=A[t.x][t.y];
                    Q.push({x,y});
                }
            }
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 716 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 716 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 720 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 1 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 716 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 716 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 720 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 1 ms 716 KB Output is correct
13 Correct 51 ms 7972 KB Output is correct
14 Correct 37 ms 7168 KB Output is correct
15 Correct 57 ms 7724 KB Output is correct
16 Correct 35 ms 6448 KB Output is correct
17 Correct 6 ms 2240 KB Output is correct
18 Correct 229 ms 12892 KB Output is correct
19 Correct 162 ms 12996 KB Output is correct
20 Correct 156 ms 13088 KB Output is correct
21 Correct 152 ms 12896 KB Output is correct
22 Correct 169 ms 13104 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 61 ms 8152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 716 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 716 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 720 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 51 ms 7972 KB Output is correct
13 Correct 37 ms 7168 KB Output is correct
14 Correct 57 ms 7724 KB Output is correct
15 Correct 35 ms 6448 KB Output is correct
16 Correct 6 ms 2240 KB Output is correct
17 Correct 229 ms 12892 KB Output is correct
18 Correct 162 ms 12996 KB Output is correct
19 Correct 156 ms 13088 KB Output is correct
20 Correct 152 ms 12896 KB Output is correct
21 Correct 169 ms 13104 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 61 ms 8152 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 166 ms 15484 KB Output is correct
26 Correct 201 ms 15964 KB Output is correct
27 Correct 111 ms 11304 KB Output is correct
28 Correct 154 ms 15648 KB Output is correct
29 Correct 88 ms 10292 KB Output is correct
30 Correct 724 ms 21572 KB Output is correct
31 Correct 616 ms 18244 KB Output is correct
32 Correct 804 ms 24392 KB Output is correct
33 Correct 813 ms 24368 KB Output is correct
34 Correct 784 ms 24316 KB Output is correct
35 Correct 201 ms 16020 KB Output is correct
36 Correct 1 ms 332 KB Output is correct