Submission #800558

#TimeUsernameProblemLanguageResultExecution timeMemory
800558synthesisTracks in the Snow (BOI13_tracks)C++17
100 / 100
1874 ms716952 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;
using ll = long long int;
using ull = unsigned long long int;

constexpr ll mod = 1e9 + 7;
constexpr ll INF = LONG_LONG_MAX;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int main()
{
    //tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> q;
    /*freopen("problemname.in", "r", stdin);
    freopen("problemname.out", "w", stdout);*/
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    char a[n][m];
    for(int i = 0;i<n;++i) {
        for(int j = 0;j<m;++j) {
            cin >> a[i][j];
        }
    }
    int c = -1;
    int cmp[n][m];
    memset(cmp, -1, sizeof cmp);
    bool vis[n][m];
    memset(vis, false, sizeof vis);
    for(int i = 0;i<n;++i) {
        for(int j = 0;j<m;++j) {
            if (!vis[i][j] && (a[i][j] == 'F' || a[i][j] == 'R')) {
                queue<pii> q;
                q.push({i, j});
                vis[i][j] = true;
                cmp[i][j] = ++c;
                while(!q.empty()) {
                    pii node = q.front();
                    q.pop();
                    for(int k = 0;k<4;++k) {
                        int x = node.fi + dx[k], y = node.se + dy[k];
                        if (x >= 0 && y >= 0 && x < n && y < m) {
                            if (!vis[x][y] && a[x][y] == a[i][j]) {
                                vis[x][y] = true;
                                cmp[x][y] = c;
                                q.push({x, y});
                            }
                        }
                    }
                }
            }
        }
    }
    set<int> adj[c + 1];
    memset(vis, false, sizeof vis);
    for(int i = 0;i<n;++i) {
        for(int j = 0;j<m;++j) {
            if (!vis[i][j] && (a[i][j] == 'F' || a[i][j] == 'R')) {
                queue<pii> q;
                q.push({i, j});
                vis[i][j] = true;
                while(!q.empty()) {
                    pii node = q.front();
                    q.pop();
                    for(int k = 0;k<4;++k) {
                        int x = node.fi + dx[k], y = node.se + dy[k];
                        if (x >= 0 && y >= 0 && x < n && y < m) {
                            if (!vis[x][y] && a[x][y] == a[i][j]) {
                                vis[x][y] = true;
                                q.push({x, y});
                            }
                            if (a[x][y] != a[i][j] && a[x][y] != '.') {
                                adj[cmp[i][j]].insert(cmp[x][y]);
                            }
                        }
                    }
                }
            }
        }
    }
    int d[c + 1];
    d[0] = 0;
    queue<int> q;
    q.push(0);
    bool vv[c + 1];
    memset(vv, false, sizeof vv);
    vv[0] = true;
    while(!q.empty()) {
        int node = q.front();
        q.pop();
        for(const int& v:adj[node]) {
            if (!vv[v]) {
                vv[v] = true;
                d[v] = d[node] + 1;
                q.push(v);
            }
        }
    }
    cout << *max_element(d, d + c + 1) + 1 << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...