QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#641108 | #6817. Permutation Puzzle | czc | WA | 377ms | 37756kb | C++23 | 2.1kb | 2024-10-14 18:34:15 | 2024-10-14 18:37:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
vector<int> G[maxn],RG[maxn];
int n,m,ind[maxn];
int ind1[maxn],ind2[maxn],a[maxn],vis[maxn];
int fmn[maxn],fmx[maxn];
inline void solve(){
set<int> st;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) vis[i]=0,ind1[i]=ind2[i]=ind[i]=0;
for(int i=1;i<=n;i++) G[i].clear(),RG[i].clear();
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]) fmn[i]=fmx[i]=a[i];
else fmn[i]=1,fmx[i]=n;
vis[a[i]]=1;
}
for(int i=1;i<=n;i++){
if(!vis[i]){
st.insert(i);
}
}
for(int i=1,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
G[x].push_back(y);
RG[y].push_back(x);
ind1[y]++;
ind2[x]++;
ind[y]++;
}
queue<int> q;
for(int i=1;i<=n;i++) if(!ind1[i]) q.push(i);
while(!q.empty()){
int x=q.front();
q.pop();
for(auto y:G[x]){
ind1[y]--;
fmn[y]=max(fmn[y],fmn[x]);
if(!ind1[y]) q.push(y);
}
}
for(int i=1;i<=n;i++) if(!ind2[i]) q.push(i);
while(!q.empty()){
int x=q.front();
q.pop();
for(auto y:RG[x]){
ind2[y]--;
fmx[y]=min(fmx[y],fmx[x]);
if(!ind2[y]) q.push(y);
}
}
priority_queue< pair<int,int> > q2;
for(int i=1;i<=n;i++){
if(!ind[i]){
q2.push({-fmx[i],i});
}
}
while(!q2.empty()){
int x=q2.top().second;
q2.pop();
if(a[x]==0){
auto it=st.lower_bound(fmn[x]);
if(it!=st.end() && (*it)<=fmx[x]){
a[x]=*it;
st.erase(it);
}
else{
puts("-1");
return ;
}
}
for(auto y:G[x]){
ind[y]--;
if(!ind[y]) q2.push({-fmx[y],y});
}
}
for(int i=1;i<=n;i++) printf("%d ",a[i]);
printf("\n");
}
int main(){
int T;
scanf("%d",&T);
while(T--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 10244kb
input:
2 4 4 1 0 0 4 1 2 1 3 2 4 3 4 3 2 0 3 1 1 2 3 1
output:
1 3 2 4 2 3 1
result:
ok All correct (2 test cases)
Test #2:
score: 0
Accepted
time: 0ms
memory: 7724kb
input:
1 4 4 1 4 0 0 1 2 1 3 2 4 3 4
output:
-1
result:
ok All correct (1 test case)
Test #3:
score: 0
Accepted
time: 0ms
memory: 9960kb
input:
3 10 15 5 2 4 8 9 6 10 7 1 3 6 7 2 5 8 4 9 1 9 2 1 6 9 3 2 3 10 6 2 7 9 4 3 1 9 7 1 5 8 5 10 15 6 7 10 5 2 9 1 0 8 3 9 6 5 1 8 3 7 1 8 9 10 6 1 2 7 4 10 3 2 6 1 6 5 10 4 1 4 2 7 8 5 5 1 0 0 3 5 1 2 1 3 2 4 3 4 3 5
output:
5 2 4 8 9 6 10 7 1 3 6 7 10 5 2 9 1 4 8 3 -1
result:
ok All correct (3 test cases)
Test #4:
score: 0
Accepted
time: 77ms
memory: 10256kb
input:
20000 10 20 0 2 4 8 9 0 10 7 1 3 5 7 9 8 4 7 6 4 9 3 8 5 9 1 10 1 4 5 10 8 6 7 1 5 2 6 8 4 9 4 9 5 9 2 2 5 10 5 6 5 10 20 6 7 0 5 0 0 1 0 0 3 7 10 7 9 4 3 5 1 5 4 9 6 10 6 9 3 5 10 8 1 2 6 10 9 7 6 2 3 7 3 7 5 10 1 1 3 6 3 5 6 10 20 6 10 0 0 9 3 0 0 8 2 10 7 5 2 3 1 3 2 10 2 4 7 6 8 3 8 3 6 7 9 4 8 ...
output:
6 2 4 8 9 5 10 7 1 3 6 7 10 5 2 9 1 4 8 3 6 10 1 4 9 3 7 5 8 2 9 6 5 4 8 1 3 10 7 2 1 8 2 9 7 6 5 3 10 4 9 3 2 6 4 7 10 5 1 8 10 9 8 4 6 7 2 5 3 1 2 4 5 9 7 3 8 1 10 6 8 4 5 9 6 7 1 2 3 10 7 6 9 5 4 3 2 1 10 8 2 7 1 3 9 6 5 8 4 10 8 2 6 10 5 9 4 3 1 7 6 4 10 2 1 3 5 9 7 8 3 10 4 9 2 1 7...
result:
ok All correct (20000 test cases)
Test #5:
score: 0
Accepted
time: 111ms
memory: 10380kb
input:
100 2000 5000 1000 720 1741 1004 915 268 0 0 172 0 0 0 0 1036 1680 1716 102 0 0 1687 1498 0 0 0 0 0 0 233 0 0 997 0 0 1090 0 0 1546 1847 1542 0 0 1125 0 1963 1958 1735 0 1755 556 278 644 1308 983 0 0 587 1875 0 1928 1264 864 0 0 820 0 1612 1708 1020 1646 147 0 1679 1284 0 0 1777 351 0 825 882 0 1537...
output:
1000 720 1741 1004 915 268 323 108 172 575 335 1229 1093 1036 1680 1716 102 1998 1991 1687 1498 352 1989 885 450 366 240 233 940 973 997 1122 451 1090 1988 93 1546 1847 1542 48 1982 1125 1981 1963 1958 1735 1972 1755 556 278 644 1308 983 1990 785 587 1875 536 1928 1264 864 1133 782 820 242 1612 1708...
result:
ok All correct (100 test cases)
Test #6:
score: 0
Accepted
time: 377ms
memory: 37568kb
input:
1 200000 500000 0 60723 0 0 0 35743 0 0 0 87302 0 55295 0 0 0 8888 0 57144 0 0 0 80209 33170 0 0 0 0 0 0 40033 0 47671 58598 0 0 0 37777 0 0 53734 85703 0 38888 0 0 0 58625 94744 60257 66946 0 0 0 35068 0 33207 0 0 0 0 0 0 0 0 0 0 0 0 0 76316 0 55226 0 40032 0 0 50496 0 0 33217 169130 0 54555 0 0 0 ...
output:
98582 60723 61571 38674 47433 35743 43444 58086 199997 87302 199987 55295 60886 36340 67552 8888 199986 57144 23522 34557 86787 80209 33170 37928 42661 86837 62158 46432 80807 40033 43193 47671 58598 199985 64969 199984 37777 75974 69516 53734 85703 53681 38888 19954 36849 199983 58625 94744 60257 6...
result:
ok All correct (1 test case)
Test #7:
score: 0
Accepted
time: 61ms
memory: 10512kb
input:
100 1982 2500 0 12 22 24 33 0 0 0 84 93 0 121 0 140 151 160 176 177 202 0 0 239 0 254 0 0 0 322 0 0 341 0 349 0 360 361 0 0 0 381 0 0 392 394 397 405 412 428 441 0 451 472 0 476 0 0 480 493 502 0 0 0 559 0 588 0 0 0 618 0 654 658 661 668 689 0 0 0 0 0 740 755 761 0 772 0 780 0 0 804 817 0 845 0 0 88...
output:
1 12 22 24 33 69 71 77 84 93 103 121 125 140 151 160 176 177 202 218 219 239 241 254 298 303 305 322 327 328 341 343 349 353 360 361 362 363 368 381 382 383 392 394 397 405 412 428 441 443 451 472 475 476 478 479 480 493 502 509 524 527 559 564 588 589 600 605 618 625 654 658 661 668 689 720 721 722...
result:
ok All correct (100 test cases)
Test #8:
score: 0
Accepted
time: 51ms
memory: 10304kb
input:
100 1982 2500 1 0 0 16 0 0 0 0 0 0 0 78 0 0 0 0 0 0 0 175 0 0 187 188 0 225 0 0 0 0 276 0 0 307 0 0 0 0 357 0 0 408 410 0 422 433 0 0 461 0 474 0 517 528 0 551 559 0 564 0 579 587 0 0 0 625 636 639 0 659 0 0 0 0 0 0 0 722 0 0 0 0 769 775 0 0 0 0 0 808 0 0 0 829 831 0 0 0 0 885 887 0 0 895 910 0 0 0 ...
output:
1 4 5 16 34 35 37 40 45 46 47 78 139 141 143 145 148 149 152 175 176 178 187 188 195 225 229 233 234 243 276 293 294 307 327 328 329 330 357 382 383 408 410 412 422 433 436 437 461 462 474 494 517 528 529 551 559 560 564 568 579 587 598 599 600 625 636 639 640 659 673 674 675 676 677 680 682 722 730...
result:
ok All correct (100 test cases)
Test #9:
score: 0
Accepted
time: 79ms
memory: 10712kb
input:
10 19982 25000 1 0 11 27 47 0 70 86 88 90 99 109 118 127 147 173 0 184 0 0 235 0 0 258 259 276 279 287 300 302 307 0 320 360 0 403 0 456 463 466 474 481 484 486 508 509 511 516 524 529 0 0 547 560 563 0 574 576 627 628 633 0 646 654 659 668 677 0 706 711 713 0 721 0 0 753 766 785 0 796 803 0 809 0 8...
output:
-1 1 44 45 47 48 51 53 70 80 91 124 126 127 128 153 157 158 161 212 216 217 218 219 247 249 250 251 252 262 266 267 269 273 274 296 298 309 315 345 346 348 349 350 366 368 399 401 402 431 436 445 448 456 460 461 470 479 480 481 500 501 502 526 528 546 547 548 550 564 567 568 600 615 631 637 639 665 ...
result:
ok All correct (10 test cases)
Test #10:
score: 0
Accepted
time: 153ms
memory: 37756kb
input:
1 199982 300000 0 18 0 37 45 46 0 0 74 0 0 103 0 0 0 0 120 0 0 0 170 172 175 0 217 247 263 0 0 0 0 0 0 333 0 0 390 0 0 0 0 442 463 0 0 0 0 0 0 572 573 0 0 0 0 0 624 626 627 0 636 642 0 0 0 681 0 0 0 0 0 0 0 0 0 0 0 0 790 0 0 0 812 0 0 839 849 0 0 871 0 874 877 0 0 909 0 949 0 0 0 971 0 0 982 0 0 0 1...
output:
1 18 19 37 45 46 52 53 74 75 76 103 105 106 108 109 120 138 141 142 170 172 175 177 217 247 263 281 282 283 284 285 289 333 359 360 390 419 420 421 422 442 463 517 518 519 520 521 522 572 573 597 598 599 600 601 624 626 627 628 636 642 649 650 655 681 757 758 759 760 761 764 765 766 768 769 770 771 ...
result:
ok All correct (1 test case)
Test #11:
score: 0
Accepted
time: 168ms
memory: 24472kb
input:
2 99999 150000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
1 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 46...
result:
ok All correct (2 test cases)
Test #12:
score: 0
Accepted
time: 116ms
memory: 24044kb
input:
2 100000 99999 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13647 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 65194 36098 0 0 0 0 0 0 0 0 0 0 0 0 43719 58008 0 0 39260 26320 0 62030 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
9442 30889 63817 44729 49979 35743 50783 61670 7334 65196 25204 99812 51632 60313 45650 99701 46320 57144 21894 24110 26804 38809 99110 63720 78428 99103 38903 54247 54240 40033 53044 47671 99002 98802 43050 28172 37777 98801 6920 53734 47208 44325 38888 37124 48294 20764 58625 94744 60257 66946 384...
result:
ok All correct (2 test cases)
Test #13:
score: 0
Accepted
time: 122ms
memory: 23484kb
input:
2 100000 99999 0 0 63817 0 0 0 50783 0 0 0 51202 55295 51632 0 0 0 0 0 0 49711 40609 0 0 63720 0 0 0 0 0 40033 0 0 0 0 0 0 0 0 6920 0 0 44325 0 0 0 20764 0 0 0 0 0 0 0 0 34894 0 0 35041 0 63406 41143 0 0 0 0 0 39526 0 0 76316 0 0 0 40032 36031 0 50496 0 17976 0 0 44994 0 10078 0 0 41138 39404 65194 ...
output:
9442 60723 63817 44729 49979 35743 50783 61670 13611 32310 51202 55295 51632 60313 45650 8888 46320 57144 43499 49711 40609 80209 33170 63720 78428 94394 30196 54247 54240 40033 53044 47671 58598 13384 43050 28172 37777 99907 6920 53734 85703 44325 38888 37124 48294 20764 58625 94744 60257 66946 384...
result:
ok All correct (2 test cases)
Test #14:
score: 0
Accepted
time: 150ms
memory: 23044kb
input:
2 100000 99999 0 0 0 0 0 0 0 0 0 0 0 55295 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47671 0 0 0 0 0 0 0 0 0 0 0 0 0 20764 0 0 0 66946 0 0 0 0 0 0 0 0 0 0 41143 0 0 0 0 0 0 0 0 0 0 0 0 40032 0 0 0 34890 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47908 0 0 0 0 0 0 0 0 0 0 26320 0 0 0 0 0 0 61928 0 0 0 0 0 0 0 0...
output:
99994 99993 99992 99990 99989 10136 99988 14618 99987 99985 99984 55295 99983 99982 99979 10569 99977 99976 10648 99975 99973 99972 99966 99965 99963 99962 7051 99961 11172 99960 99959 47671 99958 99947 99943 9827 99939 4892 8644 99938 99937 14124 99936 99933 99930 20764 99927 99926 99925 66946 9992...
result:
ok All correct (2 test cases)
Test #15:
score: 0
Accepted
time: 150ms
memory: 23268kb
input:
2 100000 99999 0 0 0 0 0 0 0 0 0 0 51202 0 0 0 0 0 0 0 0 0 0 80209 0 0 0 0 0 0 0 40033 53044 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30038 0 0 0 0 0 0 0 40032 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 87670 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 59467 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
100000 99999 99998 99997 99991 99983 99982 99979 99958 99956 51202 99955 99952 99951 99950 9617 99947 99946 99945 99943 99942 80209 99941 99934 99933 99931 99927 99922 99920 40033 53044 99919 99917 99904 99902 99897 99896 99894 99890 99889 99888 99883 99878 15348 10449 99868 99865 99863 99862 99861 ...
result:
ok All correct (2 test cases)
Test #16:
score: 0
Accepted
time: 223ms
memory: 24028kb
input:
2 100000 250000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 80090 0 0 0 0 0 0 34203 0 0 0 0 0 0 0 0 0 0 0 11234 0 0 0 0 0 0 0 0 0 0 48018 0 0 0 0 37436 0 0 0 0 0 66303 0 0 0 0 6239 0 0 0 0 0 0 0 0 80644 0 0 0 0 0 0 37451 0 57487 0 0 0 0 45836 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 83092 0 41016 0 0 0 0 0 0 0 0 0 448...
output:
34586 65298 49445 36827 65824 65508 38438 38379 62242 1018 64842 10340 57386 69606 38216 80090 84936 58285 61484 82071 51445 33673 34203 13737 56737 71993 44253 43833 55382 37387 35761 81835 25034 38223 11234 32769 61940 40630 89673 45339 43813 71451 17690 45848 43000 48018 39275 44865 56773 41672 3...
result:
ok All correct (2 test cases)
Test #17:
score: 0
Accepted
time: 193ms
memory: 15504kb
input:
5 40000 80000 18129 38665 13654 33619 39609 0 13820 22328 23050 8443 35742 0 11925 0 34948 21154 22923 22232 28669 0 0 1917 17935 0 24207 16018 30729 18572 5403 0 18619 0 37234 8945 0 0 5815 13415 0 11737 30828 13193 29168 29045 10760 17856 0 23572 9868 27860 35844 0 14275 11174 0 22078 12665 0 3791...
output:
18129 38665 13654 33619 39609 12149 13820 22328 23050 8443 35742 1415 11925 1418 34948 21154 22923 22232 28669 32297 29518 1917 17935 22282 24207 16018 30729 18572 5403 24017 18619 38751 37234 8945 5719 35538 5815 13415 823 11737 30828 13193 29168 29045 10760 17856 28248 23572 9868 27860 35844 248 1...
result:
ok All correct (5 test cases)
Test #18:
score: 0
Accepted
time: 214ms
memory: 35984kb
input:
1 200000 500000 46704 101007 0 66411 0 68826 0 75916 60211 39543 40522 0 65252 0 45544 74261 0 0 36948 0 52409 67884 34795 42729 110099 0 0 0 36698 0 41196 0 69266 100780 59396 0 57785 0 69706 33132 34550 0 36905 126695 54109 0 0 0 64835 63380 33941 73095 39195 0 0 0 0 0 60505 0 82496 68372 40720 0 ...
output:
46704 101007 117886 66411 53575 68826 40040 75916 60211 39543 40522 68143 65252 41562 45544 74261 44902 52070 36948 80047 52409 67884 34795 42729 110099 49155 57925 125619 36698 87579 41196 137062 69266 100780 59396 102488 57785 34551 69706 33132 34550 49167 36905 126695 54109 33071 159289 42148 648...
result:
ok All correct (1 test case)
Extra Test:
score: -3
Extra Test Failed : Wrong Answer on 1
time: 4ms
memory: 10344kb
input:
1 3000 5000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2940 0 0 0 0 0 0 0 0 0 0 0 0 371 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
2989 3000 2999 2992 2858 2937 2856 2997 2803 2800 2930 2927 2996 133 2794 2988 2971 2854 2745 2851 2740 2853 2744 2974 2799 6 2730 2725 2701 2924 2696 2986 2980 2692 68 2689 2673 2669 2985 2923 2793 2912 2928 2982 2966 2663 2984 2956 2967 2659 2721 2656 2911 2647 2646 2790 2632 2602 2568 2567 2562 2...
result:
wrong answer The solution does not satisfy p[169] < p[1800]. (test case 1)