Submission #8536205


Source Code Expand

Copy
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
template <class T, class U> inline void chmax(T & a, U const & b) { a = max<T>(a, b); }
template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); }

vector<vector<int> > opposite_graph(vector<vector<int> > const & g) {
    int n = g.size();
    vector<vector<int> > h(n);
    REP (i, n) for (int j : g[i]) h[j].push_back(i);
    return h;
}

/**
 * @brief strongly connected components decomposition, Kosaraju's algorithm
 * @return the pair (the number k of components, the function from vertices of g to components)
 * @note O(V + E)
 */
pair<int, vector<int> > decompose_to_strongly_connected_components(vector<vector<int> > const & g, vector<vector<int> > const & g_rev) {
    int n = g.size();
    vector<int> acc(n); {
        vector<bool> used(n);
        function<void (int)> dfs = [&](int i) {
            used[i] = true;
            for (int j : g[i]) if (not used[j]) dfs(j);
            acc.push_back(i);
        };
        REP (i,n) if (not used[i]) dfs(i);
        reverse(ALL(acc));
    }
    int size = 0;
    vector<int> component_of(n); {
        vector<bool> used(n);
        function<void (int)> rdfs = [&](int i) {
            used[i] = true;
            component_of[i] = size;
            for (int j : g_rev[i]) if (not used[j]) rdfs(j);
        };
        for (int i : acc) if (not used[i]) {
            rdfs(i);
            ++ size;
        }
    }
    return { size, move(component_of) };
}

/**
 * @return a tree in many cases
 */
vector<vector<int> > decomposed_graph(int size, vector<int> const & component_of, vector<vector<int> > const & g) {
    int n = g.size();
    vector<vector<int> > h(size);
    REP (i, n) for (int j : g[i]) {
        if (component_of[i] != component_of[j]) {
            h[component_of[i]].push_back(component_of[j]);
        }
    }
    REP (k, size) {
        sort(ALL(h[k]));
        h[k].erase(unique(ALL(h[k])), h[k].end());
    }
    return h;
}


int main() {
    // read blocks
    int h, w; cin >> h >> w;
    vector<string> f(h);
    REP (y, h) {
        cin >> f[y];
    }

    // split into pieces
    int num_piece = 0;
    vector<vector<int> > piece = make_table(h, w, -1);
    function<void (int, int)> go = [&](int y, int x) {
        piece[y][x] = num_piece;
        REP (i, 4) {
            int nx = x + (i - 1) % 2;
            int ny = y + (i - 2) % 2;
            if (0 <= ny and ny < h and 0 <= nx and nx < w) {
                if (f[ny][nx] == f[y][x] and piece[ny][nx] == -1) {
                    go(ny, nx);
                }
            }
        }
    };
    REP (y, h) REP (x, w) {
        if (piece[y][x] == -1) {
            go(y, x);
            ++ num_piece;
        }
    }

    // make the digraph
    vector<vector<int> > g(num_piece);
    REP (y, h - 1) REP (x, w) {
        if (f[y][x] != f[y + 1][x]) {
            g[piece[y][x]].push_back(piece[y + 1][x]);
        }
    }
    REP (p, num_piece) {
        sort(ALL(g[p]));
        g[p].erase(unique(ALL(g[p])), g[p].end());
    }

    // strongly connected components
    int num_scc; vector<int> scc; tie(num_scc, scc) = decompose_to_strongly_connected_components(g, opposite_graph(g));
    vector<vector<int> > t = decomposed_graph(num_scc, scc, g);
    vector<vector<int> > t_rev = opposite_graph(t);
    vector<vector<int> > scc_height = make_table(num_scc, w, 0);
    REP (y, h) REP (x, w) {
        chmax(scc_height[scc[piece[y][x]]][x], h - y);
    }

    // run dp
    vector<int> outdegree(num_scc);
    REP (s, num_scc) {
        outdegree[s] = t[s].size();
    }
    stack<int> stk;
    REP (s, num_scc) {
        if (outdegree[s] == 0) {
            stk.push(s);
        }
    }
    vector<int> answer(num_scc);
    while (not stk.empty()) {
        int s = stk.top();
        stk.pop();
        for (int u : t[s]) {
            REP (x, w) {
                chmax(scc_height[s][x], scc_height[u][x]);
            }
        }
        REP (x, w) {
            answer[s] += scc_height[s][x];
        }
        for (int u : t_rev[s]) {
            -- outdegree[u];
            if (outdegree[u] == 0) {
                stk.push(u);
            }
        }
    }

    // answer to queries
    int q; cin >> q;
    while (q --) {
        int x, y; cin >> x >> y;
        -- x;
        -- y;
        cout << answer[scc[piece[y][x]]] << endl;
    }
    return 0;
}

Submission Info

Submission Time
Task E - パズルの移動
User kimiyuki
Language C++14 (GCC 5.4.1)
Score 120
Code Size 4878 Byte
Status
Exec Time 372 ms
Memory 63084 KB

Judge Result

Set Name Score / Max Score Test Cases
sub 30 / 30 test_02E.txt, test_04E.txt, test_06E.txt, test_08E.txt, test_10E.txt, test_12E.txt, test_14E.txt, test_16E.txt, test_18E.txt, test_20E.txt, test_22E.txt, test_24E.txt, test_26E.txt, test_28E.txt, test_30E.txt, test_32E.txt, test_35E.txt, test_36E.txt
All 90 / 90 sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02E.txt, test_03.txt, test_04E.txt, test_05.txt, test_06E.txt, test_07.txt, test_08E.txt, test_09.txt, test_10E.txt, test_11.txt, test_12E.txt, test_13.txt, test_14E.txt, test_15.txt, test_16E.txt, test_17.txt, test_18E.txt, test_19.txt, test_20E.txt, test_21.txt, test_22E.txt, test_23.txt, test_24E.txt, test_25.txt, test_26E.txt, test_27.txt, test_28E.txt, test_29.txt, test_30E.txt, test_31.txt, test_32E.txt, test_33.txt, test_34.txt, test_35E.txt, test_36E.txt
Case Name Status Exec Time Memory
sample_01.txt 1 ms 256 KB
sample_02.txt 1 ms 256 KB
sample_03.txt 1 ms 256 KB
test_01.txt 253 ms 34048 KB
test_02E.txt 44 ms 33408 KB
test_03.txt 40 ms 18048 KB
test_04E.txt 6 ms 2816 KB
test_05.txt 265 ms 10288 KB
test_06E.txt 53 ms 9772 KB
test_07.txt 67 ms 7000 KB
test_08E.txt 11 ms 1896 KB
test_09.txt 304 ms 28288 KB
test_10E.txt 92 ms 27636 KB
test_11.txt 152 ms 11712 KB
test_12E.txt 18 ms 5448 KB
test_13.txt 328 ms 37976 KB
test_14E.txt 109 ms 37452 KB
test_15.txt 115 ms 7540 KB
test_16E.txt 11 ms 3804 KB
test_17.txt 330 ms 43876 KB
test_18E.txt 118 ms 43356 KB
test_19.txt 109 ms 11588 KB
test_20E.txt 27 ms 10056 KB
test_21.txt 340 ms 47836 KB
test_22E.txt 129 ms 47076 KB
test_23.txt 30 ms 2504 KB
test_24E.txt 16 ms 5988 KB
test_25.txt 354 ms 55756 KB
test_26E.txt 141 ms 55116 KB
test_27.txt 30 ms 4840 KB
test_28E.txt 12 ms 4724 KB
test_29.txt 372 ms 63084 KB
test_30E.txt 146 ms 62444 KB
test_31.txt 47 ms 13856 KB
test_32E.txt 14 ms 6168 KB
test_33.txt 298 ms 41084 KB
test_34.txt 271 ms 23592 KB
test_35E.txt 89 ms 40444 KB
test_36E.txt 55 ms 23592 KB