結果
問題 |
No.1668 Grayscale
|
ユーザー |
![]() |
提出日時 | 2021-09-03 23:05:18 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,396 bytes |
コンパイル時間 | 1,563 ms |
コンパイル使用メモリ | 125,800 KB |
最終ジャッジ日時 | 2025-01-24 06:54:31 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 1 WA * 8 RE * 4 MLE * 9 |
ソースコード
#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; }