제출 #1139525

#제출 시각아이디문제언어결과실행 시간메모리
1139525anmattroiTracks in the Snow (BOI13_tracks)C++17
2.19 / 100
840 ms614916 KiB
#include <bits/stdc++.h> #define maxn 4005 #define fi first #define se second using namespace std; using ii = pair<int, int>; int n, m; char a[maxn][maxn]; int cl[maxn][maxn]; int dv[4] = {0, 1, 0, -1}; int dh[4] = {1, 0, -1, 0}; int par[maxn*maxn]; int nho[maxn*maxn], id = 0; int find_set(int v) { return par[v] < 0 ? v : par[v] = find_set(par[v]); } void union_set(int u, int v) { u = find_set(u); v = find_set(v); if (u == v) return; if (par[u] < par[v]) swap(u, v); par[v] += par[u]; par[u] = v; } bool ok(int i, int j) { return (1 <= i && i <= n && 1 <= j && j <= m); } int rem[maxn*maxn]; vector<int> adj[maxn*maxn]; void bfs(int ox, int oy) { queue<ii> q; q.push(ii{ox, oy}); cl[ox][oy] = ++id; vector<int> nodes; while (!q.empty()) { auto [u, v] = q.front(); q.pop(); for (int k = 0; k < 4; k++) { int i = u + dv[k], j = v + dh[k]; if (!ok(i, j)) continue; if (a[i][j] != a[u][v] && a[i][j] != '.') { if (cl[i][j] && !rem[cl[i][j]]) { rem[cl[i][j]] = 1; adj[cl[i][j]].emplace_back(id); adj[id].emplace_back(cl[i][j]); } } else if (a[i][j] == a[u][v] && !cl[i][j]) { cl[i][j] = id; q.push(ii{i, j}); } } } for (int i : nodes) rem[i] = 0; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; fill(par+1, par+n*m+1, -1); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] != '.' && !cl[i][j]) bfs(i, j); queue<int> q; nho[1] = 1; q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) if (!nho[v]) { nho[v] = nho[u]+1; q.push(v); } } cout << *max_element(nho + 1, nho + id + 1); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...