Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2022 /finals /tile_transposing.cpp
wjomlex's picture
2022 Problems
f7ba5f2 verified
#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;
}