# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
874154 | MilosMilutinovic | Robots (APIO13_robots) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> u(m), v(m);
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i];
--u[i]; --v[i];
}
int ans = 0;
bool bip = true;
vector<int> f(n);
vector<int> sz(n, 1);
vector<int> c(n);
iota(f.begin(), f.end(), 0);
function<int(int)> Get = [&](int x) {
return f[x] == x ? x : Get(f[x]);
};
auto Unite = [&](int x, int y) {
int px = Get(x);
int py = Get(y);
};
vector<vector<int>> qs(4 * m);
function<void(int, int, int, int, int, int)> AddEdge = [&](int x, int l, int r, int ql, int qr, int i) {
if (ql <= l && r <= qr) {
qs[x].push_back(i);
return;
}
int mid = (l + r) / 2;
if (qr <= mid) {
AddEdge(x << 1, l, mid, ql, qr, i);
} else if (ql > mid) {
AddEdge(x << 1 | 1, mid + 1, r, ql, qr, i);
} else {
AddEdge(x << 1, l, mid, ql, qr, i);
AddEdge(x << 1 | 1, mid + 1, r, ql, qr, i);
}
};
function<void(int, int, int)> Solve = [&](int x, int l, int r) {
for (int i : qs[x]) {
Unite(u[i], v[i]);
}
if (l == r) {
ans += bip;
for (int i : qs[x]) {
Rollback();
}
return;
}
int mid = (l + r) / 2;
Solve(x << 1, l, mid);
Solve(x << 1 | 1, mid + 1, r);
for (int i : qs[x]) {
Rollback();
}
};
for (int i = 0; i < m; i++) {
if (i > 0) {
AddEdge(1, 0, m - 1, 0, i - 1, i);
}
if (i + 1 < m) {
AddEdge(1, 0, m - 1, i + 1, m - 1, i);
}
}
Solve(1, 0, m - 1);
return 0;
}