Submission #1364266

#TimeUsernameProblemLanguageResultExecution timeMemory
1364266kaiTracks in the Snow (BOI13_tracks)C++20
56.04 / 100
1152 ms321056 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];
vector<int> adj[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 bfs(){
    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);
        }
    }
}


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;
    // }

    
    // cout<< comp<<el;
    queue<pair<int, int>> q;
    q.push({1, 1});
    vis[1][1] = 1;
    while(sz(q)){
        auto [x, y] = q.front(); q.pop();
        
        int u = root[x][y];
        for(int k=0; k<4; k++){
            int nx = x + dx[k];
            int ny = y + dy[k];
            
            // cout<< u<<": "<<x<<' '<<y<<el;
            // cout<< nx<<' '<<ny<<el;
            if(nx < 1 || nx > h || ny < 1 || ny > w  || root[nx][ny] == 0 || vis[nx][ny]) continue;
            
            int v = root[nx][ny];
            if(u != v){
                adj[u].push_back(v);
                adj[v].push_back(u);
            }

            vis[nx][ny] = 1;
            q.push({nx, ny});
        }
    }

    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;
    }

    bfs();
    
    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)

tracks.cpp: In function 'void setIO(std::string)':
tracks.cpp:12:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |         freopen((s + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:13:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |         freopen((s + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...