| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1364270 | kai | Tracks in the Snow (BOI13_tracks) | C++20 | 1154 ms | 308548 KiB |
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define el '\n'
#define all(x) begin(x), end(x)
#define sz(s) (int)(s.size())
inline void setIO(string s) {
if(fopen((s + ".in").c_str(), "r")){
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
}
const int MOD = 1e9+7; //998244353;
const int N = 4000 + 36;
int h, w;
char a[N][N];
bool vis[N][N];
int root[N][N], dist[N*N];
int comp = 1;
int dx[4] = {-1, 0, 1, 0},
dy[4] = {0, 1, 0, -1};
void Solve(){
cin>> h>> w;
for(int i=1; i<=h; i++) for(int j=1; j<=w; j++) cin>> a[i][j];
for(int i=1; i<=h; i++){
for(int j=1; j<=w; j++){
if(root[i][j] || a[i][j] == '.') continue;
// cout<< root[i][j]<<el;
queue<pair<int, int>> q;
q.push({i, j});
root[i][j] = comp;
while(sz(q)){
auto [x, y] = q.front(); q.pop();
for(int k=0; k<4; k++){
int nx = x + dx[k];
int ny = y + dy[k];
// cerr<<x<<" "<<y<<' '<<nx<<" "<<ny<<' '<<a[x][y]<<' '<<a[nx][ny]<<' '<<root[x][y]<<el;
if(nx >= 1 && nx <= h && ny >= 1 && ny <= w && a[x][y] == a[nx][ny] && !root[nx][ny]){
q.push({nx, ny});
root[nx][ny] = comp;
}
}
}
comp++;
}
}
// for(int i=1; i<=h; i++){
// for(int j=1; j<=w; j++) cout<< root[i][j]<<' ';
// cout<<el;
// }
vector<vector<int>> adj(comp);
for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++){
if(root[i][j] == 0) continue;
int u = root[i][j];
for(int k = 0; k < 4; k++){
int ni = i + dx[k];
int nj = j + dy[k];
if(ni < 1 || ni > h || nj < 1 || nj > w) continue;
if(root[ni][nj] == 0) continue;
int v = root[ni][nj];
if(u != v){
adj[u].push_back(v);
}
}
}
}
for(int i=1; i<comp; i++){
// cout<< i<<": ";
sort(all(adj[i]));
adj[i].erase(unique(all(adj[i])), adj[i].end());
// for(int j: adj[i]) cout<< j<<' '; cout<<el;
}
queue<int> q;
q.push(1);
dist[1] = 1;
while(sz(q)){
int u = q.front(); q.pop();
for(int v: adj[u]){
if(dist[v]) continue;
dist[v] = dist[u] + 1;
q.push(v);
}
}
int ans = 0;
for(int i=1; i<comp; i++) ans = max(ans, dist[i]);
cout<< ans<<el;
}
signed main (){
setIO("Rem");
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T(1);
// cin >> T;
while(T--) Solve();
return 0;
}Compilation message (stderr)
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
