Submission #1133386

#TimeUsernameProblemLanguageResultExecution timeMemory
1133386lopkusTracks in the Snow (BOI13_tracks)C++20
0 / 100
112 ms24036 KiB
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 501 * 501;

struct DSU{
    int P[N];
    int siz[N];
    void add(int v){
        P[v]=v;
        siz[v]=1;
        return;
    }
    int parent(int x){
        if(x==P[x])return x;
        return P[x]=parent(P[x]);
    }
    void connect(int u,int v){
        u=parent(u);
        v=parent(v);
        if(u!=v){
            if(siz[u]<siz[v])swap(u,v);
            P[v]=u;
            siz[u]+=siz[v];
        }
        return;
    }
}dsu1, dsu2;

int n, m;

int node(int i, int j) {
    return i * n + j;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    char a[n + 1][m + 1];
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    for(int i = 1; i <= n * m; i++) {
        dsu1.add(i);
        dsu2.add(i);
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] != '.') {
                if(j - 1 > 0 && a[i][j - 1] != '.') {
                    dsu1.connect(node(i, j), node(i, j - 1));
                }
                if(j + 1 <= m && a[i][j + 1] != '.') {
                    dsu1.connect(node(i, j), node(i, j + 1));
                }
                if(i - 1 > 0 && a[i - 1][j] != '.') {
                    dsu1.connect(node(i, j), node(i - 1, j));
                }
                if(i + 1 <= n && a[i + 1][j] != '.') {
                    dsu1.connect(node(i, j), node(i + 1, j));
                }
            }
        }
    }
    set<int> ans1;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] != '.') {
                ans1.insert(dsu1.parent(node(i, j)));
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == 'F') {
                if(j - 1 > 0 && a[i][j - 1] == 'F') {
                    dsu2.connect(node(i, j), node(i, j - 1));
                }
                if(j + 1 <= m && a[i][j + 1] == 'F') {
                    dsu2.connect(node(i, j), node(i, j + 1));
                }
                if(i - 1 > 0 && a[i - 1][j] == 'F') {
                    dsu2.connect(node(i, j), node(i - 1, j));
                }
                if(i + 1 <= n && a[i + 1][j] == 'F') {
                    dsu2.connect(node(i, j), node(i + 1, j));
                }
            }
        }
    }
    set<int> ans2;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i][j] == 'F') {
                ans2.insert(dsu2.parent(node(i, j)));
            }
        }
    }
    cout << ans1.size() + ans2.size();
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...