QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789509 | #9449. New School Term | Misuki | TL | 1ms | 3852kb | C++20 | 10.0kb | 2024-11-27 20:37:56 | 2024-11-27 20:37:57 |
Judging History
answer
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
//#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)
#define clock chrono::steady_clock::now().time_since_epoch().count()
using namespace std;
template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2> pr) {
return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
for(size_t i = 0; T x : arr) {
os << x;
if (++i != N) os << ' ';
}
return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
for(size_t i = 0; T x : vec) {
os << x;
if (++i != size(vec)) os << ' ';
}
return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
for(size_t i = 0; T x : s) {
os << x;
if (++i != size(s)) os << ' ';
}
return os;
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const map<T1, T2> &m) {
for(size_t i = 0; pair<T1, T2> x : m) {
os << x;
if (++i != size(m)) os << ' ';
}
return os;
}
#ifdef DEBUG
#define dbg(...) cerr << '(', _do(#__VA_ARGS__), cerr << ") = ", _do2(__VA_ARGS__)
template<typename T> void _do(T &&x) { cerr << x; }
template<typename T, typename ...S> void _do(T &&x, S&&...y) { cerr << x << ", "; _do(y...); }
template<typename T> void _do2(T &&x) { cerr << x << endl; }
template<typename T, typename ...S> void _do2(T &&x, S&&...y) { cerr << x << ", "; _do2(y...); }
#else
#define dbg(...)
#endif
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
//#define double ldb
template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using max_heap = priority_queue<T>;
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, class OP = plus<T>>
void pSum(rng &&v) {
if (!v.empty())
for(T p = v[0]; T &x : v | views::drop(1))
x = p = OP()(p, x);
}
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, class OP>
void pSum(rng &&v, OP op) {
if (!v.empty())
for(T p = v[0]; T &x : v | views::drop(1))
x = p = op(p, x);
}
template<ranges::forward_range rng>
void Unique(rng &v) {
ranges::sort(v);
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template<ranges::random_access_range rng>
rng invPerm(rng p) {
rng ret = p;
for(int i = 0; i < ssize(p); i++)
ret[p[i]] = i;
return ret;
}
template<ranges::random_access_range rng, ranges::random_access_range rng2>
rng Permute(rng v, rng2 p) {
rng ret = v;
for(int i = 0; i < ssize(p); i++)
ret[p[i]] = v[i];
return ret;
}
template<bool directed>
vector<vector<int>> readGraph(int n, int m, int base) {
vector<vector<int>> g(n);
for(int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
u -= base, v -= base;
g[u].emplace_back(v);
if constexpr (!directed)
g[v].emplace_back(u);
}
return g;
}
template<class T>
void setBit(T &msk, int bit, bool x) {
msk = (msk & ~(T(1) << bit)) | (T(x) << bit);
}
template<class T> void flipBit(T &msk, int bit) { msk ^= T(1) << bit; }
template<class T> bool getBit(T msk, int bit) { return msk >> bit & T(1); }
template<class T>
T floorDiv(T a, T b) {
if (b < 0) a *= -1, b *= -1;
return a >= 0 ? a / b : (a - b + 1) / b;
}
template<class T>
T ceilDiv(T a, T b) {
if (b < 0) a *= -1, b *= -1;
return a >= 0 ? (a + b - 1) / b : a / b;
}
template<class T> bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T> bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef vector<int> vi;
//source: KACTL(https://github.com/kth-competitive-programming/kactl)
ull modmul(ull a, ull b, ull M) {
ll ret = a * b - M * ull(1.L / M * a * b);
return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull modpow(ull b, ull e, ull mod) {
ull ans = 1;
for (; e; b = modmul(b, b, mod), e /= 2)
if (e & 1) ans = modmul(ans, b, mod);
return ans;
}
bool isPrime(ull n) {
if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;
ull A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022},
s = __builtin_ctzll(n-1), d = n >> s;
for (ull a : A) { // ^ count trailing zeroes
ull p = modpow(a%n, d, n), i = s;
while (p != 1 && p != n - 1 && a % n && i--)
p = modmul(p, p, n);
if (p != n-1 && i != s) return 0;
}
return 1;
}
struct DSU {
vector<int> dep;
vector<int> bos;
vector<int> d;
int size;
DSU(int _size) : size(_size), dep(_size, 0), bos(_size), d(_size) {
iota(bos.begin(), bos.end(), 0);
}
array<int, 2> query(int V) {
if (bos[V] == V) {
return {d[V], V};
} else {
auto res = query(bos[V]);
d[V] ^= res[0];
bos[V] = res[1];
return {d[V], bos[V]};
}
}
bool merge(int V1, int V2, bool eq) {
int del = (eq ? 0 : 1);
int B1 = query(V1)[1];
int B2 = query(V2)[1];
if (B1 == B2)
return (d[V1] ^ d[V2]) == del;
if (dep[B1] < dep[B2]) {
bos[B1] = B2, d[B1] = d[V1] ^ d[V2] ^ del;
} else if (dep[B1] > dep[B2]) {
bos[B2] = B1, d[B2] = d[V1] ^ d[V2] ^ del;
} else {
bos[B1] = B2, d[B1] = d[V1] ^ d[V2] ^ del;
dep[B2] += 1;
}
return true;
}
};
mt19937 rng(clock);
uniform_int_distribution<int> unif(500'000'000, 1'000'000'000);
const int K = 2;
array<int, 4> P;
struct knapsack {
static const int C = 5001;
int base = 0;
array<array<int, C>, K> dp;
knapsack() : dp({}) {
for(int i = 0; i < K; i++) dp[i][0] = 1;
}
void add_item(array<int, 2> x) {
base += ranges::min(x);
int w = abs(x[0] - x[1]);
if (w == 0) return;
for(int i = 0; i < K; i++)
for(int j = C - 1; j >= w; j--)
dp[i][j] = (dp[i][j] + dp[i][j - w]) % P[i];
}
void del_item(array<int, 2> x) {
base -= ranges::min(x);
int w = abs(x[0] - x[1]);
if (w == 0) return;
for(int i = 0; i < K; i++)
for(int j = w; j < C; j++)
dp[i][j] = (dp[i][j] - dp[i][j - w] + P[i]) % P[i];
}
bool query(int s) {
dbg(s, base);
for(int i = 0; i < K; i++)
if (dp[i][s - base] != 0)
return true;
return false;
}
};
signed main() {
ios::sync_with_stdio(false), cin.tie(NULL);
for(int i = 0; i < K; i++)
while(!isPrime(P[i] = unif(rng))) ;
int n, m; cin >> n >> m;
vector<array<int, 2>> e(m);
for(auto &[a, b] : e) {
cin >> a >> b;
a--, b--;
}
vector<array<int, 2>> f(2 * n, {1, 0});
DSU dsu(2 * n);
auto update_f = [&]() {
fill(f.begin(), f.end(), array<int, 2>{0, 0});
for(int i = 0; i < 2 * n; i++) {
auto [d, bos] = dsu.query(i);
f[bos][d]++;
}
};
knapsack ks;
for(int i = 0; i < 2 * n; i++)
ks.add_item(f[i]);
//string con;
for(auto [a, b] : e | views::reverse) {
auto [da, ba] = dsu.query(a);
auto [db, bb] = dsu.query(b);
if (ba == bb) { /*con += '0';*/ continue; }
ks.del_item(f[ba]), ks.del_item(f[bb]);
array<int, 2> x = f[ba];
if (da == db) x[0] += f[bb][1], x[1] += f[bb][0];
else x[0] += f[bb][0], x[1] += f[bb][1];
array<int, 2> y = f[ba];
if (da == db) y[0] += f[bb][1], y[1] += f[bb][0];
else y[0] += f[bb][0], y[1] += f[bb][1];
ks.add_item(x);
if (ks.query(n)) {
dsu.merge(a, b, false);
update_f();
//con += '1';
} else {
dsu.merge(a, b, true);
ks.del_item(x);
ks.add_item(y);
update_f();
//con += '0';
}
//assert(ks.query(n));
}
//cout << con << '\n';
//return 0;
vector<int> pre(n + 1, -1);
pre[0] = 0;
int base = 0;
for(int j = -1; auto [x, y] : f) {
j++;
base += min(x, y);
int w = abs(x - y);
if (w == 0) continue;
for(int i = n; i >= w; i--)
if (pre[i - w] != -1 and pre[i] == -1)
pre[i] = j;
}
//assert(pre[n - base] != -1);
dbg(pre);
vector<bool> high(2 * n, false);
{
int i = n - base;
while(i != 0) {
dbg("take", pre[i]);
high[pre[i]] = true;
i -= abs(f[pre[i]][0] - f[pre[i]][1]);
}
}
dbg(high);
string ans;
for(int i = 0; i < 2 * n; i++) {
auto [d, bos] = dsu.query(i);
bool is_high = pii(f[bos][d], d) > pii(f[bos][d ^ 1], d ^ 1);
if (is_high xor high[bos])
ans += '0';
else
ans += '1';
}
/*
{
int f[2] = {};
for(char c : ans) f[c - '0']++;
if (f[0] != n or f[1] != n) cout << rng() << '\n';
for(int i = 0; i < 2 * n; i++)
for(int j = i + 1; j < 2 * n; j++) {
auto [da, ba] = dsu.query(i);
auto [db, bb] = dsu.query(j);
if (ba != bb) continue;
if ((da == db) != (ans[i] == ans[j]))
cout << rng() << '\n';
}
}
*/
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3692kb
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: 1ms
memory: 3688kb
input:
3 7 2 5 1 3 4 6 2 6 4 5 2 4 5 6
output:
001101
result:
ok Output is valid. OK
Test #3:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
1 0
output:
10
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 1ms
memory: 3852kb
input:
1 1 1 2
output:
01
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
2 3 2 4 3 4 1 2
output:
1001
result:
ok Output is valid. OK
Test #6:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
3 8 4 6 3 5 1 4 2 4 1 6 1 2 3 4 4 5
output:
101010
result:
ok Output is valid. OK
Test #7:
score: -100
Time Limit Exceeded
input:
4 9 4 7 3 8 1 5 2 7 2 8 6 8 7 8 1 4 1 6