結果
問題 | No.2639 Longest Increasing Walk |
ユーザー |
|
提出日時 | 2024-05-29 06:44:40 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 197 ms / 2,000 ms |
コード長 | 1,660 bytes |
コンパイル時間 | 4,291 ms |
コンパイル使用メモリ | 110,552 KB |
実行使用メモリ | 41,452 KB |
最終ジャッジ日時 | 2024-12-20 20:58:52 |
合計ジャッジ時間 | 8,163 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> topological_sort (vector<vector<int>>& graph) { vector<int> res(0); vector<bool> vis(graph.size(), false); auto dfs = [&](auto self, int pos) -> void { vis[pos] = true; for (auto to: graph[pos]) { if (vis[to]) continue; self(self, to); } res.push_back(pos); }; for (int i = 0; i < graph.size(); i++) if (!vis[i]) dfs(dfs, i); reverse(res.begin(), res.end()); return res; } int main () { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } const int dxy[][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}, }; auto is_in = [&](int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }; vector<vector<int>> graph(H * W); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (auto d: dxy) { int ni = i + d[0], nj = j + d[1]; if (!is_in(ni, nj)) continue; if (A[i][j] < A[ni][nj]) { graph[i * W + j].push_back(ni * W + nj); } } } } auto update_ord = topological_sort(graph); vector<int> score(H * W, 1); for (auto v: update_ord) { for (auto nex: graph[v]) { score[nex] = max(score[nex], score[v] + 1); } } int ans = 0; for (auto v: score) ans = max(ans, v); cout << ans << "\n"; }