QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#618583 | #9449. New School Term | ucup-team4906 | ML | 37ms | 30320kb | C++17 | 4.1kb | 2024-10-07 00:05:18 | 2024-10-07 00:05:20 |
Judging History
answer
//by 72
#include<bits/stdc++.h>
#define F(i, a, b) for(int i = a; i <= b; i ++)
#define Fd(i, a, b) for(int i = a; i >= b; i --)
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
// #define int long long
using namespace std;
const int mod = 998244353;
const int N = 1e6 + 10;
const int M = 1e4 + 10;
// const int inf = 1e18;
typedef array<int, 3> a3;
typedef long long ll;
int n, m, f[N], sz1[N], sz2[N], rt[N], qsy[N], f2[N];
pii a[N];
int find(int x) {return x == f[x] ? x : find(f[x]);}
int find2(int x) {return x == f2[x] ? x : f2[x] = find2(f2[x]);}
int v1[N], v2[N];
int s[N], tot = 0;
// 扩展域并查集
void merge2(int x, int y) {
int p = find(x), q = find(y);
if(p == q) return;
f2[q] = p;
}
bool merge(int x, int y) {
int p = find(x), q = find(y);
if(p == q) return false;
if(sz1[p] + sz2[p] < sz1[q] + sz2[q]) swap(p, q);
f[q] = p, rt[q] = 0;
sz1[p] += sz1[q], sz2[p] += sz2[q];
if(v1[p] && v1[q]) merge2(p, q);
else if(! v1[p] && v1[q]) v1[p] = v1[q];
if(v2[p] && v2[q]) merge2(p, q);
else if(! v2[p] && v2[q]) v2[p] = v2[q];
s[++ tot] = q;
return true;
}
void del(){//删除栈顶边
if(! tot) return;
int y = s[tot --];
sz1[f[y]] -= sz1[y], sz2[f[y]] -= sz2[y];
f[y] = y, rt[y] = 1;
}
int cnt[N];
int dp[M][M / 2], pre[M][M / 2];
pii now[N];
void sol() {
cin >> n >> m;
// n = 5000, m = 1e6;
// 再维护一个表示一堆在一个连通块里的
F(i, 1, 4 * n) {
f[i] = i, f2[i] = i, rt[i] = 1;
if(i <= 2 * n) sz1[i] = 1, sz2[i] = 0, v1[i] = i;
else sz2[i] = 1, sz1[i] = 0, v2[i] = i - 2 * n;
}
F(i, 1, m) cin >> a[i].fi >> a[i].se;
// int now1 = 1, now2 = 2;
// F(i, 1, m) {
// a[i].fi = now1, a[i].se = now2;
// now2 ++;
// if(now2 > 2 * n) now1 ++, now2 = now1 + 1;
// }
Fd(i, m, 1) {
auto [u, v] = a[i];
if(find2(u) == find2(v)) continue;
if(find(u) == find(v)) continue;
int sum = 0, qsyy = tot;
if(! merge(u, v + 2 * n)) continue;
merge(v, u + 2 * n);
int mx = 0;
F(j, 1, 4 * n) if(rt[j]) {
// 保证每个连通块会被遍历两次,两次中的集合互为反点
int x = sz1[j], y = sz2[j];
if(x > y) swap(x, y);
mx = max(mx, y - x);
sum += x, cnt[y - x] ++;
}
sum /= 2;
int idx = 0;
F(j, 1, mx) if(cnt[j]) now[++ idx] = {j, cnt[j] / 2}, cnt[j] = 0;
sum = n - sum; // 背出这么多
bitset<5001> g;
g[0] = 1;
F(j, 1, idx) {
auto [x, y] = now[j];
if(g[sum]) break;
for(int j = 0, now; y; j ++) {
now = min(1 << j, y), y -= now;
g |= g << (now * x);
}
}
if(! g[sum]) {
del(), del();
merge2(u, v);
}
}
vector<a3> tmp;
F(i, 1, 4 * n) if(rt[i]) {
tmp.push_back({i, sz1[i], sz2[i]});
rt[find(i + 2 * n)] = 0; // 保留这个集合 把另一个删了
}
dp[0][0] = 1;
assert(tmp.size() < M);
for(int i = 0; i < tmp.size(); i ++) {
auto [id, x, y] = tmp[i];
F(j, 0, n) if(dp[i][j]) {
if(j + x <= n) dp[i + 1][j + x] = 1, pre[i + 1][j + x] = j;
if(j + y <= n) dp[i + 1][j + y] = 1, pre[i + 1][j + y] = j;
}
}
int now = n;
Fd(i, tmp.size(), 1) {
auto [id, x, y] = tmp[i - 1];
int qaq = now - pre[i][now];
now = pre[i][now];
if(qaq == y) qsy[id] = 1;
}
vector<int> res(2 * n + 1);
F(i, 1, 4 * n) if(rt[find(i)]) {
int flag = qsy[find(i)];
if(i <= 2 * n) res[i] = flag;
else res[i - 2 * n] = flag ^ 1;
}
F(i, 1, 2 * n) cout << res[i]; cout << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int st = clock();
int t = 1;
F(i, 1, t) sol();
// cout << (clock() - st) / 1000.0 << "\n";
return 0;
}
//sldl
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28496kb
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: 0ms
memory: 28428kb
input:
3 7 2 5 1 3 4 6 2 6 4 5 2 4 5 6
output:
100101
result:
ok Output is valid. OK
Test #3:
score: 0
Accepted
time: 0ms
memory: 24108kb
input:
1 0
output:
01
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 0ms
memory: 28236kb
input:
1 1 1 2
output:
10
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 0ms
memory: 28204kb
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: 28332kb
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: 0
Accepted
time: 3ms
memory: 28140kb
input:
4 9 4 7 3 8 1 5 2 7 2 8 6 8 7 8 1 4 1 6
output:
10101001
result:
ok Output is valid. OK
Test #8:
score: 0
Accepted
time: 3ms
memory: 28236kb
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:
0010111010
result:
ok Output is valid. OK
Test #9:
score: 0
Accepted
time: 0ms
memory: 28292kb
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:
001001110110
result:
ok Output is valid. OK
Test #10:
score: 0
Accepted
time: 0ms
memory: 28236kb
input:
12 153 1 24 16 18 7 14 1 16 20 21 9 14 21 22 4 5 17 24 4 12 5 17 13 24 14 15 12 23 12 16 8 11 14 24 9 16 2 5 6 19 11 17 4 22 4 7 6 16 7 20 8 15 5 24 2 10 10 21 21 24 1 12 11 19 18 21 18 24 12 17 13 22 7 9 13 23 4 9 11 13 15 21 5 7 2 4 15 16 17 19 11 16 11 20 7 8 4 15 13 14 6 18 2 19 9 13 23 24 4 21 ...
output:
111100011001101110000001
result:
ok Output is valid. OK
Test #11:
score: 0
Accepted
time: 4ms
memory: 30256kb
input:
259 33757 472 500 65 336 138 469 307 442 427 458 43 239 17 508 460 466 108 393 79 92 250 483 44 277 17 132 35 57 155 499 184 474 246 272 274 418 457 458 338 372 196 514 31 208 117 187 90 229 153 284 189 355 16 337 146 456 269 271 279 412 305 336 303 441 399 472 85 286 91 97 157 437 137 379 71 360 27...
output:
111000110010111001101110000111000000011111100010101100100010001110101101111110000111000101111100010110101110111100011010101100111010010011101010011010011010011101100111001011110001100111000010101011100110110001001010010010101010010110011001100001010110000100001110101110000010110001010010000110110011...
result:
ok Output is valid. OK
Test #12:
score: 0
Accepted
time: 37ms
memory: 30320kb
input:
811 265557 217 1153 383 1609 165 177 612 1602 1057 1428 37 436 135 1200 368 684 448 722 145 1583 325 1052 246 480 74 148 122 1111 1256 1327 304 1070 1285 1542 802 813 454 1563 265 1193 94 848 432 1156 429 1194 427 1230 1152 1406 1329 1355 702 845 591 1232 877 1288 1257 1549 340 659 1080 1333 910 137...
output:
110111111101000111011111111101011110100101000101001000000001111000101111010100100101110100110101000111110100111100100110111110100101110101111100011110101010011001101010100110010011011111011011110101111111011000001100001101110111111110100010011100011110111111010001011111110110011111100001010011011101...
result:
ok Output is valid. OK
Test #13:
score: -100
Memory Limit Exceeded
input:
1691 323743 1246 2397 1445 2647 2010 2806 2001 2896 802 2258 2679 2976 2203 2875 2445 2698 137 3004 536 1800 2316 2520 594 1517 279 1558 1934 2871 57 1358 357 976 1764 2672 869 2137 1694 2201 491 1906 1177 1414 1304 1377 2454 2653 626 2637 1425 1677 620 876 1326 2085 404 874 626 1565 136 597 2885 31...