QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#626632 | #9449. New School Term | tovarisch | WA | 0ms | 3632kb | C++23 | 3.5kb | 2024-10-10 11:02:07 | 2024-10-10 11:02:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010, rootn = 105;
struct DSU {
int n;
vector<int> f, ones;
vector<vector<int>> x;
DSU (int n_) : n(n_) {
f.resize(n);
ones.resize(n);
x.assign(n, vector<int>());
for (int i=0; i<n; ++i) {
f[i] = i;
ones[i] = i&1;
x[i].push_back(i);
}
}
int find(int a) { return f[a] = f[a] == a ? a : find(f[a]); }
void merge(int a, int b) {
a=find(a), b=find(b);
if (a == b) return;
if (x[a].size()<x[b].size()) swap(a, b);
f[b] = a;
ones[a] += ones[b];
while (x[b].size()) {
x[a].push_back(x[b].back());
x[b].pop_back();
}
}
};
vector<int> comp, other;
int col[maxn];
int from[maxn], to[maxn];
bitset<maxn> dp, dp2, affected;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // LOCAL
int n, m; cin >> n >> m;
for (int i=0; i<m; ++i) {
cin >> from[i] >> to[i];
from[i]--;
to[i]--;
}
DSU dsu(2*n), dsu2(2*n);
for (int i=0; i<2*n; ++i) col[i] = i&1;
auto solve = [&](bitset<maxn>& bit, DSU& ds, vector<int>& a) -> void {
bit.reset();
bit[0]=1;
int s = 0;
for (int k : a) {
bit = (bit << (ds.ones[k])) | (bit << (ds.x[k].size() - ds.ones[k]));
s += ds.x[k].size();
}
bit[0] = bit[s];
};
for (int i=m-1, j=m-1; i>=0; --i) {
if (i == j) { // new block
int added = 0;
affected.reset();
while (j>=0 && added<rootn) {
int u = from[j], v = to[j];
if (dsu2.find(u) != dsu2.find(v)) {
affected[u] = affected[v] = 1;
added++;
dsu2.merge(u, v);
}
j--;
}
other.clear();
for (int k=0; k<2*n; ++k) if (dsu2.find(k) == k && !affected[k] ) other.push_back(k);
solve(dp2, dsu2, other);
}
int u=from[i], v = to[i];
if (dsu.find(u) == dsu.find(v)) continue;
int ou = u, ov = v;
u=dsu.find(u), v=dsu.find(v);
comp.clear();
for (int k=0; k<2*n; ++k) if (dsu.find(k) == k && affected[k]) {
if (dsu.find(k) != u && dsu.find(k) != v) comp.push_back(k);
}
solve(dp, dsu, comp);
int flipu = (dsu.x[u].size()-dsu.ones[u]) + dsu.ones[v];
int flipv = (dsu.x[v].size()-dsu.ones[v]) + dsu.ones[u];
int eq = dsu.ones[u] + dsu.ones[v];
bool oku = 0, okv = 0, oke = 0;
for (int k=0; k<=n; ++k) if (dp[k]) {
oku |= n - flipu - k >= 0 && dp2[n - flipu - k];
okv |= n - flipv - k >= 0 && dp2[n - flipv - k];
oke |= n - eq - k >= 0 && dp2[n - eq - k];
}
int c = -1;
if (col[ou]!=col[ov] || !oke) {
if (oku) c = u;
else if (okv) c = v;
}
if (c != -1) {
for(int& w : dsu.x[c]) {
dsu.ones[c] -= col[w];
col[w] ^= 1;
dsu.ones[c] += col[w];
}
}
dsu.merge(u, v);
}
for(int i=0; i<2*n; ++i) cout << col[i];
cout << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3632kb
input:
2 4 1 3 2 4 1 4 1 2
output:
1100
result:
wrong answer The division is not minimized.