1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #include <bits/stdc++.h> #define SINGLE_INPUT #define ll long long #define ull unsigned long long #define N 1005 #define MOD 998244353 using namespace std;
int vis[N][N];
ll g[N][N];
ll n, m, k;
int st[N * N]; ll cnt = 0;
void dfs(int x, int y, ll lm) { if (cnt == 0) return; cnt--; vis[x][y] = 1; for (int d = 0; d < 4; d++) { int mx = x + (d - 1) % 2; int my = y + (d - 2) % 2; if (mx < 0 || mx >= n || my < 0 || my >= m || g[mx][my] < lm || vis[mx][my]) continue; dfs(mx, my, lm); } }
void uf_init() { memset(st, -1, sizeof(st)); }
int uf_find(int x) { return st[x] < 0 ? x : st[x] = uf_find(st[x]); }
bool uf_union(int x, int y) { int fx = uf_find(x), fy = uf_find(y); if (fx != fy) { st[fx] += st[fy]; st[fy] = fx; return true; } return false; }
void sol() { uf_init(); map<ll, vector<pair<int, int>>, greater<ll>> mp; cin >> n >> m >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> g[i][j]; mp[g[i][j]].emplace_back(i, j); } } set<pair<ll, ll>> root; for (auto& [i, j] : mp) { for (auto [x, y] : j) { int t = uf_find(x * m + y); root.insert({st[t], t}); for (int d = 0; d < 4; d++) { int mx = x + (d - 1) % 2; int my = y + (d - 2) % 2;
if (mx < 0 || mx >= n || my < 0 || my >= m || g[mx][my] < i) continue; int fa = uf_find(mx * m + my), fb = uf_find(x * m + y); if (root.count({st[fa], fa})) root.erase({st[fa], fa}); if (root.count({st[fb], fb})) root.erase({st[fb], fb}); if (uf_union(mx * m + my, x * m + y)) { fa = uf_find(x * m + y); root.insert({st[fa], fa}); } else { root.insert({st[fa], fa}); root.insert({st[fb], fb}); } } } if (k % i == 0 && -i * root.begin()->first >= k) { cnt = k / i; cout << "YES\n";
dfs(j[0].first, j[0].second, i);
for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { cout << (vis[r][c] ? i : 0) << " "; } cout << "\n"; } return; } } cout << "NO\n"; }
int main() { cout << setprecision(15) << fixed; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef SINGLE_INPUT int t; cin >> t; while (t--) { sol(); } #else sol(); #endif return 0; }
|