結果

問題 No.1668 Grayscale
ユーザー milanis48663220milanis48663220
提出日時 2021-09-03 23:05:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,396 bytes
コンパイル時間 1,375 ms
コンパイル使用メモリ 131,140 KB
実行使用メモリ 813,860 KB
最終ジャッジ日時 2024-05-09 06:35:17
合計ジャッジ時間 3,432 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 MLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

bool topological_sort(vector<vector<int>> g, vector<int> &order){
    int n = g.size();
    order.clear();
    vector<bool> used(n, false);
    function<void(int)> dfs = [&](int v){
        used[v] = true;
        for(int to : g[v]){
            if(!used[to]) dfs(to);
        }
        order.push_back(v);
    };
    for(int v = 0; v < n; v++){
        if(!used[v]) dfs(v);
    }
    reverse(order.begin(), order.end());
    vector<int> inv_order(n);
    for(int i = 0; i < n; i++) inv_order[order[i]] = i;
    for(int v = 0; v < n; v++){
        for(int u : g[v]){
            if(inv_order[v] > inv_order[u]) return false;
        }
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w, n; cin >> h >> w >> n;
    vector<vector<int>> c(n, vector<int>(n));
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> c[i][j];
        }
    }
    UnionFind uf(h*w);
    auto to_idx = [&](int i, int j){
        return i*w+j;
    };
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(i+1 < h){
                if(c[i][j] == c[i+1][j]) uf.unionSet(to_idx(i, j), to_idx(i+1, j));
            }
            if(j+1 < w){
                if(c[i][j] == c[i+1][j]) uf.unionSet(to_idx(i, j), to_idx(i, j+1));
            }
        }
    }
    vector<vector<int>> g(h*w);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(i+1 < h){
                int u = to_idx(i, j);
                int v = to_idx(i+1, j);
                if(c[i][j] > c[i+1][j]) g[v].push_back(u);
                if(c[i][j] < c[i+1][j]) g[u].push_back(v);
            }
            if(j+1 < w){
                int u = to_idx(i, j);
                int v = to_idx(i, j+1);
                if(c[i][j] > c[i][j+1]) g[v].push_back(u);
                if(c[i][j] < c[i][j+1]) g[u].push_back(v);
            }
        }
    }
    vector<int> ord(h*w);
    topological_sort(g, ord);
    int s = ord[0];
    vector<int> dp(h*w);
    dp[s] = 1;
    for(int u: ord){
        for(int v: g[u]){
            chmax(dp[v], dp[u]+1);
        }
    }
    cout << *max_element(dp.begin(), dp.end()) << endl;
}
0