QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#771919 | #7861. Inverse Topological Sort | caijianhong | TL | 61ms | 9552kb | C++23 | 2.1kb | 2024-11-22 16:18:20 | 2024-11-22 19:55:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
int n, deg[100010], m;
basic_string<int> g[100010];
vector<int> toposort(int op) {
// op == -1 min
// op == 1 max
priority_queue<int> q;
vector<int> ret;
for (int i = 1; i <= n; i++) deg[i] = 0;
for (int i = 1; i <= n; i++) for (int v : g[i]) deg[v] += 1;
for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i * op);
while (!q.empty()) {
int u = q.top() * op; q.pop();
ret.push_back(u);
for (int v : g[u]) if (--deg[v] == 0) q.push(v * op);
}
return ret;
}
vector<int> a, b;
void solvea(int l, int r) {
if (l >= r) return ;
int lst = a[l], pre = l, llst = -1;
for (int i = l + 1; i < r; i++) {
if (lst > a[i]) {
if (llst == -1 || llst < a[i]) g[lst] += a[i], llst = a[i];
}
else solvea(pre + 1, i), lst = a[i], pre = i, llst = -1;
}
solvea(pre + 1, r);
}
void solveb(int l, int r) {
if (l >= r) return ;
int lst = b[l], pre = l, llst = -1;
for (int i = l + 1; i < r; i++) {
if (lst < b[i]) {
if (llst == -1 || llst > b[i]) g[lst] += b[i], llst = b[i];
} else solveb(pre + 1, i), lst = b[i], pre = i, llst = -1;
}
solveb(pre + 1, r);
}
int main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cin >> n;
a.resize(n);
b.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
solvea(0, n);
solveb(0, n);
#ifdef LOCAL
for (int i = 1; i <= n; i++) for (int v : g[i]) cout << i << " " << v << endl;
#endif
if (toposort(-1) == a && toposort(+1) == b) {
cout << "Yes" << endl;
for (int i = 1; i <= n; i++) {
auto bg = begin(g[i]), ed = end(g[i]);
sort(bg, ed), g[i].erase(unique(bg, ed), ed);
m += (int)g[i].size();
}
cout << m << endl;
for (int i = 1; i <= n; i++) for (int v : g[i]) cout << i << " " << v << endl;
} else {
cout << "No" << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 6960kb
input:
3 1 2 3 1 2 3
output:
Yes 2 1 2 2 3
result:
ok n=3
Test #2:
score: 0
Accepted
time: 2ms
memory: 6764kb
input:
3 1 2 3 3 2 1
output:
Yes 0
result:
ok n=3
Test #3:
score: 0
Accepted
time: 2ms
memory: 6796kb
input:
3 3 2 1 1 2 3
output:
No
result:
ok n=3
Test #4:
score: 0
Accepted
time: 2ms
memory: 7000kb
input:
10 6 8 9 4 1 3 7 5 10 2 8 6 9 10 4 7 5 3 2 1
output:
Yes 10 4 1 4 3 4 5 4 7 6 9 7 5 9 4 9 7 9 10 10 2
result:
ok n=10
Test #5:
score: 0
Accepted
time: 2ms
memory: 6692kb
input:
10 4 2 5 6 7 8 9 1 3 10 8 7 9 6 5 4 2 1 10 3
output:
Yes 6 1 3 1 10 4 2 7 9 9 1 9 3
result:
ok n=10
Test #6:
score: 0
Accepted
time: 2ms
memory: 6792kb
input:
100 5 16 25 26 36 28 42 46 2 38 48 23 29 30 31 12 40 51 58 64 71 75 83 14 68 74 79 84 86 88 56 6 39 92 9 11 4 47 3 13 15 8 49 54 32 45 61 33 66 72 80 24 69 89 21 82 93 94 27 76 90 10 18 77 78 57 95 7 50 81 96 97 35 19 44 20 55 63 34 60 67 22 73 52 87 91 65 43 85 37 62 53 98 1 41 70 99 59 100 17 92 8...
output:
Yes 148 1 41 2 3 2 7 2 8 2 13 2 15 2 33 2 40 2 86 3 17 3 20 3 37 3 43 3 53 3 57 3 62 3 65 3 78 3 85 3 91 4 24 4 61 4 80 7 34 7 52 7 60 7 81 7 96 8 10 8 18 8 19 8 27 8 35 8 76 8 87 8 94 9 32 10 77 11 4 11 54 11 93 14 74 14 79 15 8 15 21 15 50 15 72 15 95 16 71 19 22 19 44 19 63 19 67 20 59 23 29 23 3...
result:
ok n=100
Test #7:
score: 0
Accepted
time: 2ms
memory: 6804kb
input:
1000 11 2 29 50 53 54 155 162 211 213 223 240 270 226 243 276 288 304 315 341 249 358 359 381 178 402 51 417 434 163 459 466 471 498 327 464 518 527 549 559 113 581 589 60 347 594 504 593 598 603 607 610 619 648 649 658 681 684 416 686 153 712 575 741 349 382 759 322 17 289 763 764 774 718 777 9 637...
output:
Yes 1830 1 761 1 964 2 3 2 4 2 5 2 6 2 7 2 8 2 18 2 21 2 23 2 35 2 46 2 63 2 72 2 74 2 78 2 84 2 104 2 117 2 125 2 132 2 147 2 169 2 170 2 195 2 206 2 241 2 280 2 295 2 343 2 561 2 566 2 598 2 762 2 901 3 12 3 13 3 26 3 27 3 28 3 39 3 40 3 43 3 48 3 87 3 106 3 121 3 122 3 126 3 144 3 157 3 167 3 173...
result:
ok n=1000
Test #8:
score: 0
Accepted
time: 41ms
memory: 9056kb
input:
100000 1 5 10 12 13 14 16 17 18 19 21 27 28 33 37 40 41 44 45 49 50 51 52 54 57 58 62 64 67 69 71 72 74 75 77 78 79 80 84 89 93 95 96 100 102 104 111 113 115 117 118 119 120 121 122 123 124 126 127 129 132 135 136 138 139 142 144 150 151 152 153 154 155 156 164 166 167 170 174 177 178 180 181 182 18...
output:
Yes 78810 1 17591 4 4130 4 16417 8 660 9 8007 9 12867 9 17691 9 50540 9 68381 10 52401 10 52585 12 9486 12 56242 12 72683 13 88560 16 31508 16 60003 16 77358 16 79923 17 67167 18 648 18 40557 18 41191 18 65533 23 39356 23 88336 28 7737 28 89241 29 25359 32 74286 39 54971 39 76038 40 99843 43 16914 4...
result:
ok n=100000
Test #9:
score: 0
Accepted
time: 61ms
memory: 9552kb
input:
100000 40 84 102 116 124 157 177 191 193 199 256 259 293 300 304 326 430 439 473 477 489 511 515 518 547 583 593 630 664 697 747 751 769 787 789 892 928 945 963 971 978 1052 1063 1067 1077 1080 1088 1101 1136 1143 1172 1180 1198 1274 1312 1359 1361 1380 1382 1404 1414 1428 1435 1466 1475 1497 1517 1...
output:
Yes 183695 1 2 1 8 1 10 1 15 1 34 1 43 1 45 1 49 1 72 1 73 1 85 1 93 1 103 1 110 1 113 1 114 1 129 1 135 1 138 1 140 1 143 1 145 1 151 1 159 1 161 1 165 1 167 1 173 1 178 1 183 1 187 1 190 1 194 1 208 1 217 1 218 1 224 1 229 1 239 1 243 1 248 1 252 1 263 1 264 1 265 1 291 1 305 1 309 1 311 1 317 1 3...
result:
ok n=100000
Test #10:
score: 0
Accepted
time: 27ms
memory: 9096kb
input:
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102...
output:
Yes 1988 28 25410 43 29010 44 85590 50 3874 51 75818 257 2501 386 65575 473 5377 526 40026 540 66586 547 86971 567 23179 568 35479 612 91704 712 45430 714 36371 765 27333 869 11574 996 42158 1031 80047 1105 60132 1160 14694 1249 4184 1258 98667 1338 29021 1339 42912 1377 87865 1426 76875 1536 58201 ...
result:
ok n=100000
Test #11:
score: 0
Accepted
time: 57ms
memory: 9368kb
input:
100000 4 6 12 16 20 23 24 27 32 34 36 39 46 54 68 76 77 81 86 88 95 99 103 107 112 113 117 120 125 140 142 143 149 158 161 167 171 174 176 187 190 192 195 198 200 206 207 211 217 222 226 227 231 233 239 240 241 245 247 249 264 274 275 276 277 280 288 290 296 303 305 312 321 329 333 336 338 339 341 3...
output:
Yes 122343 1 15134 1 20972 1 30430 1 58468 2 309 2 1584 2 4710 2 13749 2 47921 2 71317 3 13876 6 26403 6 42488 6 52305 6 59182 6 90079 7 502 7 583 7 10858 7 11026 7 13025 7 20996 7 21570 7 21670 7 22066 7 26627 7 27603 7 31405 7 36322 7 37771 7 45046 7 72064 7 77015 7 77815 7 98222 8 2008 8 7644 8 8...
result:
ok n=100000
Test #12:
score: 0
Accepted
time: 31ms
memory: 8900kb
input:
100000 1 2 4 5 6 7 10 13 14 15 16 20 21 22 24 25 26 28 29 30 31 33 34 35 36 37 38 39 40 43 44 45 46 47 48 51 52 55 56 57 58 59 62 65 66 67 68 69 70 71 72 73 74 75 76 77 78 80 81 82 85 87 89 91 92 93 94 97 98 99 100 101 102 103 104 105 106 107 111 112 113 115 117 119 120 121 123 124 128 130 132 133 1...
output:
Yes 44465 3 17561 3 71695 10 15024 11 12141 12 98511 13 8377 13 25354 13 29204 13 50166 13 72446 13 95895 18 13280 19 11292 19 39683 26 33568 26 47561 26 47772 33 18117 35 68646 35 76958 39 63164 42 61908 43 57505 43 77968 46 19767 46 35061 46 50623 46 56478 46 83128 47 97602 48 90140 53 1575 57 554...
result:
ok n=100000
Test #13:
score: 0
Accepted
time: 39ms
memory: 9336kb
input:
100000 33 43 47 65 67 82 88 95 96 113 130 133 140 232 262 266 282 286 298 299 303 324 326 342 352 354 356 359 362 363 364 369 392 398 408 435 442 454 460 489 508 518 537 556 572 574 580 592 613 616 629 650 652 674 684 718 721 724 732 734 801 809 819 831 845 853 856 878 879 895 897 935 946 956 958 96...
output:
Yes 167027 1 242 1 248 1 416 1 560 1 663 1 690 1 836 1 851 1 911 1 1059 1 1285 1 1290 1 1415 1 1473 1 1587 1 1665 1 1747 1 1776 1 1794 1 2001 1 2109 1 2138 1 2195 1 2314 1 2319 1 2449 1 2485 1 2621 1 2625 1 2871 1 2927 1 2939 1 2991 1 2998 1 3010 1 3032 1 3240 1 3389 1 3540 1 3542 1 3652 1 3759 1 37...
result:
ok n=100000
Test #14:
score: 0
Accepted
time: 36ms
memory: 9092kb
input:
100000 38535 3433 18670 53850 31420 79252 3155 90709 7043 47690 20905 66663 16655 77812 19606 78158 23549 54025 44700 24119 42542 85555 31117 68856 35627 37419 26767 46031 72252 71511 80835 47732 77030 61434 51792 98165 71334 70644 79996 87007 93335 56112 86306 3040 10776 30683 80961 96794 12323 656...
output:
Yes 199973 1 2 1 8 1 23 1 39 1 41 1 131 1 3185 1 5106 2 3 2 4 2 7 2 9 2 24 2 25 2 146 2 431 2 1930 2 2039 2 3125 2 4664 2 54518 3 10 3 12 3 15 3 99 3 123 3 137 3 228 3 1027 3 3026 3 6583 4 5 4 26 4 55 4 1761 4 54710 5 17 5 114 5 158 5 261 5 567 5 813 5 3799 5 25833 5 31041 5 57790 5 87828 6 20 6 32 ...
result:
ok n=100000
Test #15:
score: 0
Accepted
time: 15ms
memory: 8512kb
input:
100000 1 5 7 8 24 29 32 36 39 41 43 44 46 47 52 54 56 58 59 64 68 69 70 73 75 77 79 82 84 86 88 90 92 93 95 98 99 101 102 104 105 108 112 114 115 116 118 123 126 127 128 133 134 139 140 143 145 147 152 153 154 156 160 161 163 165 169 170 176 178 179 180 184 186 187 188 192 193 195 199 200 204 205 20...
output:
No
result:
ok n=100000
Test #16:
score: 0
Accepted
time: 3ms
memory: 8292kb
input:
100000 1 3 4 7 10 11 13 17 18 19 21 22 25 27 28 29 31 35 36 37 38 42 49 50 53 56 57 58 60 62 63 64 68 70 71 79 80 82 83 85 86 87 88 90 93 94 98 103 105 109 110 111 112 116 121 123 127 134 138 139 142 143 148 151 154 156 158 159 160 162 164 166 168 171 172 173 174 175 176 177 180 184 186 187 189 193 ...
output:
No
result:
ok n=100000
Test #17:
score: 0
Accepted
time: 11ms
memory: 8000kb
input:
100000 1 2 8 9 11 14 19 21 22 24 25 28 33 34 35 36 43 49 51 55 57 59 62 64 68 69 70 71 72 75 76 78 79 80 81 82 83 87 88 89 91 92 98 99 105 106 107 111 112 116 118 123 124 125 128 131 133 138 139 141 142 143 146 147 152 154 155 159 161 162 163 164 165 169 172 173 174 175 179 183 184 185 186 187 190 1...
output:
No
result:
ok n=100000
Test #18:
score: 0
Accepted
time: 20ms
memory: 9020kb
input:
100000 60 134 140 182 208 256 291 327 364 395 404 419 439 444 457 469 486 510 527 561 569 595 611 612 645 654 710 778 792 794 810 832 873 890 900 901 911 914 942 946 978 1022 1057 1060 1083 1094 1095 1146 1154 1155 1280 1323 1336 1368 1379 1388 1395 1480 1500 1509 1548 1573 1580 1597 1601 1622 1629 ...
output:
No
result:
ok n=100000
Test #19:
score: 0
Accepted
time: 18ms
memory: 8256kb
input:
100000 52072 2 3 50731 5 75525 49404 8 52753 2744 11 34189 13 48355 15 16 17 50376 86416 20 21 56114 23 20072 25 53838 48273 63338 29 30 60156 6205 8084 34 35 36 48381 71655 72484 63969 88506 59722 27083 5369 44672 86160 39926 48 49 8962 51 47113 53 69142 55 66271 24245 74454 59 72556 61 35930 86895...
output:
No
result:
ok n=100000
Test #20:
score: 0
Accepted
time: 15ms
memory: 8316kb
input:
100000 13821 33496 19412 85158 61916 61576 41795 39637 42402 12256 37931 7198 19499 24983 15918 19942 56948 7239 17886 24328 17628 63213 4681 90112 37749 17984 25778 75577 33274 43479 47779 64385 77793 82833 15116 96895 87829 30340 25506 7179 48585 77809 44101 91839 93597 69594 37840 3271 4541 68178...
output:
No
result:
ok n=100000
Test #21:
score: 0
Accepted
time: 2ms
memory: 6796kb
input:
1 1 1
output:
Yes 0
result:
ok n=1
Extra Test:
score: -3
Extra Test Failed : Time Limit Exceeded on 4
input:
100000 100000 99999 99998 99997 99996 99995 99994 99993 99992 99991 99990 99989 99988 99987 99986 99985 99984 99983 99982 99981 99980 99979 99978 99977 99976 99975 99974 99973 99972 99971 99970 99969 99968 99967 99966 99965 99964 99963 99962 99961 99960 99959 99958 99957 99956 99955 99954 99953 9995...