結果

問題 No.2639 Longest Increasing Walk
ユーザー ponjuice
提出日時 2024-02-13 14:43:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 1,623 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 204,228 KB
最終ジャッジ日時 2025-02-19 05:53:26
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

int main(){
    int h, w;
    cin >> h >> w;
    const auto id = [&](int x, int y)->int {
        return x * w + y;
    };
    vector a(h, vector<int>(w));
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> a[i][j];
        }
    }

    vector graph(h * w, vector<int>(0));
    vector in(h * w, 0);

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(i != 0){
                if(a[i][j] < a[i - 1][j]){
                    graph[id(i, j)].emplace_back(id(i - 1, j));
                    in[id(i - 1, j)]++;
                }
                if(a[i][j] > a[i - 1][j]){
                    graph[id(i - 1, j)].emplace_back(id(i, j));
                    in[id(i, j)]++;
                }
            }
            if(j != 0){
                if(a[i][j] < a[i][j - 1]){
                    graph[id(i, j)].emplace_back(id(i, j - 1));
                    in[id(i, j - 1)]++;
                }
                if(a[i][j] > a[i][j - 1]){
                    graph[id(i, j - 1)].emplace_back(id(i, j));
                    in[id(i, j)]++;
                }
            }
        }
    }

    queue<int> que;
    vector depth(h * w, 1);
    for(int i = 0; i < h * w; i++){
        if(in[i] == 0) que.push(i);
    }

    while(que.size()){
        int p = que.front();
        que.pop();
        for(int to : graph[p]){
            in[to]--;
            depth[to] = max(depth[to], depth[p] + 1);
            if(in[to] == 0) que.push(to);
        }
    }

    cout << *max_element(depth.begin(), depth.end()) << endl;
}
0