QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#424575 | #7861. Inverse Topological Sort | lmeowdn# | AC ✓ | 269ms | 50116kb | C++14 | 2.9kb | 2024-05-29 13:20:50 | 2024-11-22 19:54:44 |
Judging History
answer
//Shirasu Azusa 2024.5
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x) {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));}
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
int x=0,w=1; char c=getchar();
while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
return x*w;
}
const int N=1e5+5;
int a[N],b[N],c[N],n,d[N],f[N],g[N],deg[N],tick;
vi e[N]; vp ans; map<pii,int>ed;
priority_queue<int> q;
pii s1[N];
void add1(int x,pii y) {for(;x<=n;x+=x&-x) chmax(s1[x],y);}
pii qry1(int x,pii y=mp(0,0)) {for(;x;x-=x&-x) chmax(y,s1[x]); return y;}
signed main() {
n=read();
rep(i,1,n) c[i]=read(), a[c[i]]=i;
rep(i,1,n) d[i]=read(), b[d[i]]=i;
pii mn(N,N);
rep(i,1,n) {
int u=c[i]; static int r[N]; int t=2;
r[0]=mn.se, r[1]=qry1(b[u]).se;
rep(i,0,t-1) if(1<=r[i]&&r[i]<=n&&b[r[i]]<b[u]) {
if(ed.count(pii(r[i],u))) continue; ed[pii(r[i],u)]=1;
ans.eb(r[i],u); e[r[i]].eb(u);
}
chmin(mn,pii(b[u],u));
add1(b[u],pii(b[u],u));
}
rep(i,0,n) s1[i]=pii(0,0); mn=pii(N,N);
rep(i,1,n) {
int u=d[i]; static int r[N]; int t=2;
r[0]=mn.se, r[1]=qry1(a[u]).se;
rep(i,0,t-1) if(1<=r[i]&&r[i]<=n&&a[r[i]]<a[u]) {
if(ed.count(pii(r[i],u))) continue; ed[pii(r[i],u)]=1;
ans.eb(r[i],u); e[r[i]].eb(u);
}
chmin(mn,pii(a[u],u));
add1(a[u],pii(a[u],u));
}
rep(i,1,n) for(int j:e[i]) ++deg[j];
rep(i,1,n) if(!deg[i]) q.push(-i);
while(!q.empty()) {
int u=-q.top(); q.pop(); f[++tick]=u;
for(int v:e[u]) if(!--deg[v]) q.push(-v);
} tick=0;
rep(i,1,n) assert(!deg[i]);
rep(i,1,n) for(int j:e[i]) ++deg[j];
rep(i,1,n) if(!deg[i]) q.push(i);
while(!q.empty()) {
int u=q.top(); q.pop(); g[++tick]=u;
for(int v:e[u]) if(!--deg[v]) q.push(v);
}
rep(i,1,n) if(c[i]!=f[i]) return puts("No"), 0;
rep(i,1,n) if(d[i]!=g[i]) return puts("No"), 0;
puts("Yes");
assert(ans.size()<=min(n*(n-1)/2,1000000ll));
printf("%lld\n",(int)ans.size());
for(auto [x,y]:ans) printf("%lld %lld\n",x,y);
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 14000kb
input:
3 1 2 3 1 2 3
output:
Yes 3 1 2 1 3 2 3
result:
ok n=3
Test #2:
score: 0
Accepted
time: 0ms
memory: 12248kb
input:
3 1 2 3 3 2 1
output:
Yes 0
result:
ok n=3
Test #3:
score: 0
Accepted
time: 0ms
memory: 11772kb
input:
3 3 2 1 1 2 3
output:
No
result:
ok n=3
Test #4:
score: 0
Accepted
time: 0ms
memory: 11960kb
input:
10 6 8 9 4 1 3 7 5 10 2 8 6 9 10 4 7 5 3 2 1
output:
Yes 24 8 9 6 9 8 4 9 4 8 1 4 1 8 3 4 3 8 7 4 7 8 5 7 5 8 10 9 10 8 2 3 2 6 10 6 4 6 7 6 5 6 3 6 2 10 2 6 1
result:
ok n=10
Test #5:
score: 0
Accepted
time: 2ms
memory: 11868kb
input:
10 4 2 5 6 7 8 9 1 3 10 8 7 9 6 5 4 2 1 10 3
output:
Yes 13 4 2 8 9 7 9 8 1 2 1 8 3 1 3 8 10 1 10 4 1 9 1 4 10 4 3
result:
ok n=10
Test #6:
score: 0
Accepted
time: 0ms
memory: 12136kb
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 304 36 28 26 28 46 2 5 2 46 38 42 38 48 23 25 23 48 29 23 29 48 30 36 30 48 31 36 31 48 12 16 12 48 40 2 40 51 58 46 58 64 71 16 71 64 75 25 75 83 14 71 14 83 68 83 74 14 74 83 79 14 79 83 84 74 84 83 86 2 86 83 88 48 88 83 56 23 56 83 6 12 6 83 39 56 39 92 9 12 9 92 11 12 11 92 4 5 4 92 47 40 4...
result:
ok n=100
Test #7:
score: 0
Accepted
time: 0ms
memory: 12984kb
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 3487 11 2 270 226 240 226 270 243 226 243 276 288 211 288 315 341 155 341 315 249 341 249 381 178 288 178 381 402 29 402 381 51 402 51 434 163 178 163 498 327 358 327 498 464 243 464 527 549 11 549 527 559 155 559 527 113 249 113 589 60 113 60 589 347 549 347 594 504 518 504 594 593 594 598 2 59...
result:
ok n=1000
Test #8:
score: 0
Accepted
time: 126ms
memory: 30700kb
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 165867 866 867 518 867 879 331 333 331 1561 1563 894 1563 1678 362 363 362 1995 1996 1720 1996 2038 1937 1942 1937 2134 2136 201 2136 2407 2410 823 2410 2416 2417 2347 2417 2532 2535 1700 2535 2574 1981 1984 1981 2639 2143 2144 2143 2737 99 100 99 2737 810 811 810 2764 2769 2325 2769 2915 2621 2...
result:
ok n=100000
Test #9:
score: 0
Accepted
time: 254ms
memory: 47736kb
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 354775 511 515 193 515 1380 1382 1136 1382 2282 1882 1929 1882 2799 2823 2707 2823 4001 1479 1497 1479 4089 357 430 357 4678 4702 2707 4702 5025 4177 4186 4177 5650 818 892 818 5661 4309 4337 4309 6639 6646 1644 6646 7190 2459 2467 2459 7722 7724 430 7724 7790 3669 3678 3669 7807 1299 1312 1299 ...
result:
ok n=100000
Test #10:
score: 0
Accepted
time: 21ms
memory: 13972kb
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 3986 1577 1183 1184 1183 2149 890 891 890 2500 2501 257 2501 2698 2699 1634 2699 3534 3535 2017 3535 3873 3874 50 3874 4183 4184 1249 4184 4688 4689 4620 4689 5376 5377 473 5377 5479 4107 4108 4107 5573 3401 3402 3401 6683 3857 3858 3857 6854 6855 4664 6855 6873 6874 3556 6874 7485 6847 6848 684...
result:
ok n=100000
Test #11:
score: 0
Accepted
time: 169ms
memory: 36140kb
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 249955 504 508 383 508 1558 587 589 587 1656 1659 1533 1659 1863 1864 86 1864 2070 2078 2000 2078 2772 2773 374 2773 2846 2853 492 2853 2882 2884 1291 2884 3016 936 937 936 3065 1681 1686 1681 3109 3112 2595 3112 3109 3114 1270 3114 3815 1666 1667 1666 3835 3836 3615 3836 3835 3843 395 3843 4017...
result:
ok n=100000
Test #12:
score: 0
Accepted
time: 82ms
memory: 23964kb
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 93813 383 83 85 83 491 492 175 492 533 534 358 534 556 559 103 559 1131 546 547 546 1177 1179 564 1179 1634 1587 1588 1587 1654 1655 1434 1655 1661 468 469 468 1730 544 546 544 1907 1908 326 1908 1925 928 930 928 1955 1101 1104 1101 2167 1916 1917 1916 2179 992 995 992 2192 978 979 978 2420 353 ...
result:
ok n=100000
Test #13:
score: 0
Accepted
time: 238ms
memory: 42900kb
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 327211 1045 109 113 109 1929 812 819 812 2111 657 674 657 2497 781 801 781 2497 1419 1421 1419 2738 1918 1929 1918 2804 2000 2005 2000 3051 2747 2752 2747 3123 3134 362 3134 3186 1791 1795 1791 3570 3571 362 3571 4099 1828 1831 1828 4813 2663 2666 2663 4862 4199 4207 4199 4911 1334 1340 1334 497...
result:
ok n=100000
Test #14:
score: 0
Accepted
time: 183ms
memory: 33300kb
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 199997 38535 3433 38535 18670 3433 18670 38535 53850 18670 53850 38535 31420 53850 31420 38535 79252 31420 79252 38535 3155 79252 3155 38535 90709 3155 90709 38535 7043 90709 7043 38535 47690 7043 47690 38535 20905 47690 20905 38535 66663 20905 66663 38535 16655 66663 16655 38535 77812 16655 778...
result:
ok n=100000
Test #15:
score: 0
Accepted
time: 190ms
memory: 46536kb
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: 189ms
memory: 48836kb
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: 126ms
memory: 30708kb
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: 245ms
memory: 48240kb
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: 269ms
memory: 50116kb
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: 25ms
memory: 13576kb
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: 1ms
memory: 12180kb
input:
1 1 1
output:
Yes 0
result:
ok n=1
Extra Test:
score: 0
Extra Test Passed