Submission #783152

# Submission time Handle Problem Language Result Execution time Memory
783152 2023-07-14T16:04:30 Z serifefedartar Zoo (COCI19_zoo) C++17
0 / 110
299 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MAXN 100005

vector<vector<int>> tree;
vector<int> depth;
char mark[1005][1005], grid[1005][1005];
int now = 1, R, C;
int ans = 1;
bool vis[1005][1005];
void dfs2(int node, int parent) {
    for (auto u : tree[node]) {
        if (u == parent)
            continue;
        depth[u] = depth[node] + 1;
        ans = max(ans, depth[u]);
        dfs2(u, node);
    }
}

void dfs(int i, int j) {
    vis[i][j] = true;
    mark[i][j] = now;
    for (int delRow = -1; delRow <= 1; delRow++) {
        for (int delCol = -1; delCol <= 1; delCol++) {
            if ((delCol == 0 && delRow == 0) || (delCol != 0 && delRow != 0))
                continue;
            int nRow = delRow + i;
            int nCol = delCol + j;
            if (nRow <= 0 || nRow > R || nCol <= 0 || nCol > C 
                || vis[nRow][nCol] || grid[nRow][nCol] != grid[i][j])
                continue;
            dfs(nRow, nCol);
        }
    }
}

int main() {
    fast
    memset(vis, 0, sizeof(vis));
    memset(mark, -1, sizeof(mark));
    cin >> R >> C;

    for (int i = 1; i <= R; i++) {
        string s;
        cin >> s;
        for (int j = 1; j <= C; j++)
            grid[i][j] = s[j-1];
    }
    
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            if (vis[i][j] || grid[i][j] == '*')
                continue;
            dfs(i, j);
            now++;
        }
    }

    depth = vector<int>(now+1, 1);
    tree = vector<vector<int>>(now, vector<int>());
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            for (int delRow = -1; delRow <= 1; delRow++) {
                for (int delCol = -1; delCol <= 1; delCol++) {
                    if ((delCol == 0 && delRow == 0) || (delCol != 0 && delRow != 0))
                        continue;
                    int nRow = delRow + i;
                    int nCol = delCol + j;
                    if (nRow <= 0 || nRow > R || nCol <= 0 || nCol > C 
                        || mark[nRow][nCol] == mark[i][j])
                        continue;
                    int from = mark[nRow][nCol];
                    int to = mark[i][j];
                    if (from == -1 || to == -1)
                        continue;
                    
                    bool control = true;
                    for (auto u : tree[from])
                        if (u == to)
                            control = false;

                    if (control) {
                        tree[from].push_back(to);
                        tree[to].push_back(from);
                    }
                }
            }
        }
    }
    dfs2(1, -1);
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Runtime error 299 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 299 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -