답안 #491743

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
491743 2021-12-04T08:50:43 Z hoanghq2004 Zoo (COCI19_zoo) C++14
110 / 110
224 ms 49732 KB
#include <bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

using namespace std;

const int Nmax = 1010;

int n, m;
char c[Nmax][Nmax];
int par[Nmax * Nmax], sz[Nmax * Nmax], d[Nmax * Nmax];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
vector <int> g[Nmax * Nmax];

int Find(int u) {
    return (u == par[u] ? u : par[u] = Find(par[u]));
}

void Union(int u, int v) {
    if ((u = Find(u)) == (v = Find(v))) return;
    if (sz[u] < sz[v]) swap(u, v);
    par[v] = u;
    sz[u] += sz[v];
}

int main() {
    ios :: sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            cin >> c[i][j];
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j) par[i * m + j] = i * m + j, sz[i * m + j] = 1;
    for (int u = 0; u < n; ++u)
        for (int v = 0; v < m; ++v) {
            if (c[u][v] == '*') continue;
            for (int i = 0; i < 4; ++i) {
                int x = u + dx[i], y = v + dy[i];
                if (x < 0 || x >= n || y < 0 || y >= m || c[x][y] == '*') continue;
                if (c[x][y] == c[u][v]) Union(x * m + y, u * m + v);
            }
        }
    for (int u = 0; u < n; ++u)
        for (int v = 0; v < m; ++v) {
            if (c[u][v] == '*') continue;
            for (int i = 0; i < 4; ++i) {
                int x = u + dx[i], y = v + dy[i];
                if (x < 0 || x >= n || y < 0 || y >= m || c[x][y] == '*') continue;
                if (c[x][y] != c[u][v]) {
                    g[Find(x * m + y)].push_back(Find(u * m + v));
                    g[Find(u * m + v)].push_back(Find(x * m + y));
                }
            }
        }
    queue <int> q;
    q.push(Find(0));
    for (int i = 0; i < n * m; ++i) d[i] = -1;
    d[Find(0)] = 1;
    while (q.size()) {
        int u = q.front();
        q.pop();
        for (auto v: g[u]) {
            if (d[v] != -1) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    cout << *max_element(d, d + n * m);
}

Compilation message

zoo.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("O3")
      | 
zoo.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    4 | #pragma GCC optimization ("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24268 KB Output is correct
2 Correct 13 ms 24276 KB Output is correct
3 Correct 12 ms 24272 KB Output is correct
4 Correct 14 ms 24404 KB Output is correct
5 Correct 13 ms 24472 KB Output is correct
6 Correct 14 ms 24524 KB Output is correct
7 Correct 13 ms 24516 KB Output is correct
8 Correct 14 ms 24452 KB Output is correct
9 Correct 15 ms 24524 KB Output is correct
10 Correct 14 ms 24540 KB Output is correct
11 Correct 13 ms 24524 KB Output is correct
12 Correct 14 ms 24524 KB Output is correct
13 Correct 13 ms 24524 KB Output is correct
14 Correct 14 ms 24500 KB Output is correct
15 Correct 13 ms 24524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24268 KB Output is correct
2 Correct 13 ms 24276 KB Output is correct
3 Correct 12 ms 24272 KB Output is correct
4 Correct 14 ms 24404 KB Output is correct
5 Correct 13 ms 24472 KB Output is correct
6 Correct 14 ms 24524 KB Output is correct
7 Correct 13 ms 24516 KB Output is correct
8 Correct 14 ms 24452 KB Output is correct
9 Correct 15 ms 24524 KB Output is correct
10 Correct 14 ms 24540 KB Output is correct
11 Correct 13 ms 24524 KB Output is correct
12 Correct 14 ms 24524 KB Output is correct
13 Correct 13 ms 24524 KB Output is correct
14 Correct 14 ms 24500 KB Output is correct
15 Correct 13 ms 24524 KB Output is correct
16 Correct 42 ms 37748 KB Output is correct
17 Correct 38 ms 37580 KB Output is correct
18 Correct 38 ms 38220 KB Output is correct
19 Correct 44 ms 38212 KB Output is correct
20 Correct 46 ms 37720 KB Output is correct
21 Correct 193 ms 48464 KB Output is correct
22 Correct 194 ms 48584 KB Output is correct
23 Correct 180 ms 48696 KB Output is correct
24 Correct 197 ms 49732 KB Output is correct
25 Correct 224 ms 49304 KB Output is correct
26 Correct 190 ms 49044 KB Output is correct
27 Correct 188 ms 48416 KB Output is correct
28 Correct 175 ms 48324 KB Output is correct
29 Correct 199 ms 49568 KB Output is correct
30 Correct 192 ms 49324 KB Output is correct