답안 #158695

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
158695 2019-10-18T12:54:05 Z solarmagic 문명 (KOI17_civilization) C++17
100 / 100
984 ms 64496 KB
#include <bits/stdc++.h>
using namespace std;

int N, M;
int p[100001];
int world[2001][2001];
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int find(int u) {
    if (p[u] == u) return u;
    return p[u] = find(p[u]);
}
void merge(int u, int v) {
    u = find(u);
    v = find(v);
    p[u] = v;
}
bool isIn(int x, int y) { return x > 0 && x <= N && y > 0 && y <= N; }
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    queue<pair<int, int>> q;
    cin >> N >> M;
    for (int i = 1; i <= M; i++) {
        int x, y; cin >> x >> y;
        world[x][y] = i;
        q.push({x,y});
        p[i] = i;
    }

    int ans = 0;
    while (true) {
        vector<tuple<int,int,int>> v;
        while (!q.empty()) {
            auto [x,y] = q.front();
            q.pop();
            for (int d = 0; d < 4; d++) {
                int nx = x + dx[d];
                int ny = y + dy[d];
                if (isIn(nx,ny)) {
                    if (world[nx][ny]) {
                        if (find(world[nx][ny]) != find(world[x][y])) {
                            merge(world[nx][ny], world[x][y]); 
                            M--;
                        }
                    } else {
                        v.push_back({nx,ny,world[x][y]});
                    }
                }
            }
        }
        if (M == 1) break;
        ++ans;
        for (auto [x, y, w] : v) {
            if (world[x][y]) {
                if (find(world[x][y]) != find(w)) {
                    merge(world[x][y], w);
                    M--;
                }
            } else {
                q.push({x, y});
            }
            world[x][y] = find(w);
        }
    }
    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 760 KB Output is correct
2 Correct 3 ms 760 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 3 ms 760 KB Output is correct
5 Correct 3 ms 888 KB Output is correct
6 Correct 3 ms 888 KB Output is correct
7 Correct 3 ms 888 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
9 Correct 3 ms 888 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 760 KB Output is correct
12 Correct 3 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 760 KB Output is correct
2 Correct 3 ms 760 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 3 ms 760 KB Output is correct
5 Correct 3 ms 888 KB Output is correct
6 Correct 3 ms 888 KB Output is correct
7 Correct 3 ms 888 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
9 Correct 3 ms 888 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 760 KB Output is correct
12 Correct 61 ms 8112 KB Output is correct
13 Correct 52 ms 7452 KB Output is correct
14 Correct 55 ms 7912 KB Output is correct
15 Correct 42 ms 6760 KB Output is correct
16 Correct 9 ms 2424 KB Output is correct
17 Correct 222 ms 30380 KB Output is correct
18 Correct 241 ms 30492 KB Output is correct
19 Correct 230 ms 30284 KB Output is correct
20 Correct 228 ms 30404 KB Output is correct
21 Correct 235 ms 30276 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 47 ms 8312 KB Output is correct
24 Correct 3 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 760 KB Output is correct
2 Correct 3 ms 760 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 3 ms 760 KB Output is correct
5 Correct 3 ms 888 KB Output is correct
6 Correct 3 ms 888 KB Output is correct
7 Correct 3 ms 888 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
9 Correct 3 ms 888 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 760 KB Output is correct
12 Correct 61 ms 8112 KB Output is correct
13 Correct 52 ms 7452 KB Output is correct
14 Correct 55 ms 7912 KB Output is correct
15 Correct 42 ms 6760 KB Output is correct
16 Correct 9 ms 2424 KB Output is correct
17 Correct 222 ms 30380 KB Output is correct
18 Correct 241 ms 30492 KB Output is correct
19 Correct 230 ms 30284 KB Output is correct
20 Correct 228 ms 30404 KB Output is correct
21 Correct 235 ms 30276 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 47 ms 8312 KB Output is correct
24 Correct 205 ms 15980 KB Output is correct
25 Correct 259 ms 16376 KB Output is correct
26 Correct 149 ms 11592 KB Output is correct
27 Correct 222 ms 16104 KB Output is correct
28 Correct 124 ms 10772 KB Output is correct
29 Correct 791 ms 45568 KB Output is correct
30 Correct 629 ms 29828 KB Output is correct
31 Correct 965 ms 64496 KB Output is correct
32 Correct 981 ms 64292 KB Output is correct
33 Correct 984 ms 64476 KB Output is correct
34 Correct 167 ms 16280 KB Output is correct
35 Correct 3 ms 504 KB Output is correct
36 Correct 3 ms 632 KB Output is correct