QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#628745 | #9449. New School Term | tovarisch | WA | 1ms | 3732kb | C++23 | 3.2kb | 2024-10-10 22:00:58 | 2024-10-10 22:00:58 |
Judging History
answer
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
const int maxn = 10005, rootn = 105;
struct DSU {
int n;
vector<int> f;
vector<int> ones, zeros;
vector<vector<int>> comp;
DSU (int n_): n(n_) {
f.resize(n);
ones.resize(n);
comp.resize(n);
zeros.resize(n);
for (int i=0; i<n; ++i) {
f[i] = i;
ones[i] = i&1;
zeros[i] = 1 - ones[i];
comp[i].pb(i);
}
}
int find(int a) { return f[a] = f[a] == a ? a : find(f[a]); }
int size(int a) { return comp[a].size(); }
void merge(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
if (size(a) < size(b)) swap(a, b);
f[b] = a;
ones[a] += ones[b];
zeros[a] += zeros[b];
while (comp[b].size()) {
comp[a].pb(comp[b].back());
comp[b].pop_back();
}
}
};
int main(){
#ifdef LOCAL
freopen("in","r", stdin);
freopen("out","w",stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
vector<int> from(m), to(m), col(2*n, 0);
for (int i=0; i<2*n; ++i) col[i] = i&1;
DSU dsu(2*n);
for(int i=0; i<m; ++i) {
cin >> from[i] >> to[i];
from[i]--, to[i]--;
}
set<int> comp;
vector<pair<int, int>> process;
auto go = [&](bitset<maxn>& dp, set<int>& a, vector<int>&& ig) -> void {
dp[0] = 1;
for(int u : a) {
if(ig[0]==u || ig[1]==u) continue;
dp = (dp<<(dsu.ones[u])) | (dp<<(dsu.zeros[u]));
}
};
auto solve = [&]() -> void {
set<int> other;
for(int i=0; i<2*n; ++i) {
if(!comp.count(dsu.find(i))) other.insert(dsu.find(i));
}
bitset<maxn> dpc, dpo;
go(dpo, other, {-1, -1});
for(pair<int, int>& p : process) {
int u = dsu.find(p.ff), v = dsu.find(p.ss);
if (u == v) continue;
dpc = dpo;
go(dpc, comp, {u, v});
if (col[p.ff] == col[p.ss]) {
int cnt = dsu.ones[u] + dsu.zeros[v];
if (n - cnt >=0 && dpc[n - cnt]) {
for(int& w : dsu.comp[v]) col[w] ^=1;
swap(dsu.ones[v], dsu.zeros[v]);
}
} else {
int cnt = dsu.ones[u] + dsu.ones[v];
if (n - cnt < 0 || !dpc[n - cnt]) {
for(int& w : dsu.comp[v]) col[w] ^=1;
swap(dsu.ones[v], dsu.zeros[v]);
}
}
dsu.merge(u, v);
if (dsu.find(u) != u) comp.erase(u);
if (dsu.find(v) != v) comp.erase(v);
}
comp.clear();
process.clear();
};
for (int i=m-1; i>=0; --i) {
process.pb({from[i], to[i]});
comp.insert(dsu.find(from[i]));
comp.insert(dsu.find(to[i]));
if(comp.size()>=rootn) solve();
}
solve();
for(int i=0; i<2*n; ++i) cout << col[i];
cout << endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3560kb
input:
2 4 1 3 2 4 1 4 1 2
output:
0101
result:
ok Output is valid. OK
Test #2:
score: 0
Accepted
time: 0ms
memory: 3500kb
input:
3 7 2 5 1 3 4 6 2 6 4 5 2 4 5 6
output:
011010
result:
ok Output is valid. OK
Test #3:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
1 0
output:
01
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 0ms
memory: 3732kb
input:
1 1 1 2
output:
01
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
2 3 2 4 3 4 1 2
output:
0110
result:
ok Output is valid. OK
Test #6:
score: 0
Accepted
time: 0ms
memory: 3500kb
input:
3 8 4 6 3 5 1 4 2 4 1 6 1 2 3 4 4 5
output:
010101
result:
ok Output is valid. OK
Test #7:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
4 9 4 7 3 8 1 5 2 7 2 8 6 8 7 8 1 4 1 6
output:
01010110
result:
ok Output is valid. OK
Test #8:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
5 16 3 6 9 10 2 7 1 10 1 5 2 10 3 5 5 6 3 4 2 5 4 5 3 8 4 7 6 8 1 6 7 10
output:
1101000101
result:
ok Output is valid. OK
Test #9:
score: -100
Wrong Answer
time: 0ms
memory: 3500kb
input:
6 13 4 5 2 9 3 8 4 8 4 11 10 12 3 4 3 9 5 11 2 8 5 10 5 8 1 11
output:
110111001001
result:
wrong answer The number of 0s must be equal to that of 1s.