Ecnerwala 000000000069ae7c
Ecnerwala 000000000069ae7c
Ecnerwala 000000000069ae7c
h>
protected:
std::array<int, NDIMS> shape;
std::array<int, NDIMS> strides;
T* data;
public:
tensor_view() : shape{0}, strides{0}, data(nullptr) {}
protected:
int flatten_index(std::array<int, NDIMS> idx) const {
int res = 0;
for (int i = 0; i < NDIMS; i++) { res += idx[i] * strides[i]; }
return res;
}
int flatten_index_checked(std::array<int, NDIMS> idx) const {
int res = 0;
for (int i = 0; i < NDIMS; i++) {
assert(0 <= idx[i] && idx[i] < shape[i]);
res += idx[i] * strides[i];
}
return res;
}
public:
T& operator[] (std::array<int, NDIMS> idx) const {
return data[flatten_index(idx)];
}
T& at(std::array<int, NDIMS> idx) const {
return data[flatten_index_checked(idx)];
}
protected:
std::array<int, NDIMS> shape;
std::array<int, NDIMS> strides;
int len;
T* data;
public:
tensor() : shape{0}, strides{0}, len(0), data(nullptr) {}
int main() {
using namespace std;
ios_base::sync_with_stdio(false), cin.tie(nullptr);
const int V = 2 * N;
vector<int> par(V, -1);
auto get_par = [&](int a) -> int {
while (par[a] >= 0) {
if (par[par[a]] >= 0) par[a] = par[par[a]];
a = par[a];
}
return a;
};
auto merge = [&](int a, int b) -> bool {
a = get_par(a);
b = get_par(b);
if (a == b) return false;
if (par[a] > par[b]) swap(a, b);
par[a] += par[b];
par[b] = a;
return true;
};
int64_t ans = 0;
for (int v = 1000; v >= 0; v--) {
for (auto [i, j] : vals[v]) {
if (!merge(i,N+j)) ans += v;
}
}
cout << "Case #" << case_num << ": " << ans << '\n';
}
return 0;
}