QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#640943 | #9449. New School Term | crimsonsunset# | WA | 1ms | 3928kb | C++20 | 3.5kb | 2024-10-14 17:04:23 | 2024-10-14 17:04:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
constexpr int mod = 1e9 + 4099;
struct knapsack {
vector<int> cnt;
knapsack() {
cnt.resize(10001, 0);
cnt[0] = 1;
}
void add(pair<int, int> x) {
vector<int> oc = cnt;
for (int i = 0; i <= 10000; ++i)
cnt[i] = ((i >= x.ff ? oc[i - x.ff] : 0) + (i >= x.ss ? oc[i - x.ss] : 0)) % mod;
}
void del(pair<int, int> x) {
vector<int> oc = cnt;
for (int i = 0; i <= 10000; ++i)
cnt[i] = (cnt[i] - (i >= x.ff ? oc[i - x.ff] : 0) + (i >= x.ss ? oc[i - x.ss] : 0) + mod) % mod;
}
};
knapsack kp;
struct dsu {
vector<pair<int, int>> par, sz;
int need;
dsu(int n) {
need = n / 2;
par.resize(n);
sz.resize(n);
for (int i = 0; i < n; ++i) {
par[i] = {i, 0};
sz[i] = {1, 0};
kp.add({1, 0});
}
}
pair<int, int> get(int v) {
if (par[v].ff == v)
return par[v];
auto res = get(par[v].ff);
res.ss ^= par[v].ss;
par[v] = res;
return par[v];
}
bool merge(int u, int v) {
auto pu = get(u), pv = get(v);
if (pu.ff != pv.ff) {
auto opv = par[pv.ff], osz = sz[pu.ff];
kp.del(sz[pu.ff]);
kp.del(sz[pv.ff]);
par[pv.ff] = {pu.ff, pv.ss ^ pu.ss ^ 1};
if (par[pv.ff].ss == 0)
sz[pu.ff].ff += sz[pv.ff].ff, sz[pu.ff].ss += sz[pv.ff].ss;
else
sz[pu.ff].ss += sz[pv.ff].ff, sz[pu.ff].ff += sz[pv.ff].ss;
kp.add(sz[pu.ff]);
if (kp.cnt[need] != 0)
return true;
else {
par[pv.ff] = opv;
sz[pu.ff] = osz;
return false;
}
} else {
if (pu.ss != pv.ss)
return true;
return false;
}
}
};
void solve() {
int n, m;
cin >> n >> m;
n *= 2;
vector<pair<int, int>> a;
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v;
--u, --v;
a.push_back({u, v});
}
dsu cmp(n);
for (int i = m - 1; i >= 0; --i)
cmp.merge(a[i].ff, a[i].ss);
set<int> cc;
vector<array<int, 3>> sz;
for (int i = 0; i < n; ++i) {
if (!cc.count(cmp.get(i).ff)) {
int v = cmp.get(i).ff;
sz.push_back({cmp.sz[v].ff, cmp.sz[v].ss, v});
cc.insert(v);
}
}
vector<vector<int>> dp(sz.size() + 1, vector<int>(n + 1, -1));
dp[0][0] = 1;
for (int i = 1; i <= sz.size(); ++i) {
for (int j = 0; j <= n + 1; ++j) {
if (j >= sz[i - 1][0] && dp[i - 1][j - sz[i - 1][0]] != -1)
dp[i][j] = 0;
if (j >= sz[i - 1][1] && dp[i - 1][j - sz[i - 1][1]] != -1)
dp[i][j] = 1;
}
}
int x = sz.size(), y = n / 2;
assert(dp[x][y] != -1);
map<int, int> ans;
while (x != 0) {
ans[sz[x - 1][2]] = dp[x][y];
y -= sz[x - 1][dp[x][y]];
--x;
}
for (int i = 0; i < n; ++i)
cout << (cmp.get(i).ss ^ ans[cmp.get(i).ff]);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3924kb
input:
2 4 1 3 2 4 1 4 1 2
output:
1010
result:
ok Output is valid. OK
Test #2:
score: 0
Accepted
time: 1ms
memory: 3728kb
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: 3928kb
input:
1 0
output:
01
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 1ms
memory: 3664kb
input:
1 1 1 2
output:
10
result:
ok Output is valid. OK
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 3668kb
input:
2 3 2 4 3 4 1 2
output:
1010
result:
wrong answer The division is not minimized.