結果

問題 No.1668 Grayscale
ユーザー Sooh317Sooh317
提出日時 2021-09-03 23:18:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,615 bytes
コンパイル時間 4,564 ms
コンパイル使用メモリ 271,056 KB
実行使用メモリ 159,652 KB
最終ジャッジ日時 2023-08-22 01:08:49
合計ジャッジ時間 10,724 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/**
 *    author:  Sooh
 *    created: 03.09.2021 23:00:58
**/
#include<bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
//using mint = modint998244353;
//using mint = modint1000000007;
#endif
#pragma region template
using ll = long long;
template <class T> using V = vector<T>;
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define debug(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){cerr << endl;int cnt = 0;for(const auto& v : vv){cerr << cnt << "th : "; view(v); cnt++;} cerr << endl;}
ll power(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a;} a = a * a; p >>= 1;} return ret;}
ll modpow(ll a, ll p, ll mod){ll ret = 1; while(p){if(p & 1){ret = ret * a % mod;} a = a * a % mod; p >>= 1;} return ret;}
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0; while (b) {ll t = a / b ;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
template<class T, class K>bool chmax(T &a, const K b) { if (a<b) { a=b; return 1; } return 0; }
template<class T, class K>bool chmin(T &a, const K b) { if (b<a) { a=b; return 1; } return 0; }
#pragma endregion
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
const int inf = 1001001001;
const ll INF = 1001001001001001001ll;
//const double pi = acos(-1);
//const ll mod = 998244353;
const ll mod = 1000000007;


vector<int> topological_sort(vector<set<int>> &G, vector<int> &indegree){
    int V = G.size();
    vector<int> ret;
    queue<int> que;
    for(int i = 0; i < V; i++){
        if(indegree[i] == 0) que.push(i);
    }
    while(!que.empty()){
        int v = que.front(); que.pop();
        for(int nv : G[v]){
            indegree[nv]--;
            if(indegree[nv] == 0) que.push(nv);
        }
        ret.push_back(v);
    }
    return ret;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(20);
    int h,w, n; cin >> h >> w >> n;
    V<set<int>> G(n);
    V<V<int>> c(h, V<int>(w));
    rep(i,h) rep(j,w) cin >> c[i][j], --c[i][j];
    V<int> indeg(n);
    rep(i,h) rep(j,w){
        if(i >= 1 && c[i - 1][j] > c[i][j]){
            if(!G[c[i][j]].count(c[i - 1][j])){
                indeg[c[i - 1][j]]++;
                G[c[i][j]].insert(c[i - 1][j]);
            }
        }
        if(j >= 1 && c[i][j - 1] > c[i][j]){
            if(!G[c[i][j]].count(c[i][j - 1])){
                indeg[c[i][j - 1]]++;
                G[c[i][j]].insert(c[i][j - 1]);
            }
        }
        if(i < h - 1  && c[i + 1][j] > c[i][j]){
            if(!G[c[i][j]].count(c[i + 1][j])){
                indeg[c[i + 1][j]]++;
                G[c[i][j]].insert(c[i + 1][j]);
            }
        }
        if(j < w - 1 && c[i][j + 1] > c[i][j]){
            if(!G[c[i][j]].count(c[i][j + 1])){
                indeg[c[i][j + 1]]++;
                G[c[i][j]].insert(c[i][j + 1]);
            }
        }
    }
    auto res = topological_sort(G, indeg);
    V<int> dp(n + 1, 0);
    for(int v : res){
        if(dp[v] == 0) dp[v] = 1;
        for(int nv : G[v]){
            chmax(dp[nv], dp[v] + 1);
        }
    }
    cout << *max_element(all(dp)) << endl;
}
0