QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#559671 | #8730. Particija | Mysterious_Cat | 30 | 46ms | 26100kb | C++14 | 3.4kb | 2024-09-12 08:14:36 | 2024-09-12 08:14:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int T, k, n, tot, totd, ans, sum, mA, mB, h[N], a[N], b[N], dfn[N], low[N], rt[N], A[N], B[N], bri[N];
bool vis[N];
struct Edge {
int v, nxt;
} e[N * 2];
void add(int u, int v) {
e[++tot].v = v;
e[tot].nxt = h[u];
h[u] = tot;
}
void dfs(int u, int fa, int t) {
vis[u] = 1;
dfn[u] = ++totd;
low[u] = totd;
rt[u] = t;
A[u] = u <= n;
B[u] = u > n;
bri[u] = -1;
for (int i = h[u]; i; i = e[i].nxt) {
if ((i ^ 1) == fa) {
continue;
}
int v = e[i].v;
if (!vis[v]) {
dfs(v, i, t);
if (low[v] > low[u]) {
bri[v] = u;
}
low[u] = min(low[u], low[v]);
A[u] += A[v];
B[u] += B[v];
} else {
low[u] = min(low[u], dfn[v]);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T >> k;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
if (n == 1) {
cout << "1\n";
continue;
}
tot = 1;
for (int i = 1; i <= n * 2; i++) {
h[i] = 0;
}
for (int i = 1; i <= n; i++) {
int u = a[i], v = b[i] + n;
add(u, v);
add(v, u);
}
totd = 0;
for (int i = 1; i <= n * 2; i++) {
vis[i] = 0;
bri[i] = 0;
}
for (int i = 1; i <= n * 2; i++) {
if (!vis[i]) {
dfs(i, 0, i);
}
}
sum = 0;
for (int i = 1; i <= n * 2; i++) {
if (rt[i] == i) {
sum += min(A[i], B[i]);
}
}
ans = sum;
mA = 0;
mB = 0;
for (int i = 1; i <= n * 2; i++) {
if (rt[i] == i) {
mA = max(mA, A[i]);
mB = max(mB, B[i]);
}
}
if (k == 2) {
for (int i = 1; i <= n; i++) {
int u = a[i], v = b[i] + n;
if (bri[u] == v) {
int uA = A[u], uB = B[u];
int vA = A[rt[u]] - A[u], vB = B[rt[u]] - B[u];
int now = sum - min(A[rt[u]], B[rt[u]]) + min(uA, uB) + min(vA, vB);
if (uA >= uB) {
ans = max(ans, now + min(uA - uB, mB));
} else {
ans = max(ans, now + min(uB - uA, mA));
}
if (vA >= vB) {
ans = max(ans, now + min(vA - vB, mB));
} else {
ans = max(ans, now + min(vB - vA, mA));
}
} else if (bri[v] == u) {
int uA = A[rt[v]] - A[v], uB = B[rt[v]] - B[v];
int vA = A[v], vB = B[v];
int now = sum - min(A[rt[v]], B[rt[v]]) + min(uA, uB) + min(vA, vB);
if (uA >= uB) {
ans = max(ans, now + min(uA - uB, mB));
} else {
ans = max(ans, now + min(uB - uA, mA));
}
if (vA >= vB) {
ans = max(ans, now + min(vA - vB, mB));
} else {
ans = max(ans, now + min(vB - vA, mA));
}
} else {
if (A[u] >= B[u]) {
ans = max(ans, sum + min(A[u] - B[u], mB));
} else {
ans = max(ans, sum + min(B[u] - A[u], mA));
}
}
}
} else if (k == 1) {
for (int i = 1; i <= n; i++) {
int u = a[i], v = b[i] + n;
if (bri[u] == v) {
int uA = A[u], uB = B[u];
int vA = A[rt[u]] - A[u], vB = B[rt[u]] - B[u];
int now = sum - min(A[rt[u]], B[rt[u]]) + min(uA, uB) + min(vA, vB);
ans = min(ans, now);
} else if (bri[v] == u) {
int uA = A[rt[v]] - A[v], uB = B[rt[v]] - B[v];
int vA = A[v], vB = B[v];
int now = sum - min(A[rt[v]], B[rt[v]]) + min(uA, uB) + min(vA, vB);
ans = min(ans, now);
}
}
}
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 7
Accepted
Test #1:
score: 7
Accepted
time: 0ms
memory: 19908kb
input:
751 0 5 5 5 5 5 5 1 2 3 4 5 6 1 2 2 6 4 5 3 6 3 4 3 3 8 5 8 6 5 4 3 6 6 3 4 4 3 2 6 6 2 5 1 1 1 1 1 5 5 4 3 3 8 5 7 3 7 8 2 7 5 4 2 6 3 6 2 5 6 8 5 8 7 3 4 6 5 2 3 3 3 3 3 3 1 3 6 2 1 3 1 3 6 5 5 3 6 3 5 8 8 6 2 4 7 7 4 1 7 7 3 3 7 7 3 3 5 2 1 4 4 2 1 2 1 4 1 6 2 2 2 2 2 2 5 3 2 1 4 6 7 4 1 7 4 2 3 ...
output:
1 3 4 1 4 2 3 2 3 1 4 1 1 4 3 4 3 2 3 4 3 2 3 3 1 2 3 2 1 3 4 2 1 2 4 3 3 4 3 2 1 2 3 3 2 1 3 3 4 3 2 2 3 1 1 3 2 2 3 2 3 1 3 3 4 4 3 2 1 3 1 1 1 4 3 2 3 1 2 1 2 1 3 2 1 2 2 2 3 1 2 1 2 3 1 3 2 3 2 4 4 1 2 3 4 3 2 2 3 2 3 3 3 4 3 1 3 5 2 2 4 2 3 1 3 1 1 2 1 2 3 4 3 2 3 1 1 3 3 2 3 3 2 4 3 5 3 3 3 4 ...
result:
ok 751 numbers
Subtask #2:
score: 23
Accepted
Dependency #1:
100%
Accepted
Test #2:
score: 23
Accepted
time: 22ms
memory: 17948kb
input:
10000 0 1 1 1 22 3 20 22 15 8 18 14 19 15 11 18 15 15 19 20 14 10 20 19 4 18 20 22 19 9 17 2 17 8 9 22 22 8 2 15 9 19 6 15 17 9 22 22 1 6 4 1 4 1 4 4 2 2 3 3 3 2 2 1 1 2 2 65 16 27 7 58 30 4 58 59 30 45 27 58 65 15 20 26 58 48 58 25 4 39 4 16 28 28 28 33 59 30 59 19 48 41 59 58 4 65 25 20 59 42 45 5...
output:
1 9 2 1 22 1 15 3 10 1 1 2 1 2 1 10 1 2 5 3 1 17 4 4 1 5 22 1 1 2 11 5 9 4 6 2 2 7 4 8 1 1 1 1 1 6 3 4 8 9 21 1 2 1 1 2 5 5 5 1 1 3 5 5 1 6 1 4 3 1 2 1 10 1 1 11 1 1 1 17 38 2 11 4 5 1 4 1 4 2 16 5 4 25 4 23 8 9 5 6 5 1 3 1 1 1 2 2 1 1 1 2 2 3 38 24 5 5 2 4 4 2 9 7 1 6 35 4 1 3 8 8 12 4 12 2 6 2 4 1...
result:
ok 10000 numbers
Test #3:
score: 23
Accepted
time: 21ms
memory: 19992kb
input:
10000 0 5 2 4 5 3 1 3 3 3 3 3 39 3 5 24 31 18 26 12 18 16 27 1 2 20 20 4 34 4 20 14 38 2 34 13 10 25 31 23 20 2 31 30 35 22 31 6 36 29 21 22 22 4 30 11 35 14 11 10 14 21 18 10 10 22 10 10 18 29 9 4 4 9 10 10 21 36 35 13 16 34 16 18 29 31 34 10 29 9 20 42 12 13 22 25 13 11 11 3 11 13 19 41 3 20 38 11...
output:
1 16 6 12 23 24 3 4 6 3 21 5 15 3 7 7 2 10 6 8 1 2 5 3 15 2 9 3 1 19 3 15 9 1 7 1 2 2 1 6 1 9 1 2 3 8 4 11 3 12 10 14 1 2 1 2 5 14 3 4 1 24 9 3 4 9 5 5 13 1 9 3 1 1 7 1 11 9 14 3 4 6 3 6 1 5 5 1 14 8 4 5 13 4 21 4 1 18 1 1 7 1 21 29 2 12 6 4 2 1 1 1 7 4 1 1 25 2 3 3 4 4 23 2 8 1 1 14 5 3 3 10 7 2 5 ...
result:
ok 10000 numbers
Test #4:
score: 23
Accepted
time: 25ms
memory: 18068kb
input:
10000 0 42 22 41 38 29 7 20 32 9 17 1 14 19 40 17 39 26 31 12 37 10 35 24 30 8 8 34 4 11 33 36 25 17 23 36 25 15 28 38 16 2 42 21 13 24 24 23 23 41 13 23 23 23 23 13 24 24 13 24 13 41 23 13 23 24 41 27 13 24 24 24 13 24 13 13 24 36 41 24 23 36 27 13 13 13 3 3 3 3 2 2 1 4 4 2 1 1 2 2 2 2 7 2 6 6 3 6 ...
output:
6 1 1 2 4 7 17 7 2 2 2 2 1 6 19 11 7 2 13 1 10 2 1 15 2 8 2 1 3 2 2 1 1 5 1 7 4 5 1 17 6 11 8 1 16 1 2 5 1 12 4 1 17 1 1 4 4 12 2 1 1 32 22 5 1 1 51 1 1 3 5 5 1 5 5 8 1 7 2 4 2 3 4 9 1 1 1 1 5 2 6 2 1 4 1 16 3 3 1 3 11 6 2 4 6 12 1 1 6 4 1 7 1 5 1 5 1 4 2 6 5 1 1 2 4 4 14 1 26 1 2 10 2 5 10 2 11 1 2...
result:
ok 10000 numbers
Test #5:
score: 23
Accepted
time: 42ms
memory: 22296kb
input:
1 0 200000 187537 18767 101813 163606 48410 170649 76822 130218 89330 118401 172319 59998 23511 38330 144493 25491 8802 151268 189229 12042 4447 90944 3117 74158 20010 127637 161507 144844 81563 30958 14332 56186 135755 152121 71529 70034 79871 110696 85775 153001 135318 169390 85445 93814 32413 848...
output:
51015
result:
ok 1 number(s): "51015"
Test #6:
score: 23
Accepted
time: 40ms
memory: 24388kb
input:
1 0 200000 35028 121987 18839 127027 111261 194927 82719 113384 153997 35176 27076 183194 48387 128670 189053 112598 195273 194243 41109 180804 108702 177398 9342 45879 199964 197302 37350 17230 49346 57628 41197 170195 92073 107978 94705 171598 61622 70349 81374 111639 31947 13890 51348 79679 785 1...
output:
55719
result:
ok 1 number(s): "55719"
Test #7:
score: 23
Accepted
time: 38ms
memory: 24228kb
input:
1 0 200000 138519 168476 183840 134736 190461 5116 13667 159468 133786 4190 119815 130598 141492 33050 40906 139150 151429 101609 18214 120314 164914 138694 108838 154071 80487 94010 93009 38605 112559 140771 141675 16819 76448 113377 36185 115247 139527 117111 15998 148912 177636 29482 177219 17397...
output:
44165
result:
ok 1 number(s): "44165"
Test #8:
score: 23
Accepted
time: 35ms
memory: 22352kb
input:
1 0 200000 3513 67285 198547 32776 5797 143977 34496 176480 75284 72200 82905 116098 40986 199651 70240 90517 147287 14722 26032 90245 19925 80740 150300 91991 162748 29580 104389 127295 84353 172689 90217 126969 189559 117484 90336 153782 192791 125420 172800 48998 130350 43857 173431 23110 143590 ...
output:
47160
result:
ok 1 number(s): "47160"
Test #9:
score: 23
Accepted
time: 40ms
memory: 22256kb
input:
1 0 200000 83034 29934 7639 53738 86571 112470 188606 151239 174639 115197 160615 90066 70477 69825 71274 13167 157710 175588 184042 9329 195761 54008 133119 126080 123098 53084 22530 29648 108438 4312 105264 184797 71622 172887 184926 79439 143700 79447 174229 193304 64335 6959 12576 100452 94876 3...
output:
59514
result:
ok 1 number(s): "59514"
Test #10:
score: 23
Accepted
time: 46ms
memory: 26100kb
input:
1 0 200000 71735 59782 40990 159955 158396 61677 120140 108108 90472 120033 93945 159982 173388 111158 91048 38935 137669 127656 185936 153538 178865 4112 11119 21865 127899 8673 24290 154377 160836 139774 67706 95473 106305 94872 182031 185824 1449 173005 67567 184610 91139 142203 8189 20355 80902 ...
output:
59388
result:
ok 1 number(s): "59388"
Test #11:
score: 23
Accepted
time: 39ms
memory: 22336kb
input:
1 0 200000 75179 83698 165139 112108 56661 40923 34829 22484 96146 29060 71444 119149 21277 50989 105192 160539 1085 167863 131915 182220 57630 196589 4991 84041 49825 102945 192364 186625 72791 154368 185326 88337 96848 70737 40769 100184 111261 115171 65620 198383 18518 154456 106359 116713 110737...
output:
48580
result:
ok 1 number(s): "48580"
Test #12:
score: 23
Accepted
time: 39ms
memory: 22252kb
input:
1 0 200000 186148 38986 30647 142364 182882 35923 161993 54055 116406 23087 153956 22532 142684 60155 64145 9603 5183 17445 159714 12970 185475 54218 24157 84518 45909 47548 91423 158033 122406 28216 165342 12747 12658 42337 103290 29555 164211 197186 181766 122891 178563 5498 15663 157410 181528 16...
output:
21886
result:
ok 1 number(s): "21886"
Test #13:
score: 23
Accepted
time: 39ms
memory: 22256kb
input:
1 0 200000 130206 46063 98170 21224 6304 60015 55867 151125 157945 191346 117918 133897 94404 113743 181378 3827 92280 122192 90462 40961 30650 82537 30753 42205 66585 184818 173919 151847 16997 121028 113142 25101 37747 85759 98605 48681 126382 48717 158856 34089 5221 20482 107091 92811 90437 32241...
output:
50093
result:
ok 1 number(s): "50093"
Subtask #3:
score: 0
Wrong Answer
Test #14:
score: 15
Accepted
time: 2ms
memory: 18060kb
input:
719 1 8 1 1 2 1 5 1 5 1 2 5 6 1 4 4 8 7 5 5 5 5 5 5 1 4 1 2 5 8 5 2 4 7 8 6 1 3 4 4 4 4 4 4 4 4 7 4 5 5 4 4 5 5 1 7 5 3 6 2 4 5 1 4 4 5 2 1 4 2 1 3 2 1 2 2 1 8 6 8 6 2 8 4 5 6 3 4 7 4 8 8 8 4 8 2 7 2 7 2 2 2 2 3 4 1 3 8 5 7 4 8 3 3 2 8 5 6 4 4 4 7 7 4 7 7 8 7 5 2 4 4 4 4 5 2 3 1 4 6 1 1 1 1 1 1 6 5 ...
output:
2 1 1 2 2 1 3 2 2 1 1 3 2 1 1 1 2 2 2 2 2 1 1 1 1 2 1 2 1 1 3 2 2 1 2 2 3 2 2 2 2 1 1 2 3 3 1 1 3 2 2 1 2 2 2 2 3 3 1 2 1 1 3 1 3 2 2 3 3 2 1 1 2 3 1 2 1 2 2 2 1 2 1 3 1 2 1 2 2 2 2 2 2 1 2 2 1 1 1 3 2 2 1 1 1 1 2 1 1 1 1 2 2 1 2 3 1 3 1 2 1 3 2 1 2 2 2 2 2 2 3 3 1 2 2 1 1 2 2 1 1 2 4 1 1 1 2 2 2 2 ...
result:
ok 719 numbers
Test #15:
score: 0
Wrong Answer
time: 9ms
memory: 17980kb
input:
4998 1 8 8 6 4 7 7 2 2 4 4 4 4 5 5 7 7 4 9 6 1 7 7 2 9 3 6 5 8 8 8 8 8 8 8 8 8 3 3 2 1 2 2 2 17 12 12 11 3 16 11 12 17 12 16 12 10 12 6 17 3 17 5 6 5 8 11 6 10 11 11 1 8 16 14 5 8 1 2 21 12 21 4 21 12 9 4 8 3 12 5 12 5 16 11 12 9 3 21 5 12 18 17 17 2 3 21 17 21 14 12 5 12 15 8 11 18 11 14 2 21 18 7 ...
output:
3 1 1 6 8 2 2 4 1 1 1 1 2 1 1 1 1 5 2 3 10 1 3 1 4 5 1 1 3 3 1 4 2 1 1 6 4 2 1 1 1 1 2 1 17 3 8 4 7 1 1 1 5 5 6 3 1 1 1 1 1 4 2 5 1 1 1 1 4 5 10 2 1 1 21 1 5 1 1 14 1 1 2 1 4 1 1 2 2 1 8 1 8 4 3 1 1 1 1 4 1 2 1 1 6 2 1 4 3 4 4 5 1 4 5 5 1 3 3 5 2 20 2 1 7 2 1 1 1 2 1 8 4 1 1 2 2 4 1 5 4 1 2 6 2 1 1 ...
result:
wrong answer 64th numbers differ - expected: '6', found: '5'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #40:
score: 0
Wrong Answer
time: 0ms
memory: 17920kb
input:
704 2 6 5 3 1 4 4 4 4 5 6 4 5 2 7 1 4 1 4 1 1 1 7 1 4 1 6 7 5 7 5 6 6 5 5 6 6 3 4 6 1 7 3 5 8 8 2 7 4 6 1 3 4 6 6 6 6 6 6 6 6 8 1 3 1 2 8 7 4 1 8 8 8 8 5 1 8 8 8 1 6 2 6 7 3 5 8 7 7 5 8 5 7 5 5 6 6 4 5 3 6 1 3 3 3 3 4 3 7 3 3 7 3 3 3 3 1 4 5 3 6 5 7 5 3 1 5 5 1 5 4 3 5 3 6 4 1 4 2 2 2 6 2 2 1 5 2 6 ...
output:
4 3 4 2 4 5 4 4 3 4 3 5 3 4 6 2 3 5 4 3 2 4 3 4 3 4 5 2 4 4 3 5 6 2 4 4 4 5 5 5 6 5 2 4 3 4 3 3 5 4 2 5 3 2 4 2 4 3 4 3 6 3 4 2 3 4 4 4 2 3 3 5 2 6 4 3 4 6 3 6 4 4 4 5 2 3 4 5 5 4 4 4 2 4 2 3 4 6 2 3 5 2 6 2 3 3 4 6 5 3 3 5 2 3 4 3 6 4 4 4 5 4 2 2 3 2 2 4 2 2 5 5 2 2 4 2 5 2 4 3 2 3 3 4 4 4 3 4 4 5 ...
result:
wrong answer 3rd numbers differ - expected: '3', found: '4'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%