File size: 8,442 Bytes
f7ba5f2 |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
const int LIM = 3005;
const int MAXN = LIM * LIM;
vector<int> adj[MAXN];
unordered_set<int> cutpoints;
vector<int> comp;
vector<vector<int>> bcc, bcc_adj;
void reset_edges() {
for (int i = 0; i < MAXN; i++) {
adj[i].clear();
}
}
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void tarjan(int N) {
cutpoints.clear();
bcc.clear();
comp.assign(N, -1);
vector<int> vis(N), parent(N, -1), children(N), tin(N), pushed(N), low(N);
vector<bool> is_cut(N);
int curr_root = 0;
stack<tuple<int, int, int>> dfs;
stack<int> st;
for (int i = 0; i < N; i++) {
if (vis[i]) {
continue;
}
vis[i] = ++curr_root;
tin[i] = 0;
int timer = 1;
dfs.push({i, 0, 0});
while (!dfs.empty()) {
auto [u, j, t] = dfs.top();
low[u] = t;
if (!pushed[u]) {
st.push(u);
pushed[u] = 1;
}
if (j == (int)adj[u].size()) {
if ((parent[u] == -1 && children[u] >= 2) ||
(parent[u] != -1 && is_cut[u])) {
cutpoints.insert(u);
}
if (t >= tin[u]) {
vector<int> component;
do {
int v = st.top();
component.push_back(v);
comp[v] = bcc.size();
st.pop();
} while (comp[u] == -1);
bcc.push_back(component);
}
dfs.pop();
continue;
}
int v = adj[u][j];
if (parent[u] == v) {
get<1>(dfs.top())++;
continue;
}
if (vis[v] == 0) {
vis[v] = curr_root;
parent[v] = u;
children[u]++;
dfs.push({v, 0, timer});
tin[v] = timer++;
continue;
}
is_cut[u] = is_cut[u] || low[v] >= tin[u];
if (vis[v] == curr_root && comp[v] == -1 && low[v] < t) {
get<2>(dfs.top()) = low[v];
}
get<1>(dfs.top())++;
}
}
bcc_adj.assign(bcc.size(), {});
for (int i = 0; i < N; i++) {
for (int j = 0; j < (int)adj[i].size(); j++) {
if (comp[i] != comp[adj[i][j]]) {
bcc_adj[comp[i]].push_back(comp[adj[i][j]]);
}
}
}
}
template <class Value>
class LinkCut {
struct Node {
int ch[2] = {0, 0}, p = 0;
Value self = 0, path = 0; // Path aggregates
Value sub = 0, vir = 0; // Subtree aggregates
bool flip = 0; // Lazy tags
};
vector<Node> T;
void push(int x) {
if (x == 0 || !T[x].flip) {
return;
}
int l = T[x].ch[0], r = T[x].ch[1];
T[l].flip ^= 1;
T[r].flip ^= 1;
swap(T[x].ch[0], T[x].ch[1]);
T[x].flip = 0;
}
void pull(int x) {
int l = T[x].ch[0], r = T[x].ch[1];
push(l);
push(r);
T[x].path = T[l].path + T[x].self + T[r].path;
T[x].sub = T[x].vir + T[l].sub + T[r].sub + T[x].self;
}
void set(int x, int d, int y) {
T[x].ch[d] = y;
T[y].p = x;
pull(x);
}
void splay(int x) {
auto dir = [&](int x) {
int p = T[x].p;
if (p == 0) {
return -1;
}
return T[p].ch[0] == x ? 0 : T[p].ch[1] == x ? 1 : -1;
};
auto rotate = [&](int x) {
int y = T[x].p, z = T[y].p, dx = dir(x), dy = dir(y);
set(y, dx, T[x].ch[!dx]);
set(x, !dx, y);
if (~dy) {
set(z, dy, x);
}
T[x].p = z;
};
for (push(x); ~dir(x);) {
int y = T[x].p, z = T[y].p;
push(z);
push(y);
push(x);
int dx = dir(x), dy = dir(y);
if (~dy) {
rotate(dx != dy ? x : y);
}
rotate(x);
}
}
int access(int x) {
int v = 0;
for (int u = x; u != 0; u = T[u].p) {
splay(u);
int &ov = T[u].ch[1];
T[u].vir += T[ov].sub;
T[u].vir -= T[v].sub;
ov = v;
pull(u);
v = u;
}
splay(x);
return v;
}
void reroot(int x) {
access(x);
T[x].flip ^= 1;
push(x);
}
public:
LinkCut(int n = 0) : T(n + 1) {}
void Init(int u, Value v) {
T[++u].self = v;
pull(u);
}
void Reset(int n) {
T.clear();
T.resize(n + 1);
}
void Link(int u, int v) {
if (u == v || LCA(u, v) != -1) {
return;
}
reroot(++u);
access(++v);
T[v].vir += T[u].sub;
T[u].p = v;
pull(v);
}
void Cut(int u, int v) {
if (u == v || LCA(u, v) == -1) {
return;
}
reroot(++u);
access(++v);
T[v].ch[0] = T[u].p = 0;
pull(v);
}
// Rooted tree LCA. Returns -1 if u and v aren't connected.
int LCA(int u, int v) {
if (++u == ++v) {
return u;
}
access(u);
int ret = access(v);
return T[u].p ? ret : -1;
}
// Query subtree of u where v is outside the subtree.
// Pass u = v to get the aggregate for the entire tree containing u.
Value Subtree(int u, int v) {
reroot(++v);
access(++u);
return T[u].vir + T[u].self;
}
Value Path(int u, int v) {
reroot(++u);
access(++v);
return T[v].path;
}
void Update(int u, Value v) {
access(++u);
T[u].self = v;
pull(u);
}
};
// Main algorithm.
int R, C;
int G[LIM][LIM];
LinkCut<int> lcf;
inline int getn(int r, int c) { return r * C + c; }
inline int getr(int n) { return n / C; }
inline int getc(int n) { return n % C; }
// Size of group connected to (r2, c2) after swapping (r1, c1) to (r2, c2).
int num_cleared(int r1, int c1, int r2, int c2) {
int res = 0;
int u = getn(r1, c1), cu = comp[u];
// If u is a cutpoint, disconnect it from all neighboring BCCs.
if (cutpoints.count(u)) {
for (int v : adj[u]) {
int cv = comp[v];
if (cu != cv) {
lcf.Cut(cu, cv);
}
}
}
// Get a representative for each neighboring group of (r2, c2).
unordered_set<int> seen_groups;
bool seen_orig = false; // Whether we've seen the original group of cu.
for (auto [dr, dc] : {pair{-1, 0}, {0, 1}, {1, 0}, {0, -1}}) {
int r3 = r2 + dr, c3 = c2 + dc;
if (r3 < 0 || r3 >= R || c3 < 0 || c3 >= C) {
continue;
}
if (!(r1 == r3 && c1 == c3) && G[r1][c1] == G[r3][c3]) {
int cv = comp[getn(r3, c3)];
bool seen = false; // Have we seen the same group?
for (int cmp : seen_groups) {
if (lcf.LCA(cv, cmp) != -1) {
seen = true;
break;
}
}
if (!seen) {
if (lcf.LCA(cu, cv) != -1) {
seen_orig = true;
}
res += lcf.Subtree(cv, cv);
seen_groups.insert(cv);
}
}
}
// If we didn't encounter the original component, add 1 for the cell itself.
if (!seen_orig) {
res++;
}
// Undo disconnections.
if (cutpoints.count(u)) {
for (int v : adj[u]) {
int cv = comp[v];
if (cu != cv) {
lcf.Link(cu, cv);
}
}
}
return res >= 3 ? res : 0;
}
long long solve() {
reset_edges();
// Build graph.
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
for (auto [dr, dc] : {pair{0, 1}, {1, 0}}) {
int r2 = r + dr, c2 = c + dc;
if (r2 < 0 || r2 >= R || c2 < 0 || c2 >= C) {
continue;
}
if (G[r][c] == G[r2][c2]) {
add_edge(getn(r, c), getn(r2, c2));
}
}
}
}
// Compute BCC and block forest.
tarjan(R * C);
int bcc_nodes = (int)bcc.size();
// Create link-cut forest from BCC.
lcf.Reset(bcc_nodes);
for (int i = 0; i < bcc_nodes; i++) {
lcf.Init(i, bcc[i].size());
}
for (int i = 0; i < bcc_nodes; i++) {
for (int j : bcc_adj[i]) {
lcf.Link(i, j);
}
}
// Try swapping each neighbor.
long long ans = 0;
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
// Only check right and bottom for ordered pairs.
for (auto [dr, dc] : {pair{0, 1}, {1, 0}}) {
int r2 = r + dr, c2 = c + dc;
if (r2 < 0 || r2 >= R || c2 < 0 || c2 >= C) {
continue;
}
if (G[r][c] != G[r2][c2]) {
ans += num_cleared(r, c, r2, c2) + num_cleared(r2, c2, r, c);
}
}
}
}
return 2 * ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
memset(G, 0, sizeof G);
cin >> R >> C;
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
cin >> G[r][c];
}
}
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
}
|