QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402224 | #4805. Grammy Sorting | Zxc200611 | WA | 24ms | 5808kb | C++14 | 3.3kb | 2024-04-30 08:37:37 | 2024-04-30 08:37:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct Edge
{
int end,rev;
bool del;
};
int n,m,S,T;
vector<Edge> t[1100];
bool usd[1100];
vector<int> g[1100];
int val[1100];
bool vis[1100];
vector<vector<int>> opt;
void addEdge(int u,int v)
{
// cout<<"AddEdge "<<u<<" "<<v<<endl;
t[u].push_back((Edge){v,t[v].size(),0});
t[v].push_back((Edge){u,t[u].size()-1,0});
}
namespace bipolarOrientation
{
int dfn[1100],clk;
vector<int> nxt[1100];
bool vis[1100];
bool shrink(int u,int f,vector<int> &chn)
{
// cout<<"Shrink u="<<u<<" f="<<f<<endl;
dfn[u]=++clk;
bool rchS=(u==S);
int aid=-1,fid=-1;
for(int i=0;i<t[u].size();i++)
{
int v=t[u][i].end;
// cout<<"u="<<u<<" v="<<v<<" del="<<t[u][i].del<<endl;
if(v==f)
fid=i;
if(v==f||t[u][i].del)
continue;
if(!dfn[v])
rchS|=shrink(v,u,chn);
else if(aid==-1||dfn[v]<dfn[t[u][aid].end])
aid=i;
}
if(!rchS)
{
// cout<<"Shrinking "<<u<<" fid="<<fid<<" aid="<<aid<<endl;
// if(aid!=-1)
// cout<<"aid="<<aid<<" end="<<t[u][aid].end<<" dfn="<<dfn[t[u][aid].end]<<endl;
if(!(fid!=-1&&aid!=-1&&dfn[t[u][aid].end]<dfn[u]))
{
// cout<<"Err at "<<u<<endl;
cout<<-1<<endl;
exit(0);
}
nxt[t[u][fid].end].push_back(u);
nxt[t[u][aid].end].push_back(u);
addEdge(t[u][fid].end,t[u][aid].end);
for(int i=0;i<t[u].size();i++)
t[u][i].del=t[t[u][i].end][t[u][i].rev].del=1;
}
else
chn.push_back(u);
return rchS;
}
void expand(int u,vector<int> &res)
{
vis[u]=1,res.push_back(u);
for(int v:nxt[u])
{
if(!vis[v])
expand(v,res);
}
}
vector<int> solve()
{
vector<int> chn(0),res(0);
shrink(T,0,chn);
for(int u:chn)
expand(u,res);
return res;
}
}
bool findPathOut(int u,int T,vector<int> &res)
{
if(u==T)
return 1;
vis[u]=1;
for(int i=0;i<t[u].size();i++)
{
int v=t[u][i].end;
if(!vis[v]&&!usd[v])
{
if(findPathOut(v,T,res))
{
res.push_back(u);
return 1;
}
}
}
return 0;
}
void findPathIn(int u,vector<int> &res)
{
res.push_back(u);
int mnf=0;
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(usd[v]&&(mnf==0||val[v]<val[mnf]))
mnf=v;
}
if(mnf==0||val[mnf]>val[u])
return;
swap(val[u],val[mnf]);
findPathIn(mnf,res);
}
void appendPoint(int u)
{
memset(vis,0,sizeof(vis));
vector<int> pth(0);
findPathOut(S,u,pth);
reverse(pth.begin(),pth.end());
for(int i=0;i<pth.size();i++)
{
swap(val[pth[i]],val[i==pth.size()-1?u:pth[i+1]]);
}
for(int i=0;i<t[u].size();i++)
{
int v=t[u][i].end;
if(usd[v])
g[u].push_back(v);
}
usd[u]=1;
findPathIn(u,pth);
opt.push_back(pth);
}
int main()
{
cin>>n>>m>>S>>T;
for(int i=1;i<=n;i++)
cin>>val[i];
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
addEdge(a,b);
}
vector<int> bpo=bipolarOrientation::solve();
// cout<<"> ";
// for(int i=0;i<bpo.size();i++)
// cout<<bpo[i]<<" ";
// cout<<endl;
usd[bpo.back()]=1;
for(int i=bpo.size()-2;i>=0;i--)
appendPoint(bpo[i]);
cout<<opt.size()<<endl;
for(int i=0;i<opt.size();i++)
{
cout<<opt[i].size()<<" ";
for(int j=0;j<opt[i].size();j++)
cout<<opt[i][j]<<" ";
cout<<endl;
}
// for(int i=1;i<=n;i++)
// cout<<val[i]<<" ";
// cout<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3668kb
input:
5 6 1 2 1 2 3 4 5 1 3 2 3 1 4 2 4 1 5 3 5
output:
4 2 1 3 4 1 5 3 2 3 1 4 2 3 1 5 3
result:
ok ok
Test #2:
score: 0
Accepted
time: 1ms
memory: 3860kb
input:
4 3 1 2 1 4 2 3 1 4 2 4 3 4
output:
-1
result:
ok ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
1000 2000 690 321 822 159 322 907 204 349 422 568 486 775 516 80 996 532 866 134 967 772 421 10 46 878 570 958 62 859 853 32 657 868 145 767 600 748 112 938 343 404 677 116 43 690 92 300 380 942 155 5 870 522 628 464 956 25 30 107 241 708 526 846 272 658 670 160 979 707 829 580 452 338 980 88 166 13...
output:
-1
result:
ok ok
Test #4:
score: 0
Accepted
time: 1ms
memory: 3788kb
input:
1000 2000 818 462 259 437 217 750 664 585 779 875 6 99 642 145 157 303 526 84 309 959 704 970 973 542 811 832 199 516 397 325 907 485 696 559 999 335 301 974 177 426 862 520 297 446 420 990 534 32 137 376 804 35 14 754 87 227 67 826 183 930 831 668 874 720 4 641 304 54 313 393 122 427 658 363 323 19...
output:
-1
result:
ok ok
Test #5:
score: 0
Accepted
time: 1ms
memory: 3756kb
input:
1000 2000 242 10 265 105 286 761 436 727 365 271 87 34 104 263 400 74 103 623 976 390 626 482 797 204 712 412 641 399 942 765 805 585 131 754 165 168 51 546 319 427 784 12 920 380 892 246 422 184 616 611 516 396 530 682 653 907 924 474 167 954 56 391 401 409 55 26 497 969 417 934 994 780 768 309 27 ...
output:
-1
result:
ok ok
Test #6:
score: 0
Accepted
time: 1ms
memory: 3988kb
input:
1000 2000 666 150 671 295 803 55 457 427 489 783 578 431 481 689 30 716 799 624 981 173 298 825 339 563 705 71 958 679 292 317 385 808 929 584 662 19 101 901 118 404 931 11 634 113 763 43 785 493 290 358 976 961 695 147 675 368 107 211 638 989 232 23 996 458 850 652 812 68 413 56 960 490 330 715 496...
output:
-1
result:
ok ok
Test #7:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
1000 2000 794 995 651 17 422 858 76 632 96 467 841 151 645 722 557 653 146 413 627 497 683 824 267 67 923 748 164 915 385 78 249 228 744 227 253 137 359 1 108 679 380 648 35 959 126 804 802 474 867 356 507 291 128 776 283 974 138 903 45 173 813 773 900 243 437 178 839 260 342 425 221 861 524 732 505...
output:
-1
result:
ok ok
Test #8:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
500 2000 361 270 1 416 382 489 19 269 340 435 497 5 276 316 358 332 137 63 257 395 73 61 496 181 119 100 149 304 409 102 191 264 279 286 386 200 82 455 250 366 154 135 273 164 108 398 344 400 109 498 99 369 172 411 193 471 327 171 64 218 123 107 143 295 45 350 294 371 59 432 114 252 442 69 244 392 2...
output:
-1
result:
ok ok
Test #9:
score: 0
Accepted
time: 0ms
memory: 3784kb
input:
500 2000 81 410 199 182 230 436 475 300 32 479 181 329 228 338 351 167 395 36 232 411 356 412 314 317 467 464 257 405 162 438 426 483 43 435 461 319 361 343 15 381 401 91 156 493 447 37 172 470 268 117 299 274 18 473 275 367 98 277 133 127 205 35 45 220 204 140 290 72 402 27 382 302 129 389 318 416 ...
output:
-1
result:
ok ok
Test #10:
score: 0
Accepted
time: 1ms
memory: 4068kb
input:
500 2000 209 254 272 8 472 466 136 277 204 38 342 3 421 338 23 408 273 320 151 391 290 94 44 215 179 84 16 218 251 281 285 85 433 133 257 217 55 138 143 367 390 488 209 237 465 326 161 198 36 175 485 28 178 458 283 308 240 241 443 306 207 484 154 279 304 435 431 224 226 449 210 25 53 375 141 146 177...
output:
-1
result:
ok ok
Test #11:
score: 0
Accepted
time: 1ms
memory: 3768kb
input:
500 2000 133 99 414 240 21 246 40 11 459 205 20 260 114 282 53 43 468 108 301 212 209 476 164 171 206 261 490 296 251 170 31 39 462 272 386 172 29 41 359 452 69 338 289 214 268 265 208 375 103 192 361 288 112 355 86 342 488 146 101 79 255 303 333 12 317 22 50 431 181 429 162 169 185 187 161 494 486 ...
output:
-1
result:
ok ok
Test #12:
score: 0
Accepted
time: 1ms
memory: 3768kb
input:
500 2000 261 443 70 479 170 186 15 223 8 77 337 123 349 295 437 230 265 365 54 444 375 308 169 281 333 297 176 376 26 366 172 274 20 49 224 191 4 370 299 406 338 328 119 470 81 56 126 11 92 347 471 257 83 48 445 438 494 271 52 1 331 196 408 469 473 85 413 235 289 110 477 309 155 287 50 319 354 250 4...
output:
-1
result:
ok ok
Test #13:
score: 0
Accepted
time: 3ms
memory: 3884kb
input:
200 2000 164 195 139 155 66 190 47 132 198 171 123 38 196 43 181 163 126 22 30 6 46 149 117 8 81 100 141 98 37 115 59 200 72 106 74 44 75 160 183 186 189 174 127 26 152 182 4 113 80 158 99 56 118 133 33 10 104 173 78 65 70 51 91 83 130 52 191 73 187 60 63 49 36 17 151 150 140 164 9 58 84 3 14 138 17...
output:
199 113 164 37 21 144 25 183 40 20 77 141 98 22 193 102 31 73 4 113 29 161 70 80 26 35 18 199 189 53 177 97 157 24 181 15 16 112 58 182 75 154 81 79 119 42 121 127 118 200 122 107 67 82 27 13 105 19 99 52 108 36 156 11 128 106 149 101 124 171 89 153 184 172 174 115 54 152 130 7 96 94 140 185 198 137...
result:
ok ok
Test #14:
score: 0
Accepted
time: 3ms
memory: 4056kb
input:
200 2000 92 40 147 175 72 43 97 104 12 196 46 92 148 44 125 174 134 200 199 154 143 10 190 71 114 39 184 1 155 101 90 3 135 122 186 140 116 58 54 117 22 82 73 81 57 91 138 21 93 161 158 162 110 65 96 41 2 28 26 11 24 160 136 129 176 7 31 150 48 170 124 56 53 146 142 68 80 79 49 194 27 187 133 40 182...
output:
199 71 92 89 183 145 139 161 6 134 75 184 9 49 130 188 180 172 115 193 121 126 29 194 1 50 186 48 66 57 140 120 182 95 125 156 13 22 122 105 135 52 158 163 21 38 154 35 69 144 138 102 178 109 27 189 196 54 10 74 42 141 129 25 99 175 28 149 179 170 94 173 40 124 92 89 183 145 139 161 6 134 75 184 9 ...
result:
ok ok
Test #15:
score: 0
Accepted
time: 3ms
memory: 3772kb
input:
200 2000 12 180 192 200 42 4 101 150 28 127 15 76 173 130 98 18 97 62 5 113 128 75 89 168 129 193 34 45 186 110 31 82 12 57 73 55 2 114 107 63 124 103 56 94 38 117 46 176 118 197 158 122 160 36 125 146 102 27 61 157 177 141 121 188 169 16 175 116 123 199 44 80 35 131 79 92 100 161 198 167 149 3 83 8...
output:
199 29 12 36 91 113 170 72 132 120 63 55 186 185 172 59 82 51 84 31 11 175 22 179 68 193 102 137 23 127 181 89 12 36 91 113 170 72 132 120 63 55 186 185 172 59 82 51 84 31 11 175 22 179 68 193 102 137 23 127 42 131 115 155 194 75 38 196 62 80 157 98 88 118 128 95 49 78 195 53 200 37 18 94 142 57 10...
result:
ok ok
Test #16:
score: 0
Accepted
time: 0ms
memory: 4084kb
input:
200 2000 140 25 75 98 10 69 110 15 70 73 9 178 163 197 198 169 177 84 80 126 49 51 191 25 166 96 38 47 1 176 87 183 4 44 60 185 53 146 31 111 149 154 65 17 160 12 92 91 165 76 101 158 130 152 147 32 62 164 45 67 30 113 117 170 86 27 114 3 148 52 79 189 2 112 56 57 103 155 142 28 6 141 121 140 134 99...
output:
199 18 140 192 2 145 156 13 85 131 124 160 73 8 184 32 76 31 179 25 17 140 192 2 145 156 13 85 131 124 160 73 8 184 32 76 31 179 17 140 192 2 145 156 13 85 131 124 160 73 8 184 32 76 31 179 14 140 192 2 145 156 13 85 131 124 160 76 31 179 25 13 140 192 2 145 156 13 85 131 124 160 76 31 179 14 1...
result:
ok ok
Test #17:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
200 2000 164 181 166 146 67 152 97 41 13 39 82 176 161 61 88 24 193 138 131 55 81 83 50 184 180 58 96 101 141 103 99 156 162 139 31 160 68 29 170 21 59 192 140 178 112 98 181 3 28 148 70 86 94 167 19 34 18 92 115 195 188 54 38 72 53 17 79 145 35 153 121 102 191 49 182 22 23 77 108 117 114 26 129 64 ...
output:
199 3 164 82 181 18 164 38 180 155 166 169 126 161 128 189 153 185 184 70 52 182 43 167 9 164 38 180 155 166 169 126 161 128 22 164 38 180 155 166 169 126 161 100 7 119 19 4 142 183 10 160 1 74 114 189 128 61 164 38 180 155 166 169 126 161 100 7 119 19 4 142 183 10 160 1 74 114 175 135 34 40 152...
result:
ok ok
Test #18:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
100 2000 4 7 69 82 43 4 5 22 36 87 52 40 62 66 54 86 3 30 55 97 18 19 57 48 67 94 12 61 16 41 24 53 93 39 1 38 11 20 91 92 37 23 96 68 95 65 84 73 81 63 58 83 27 77 72 49 6 100 85 28 2 98 76 45 26 60 88 42 79 71 8 9 64 90 21 33 50 32 34 17 7 46 29 25 99 47 80 13 31 75 59 14 51 78 44 74 35 56 10 15 8...
output:
99 46 4 2 77 61 5 74 48 23 34 9 22 28 29 68 21 50 24 30 72 55 96 94 53 10 18 81 6 97 98 100 99 26 43 78 51 14 93 76 33 54 8 49 70 39 45 67 12 4 2 77 61 5 74 48 23 34 9 67 7 24 4 2 77 61 5 74 48 23 34 94 53 10 18 81 28 29 68 21 50 24 30 72 22 9 18 4 2 77 61 5 74 48 23 34 94 53 10 18 81 28 22 9 67 ...
result:
ok ok
Test #19:
score: 0
Accepted
time: 2ms
memory: 3984kb
input:
100 2000 32 47 86 18 25 98 42 63 59 56 95 92 84 9 60 77 70 68 36 48 5 83 38 37 76 72 74 71 99 58 97 39 45 31 2 100 61 53 17 73 65 26 64 96 55 27 90 75 6 54 7 87 33 3 14 41 47 40 91 10 16 1 50 94 79 34 8 24 81 66 62 21 57 89 43 93 35 52 51 12 23 11 22 78 20 46 19 28 88 69 82 15 49 29 85 30 32 13 44 8...
output:
99 5 32 17 99 85 47 38 32 17 99 24 98 82 75 46 77 97 5 29 84 53 6 95 74 100 86 13 36 72 10 91 87 2 56 27 67 33 34 38 51 71 63 70 85 47 35 32 17 99 24 98 82 75 46 77 97 5 29 84 53 6 95 74 100 86 13 36 72 10 91 87 2 56 27 67 33 34 38 51 71 63 48 32 17 99 24 98 82 75 46 77 97 5 29 84 53 6 95 74 100 ...
result:
ok ok
Test #20:
score: 0
Accepted
time: 2ms
memory: 4008kb
input:
100 2000 56 91 55 34 19 17 87 33 85 97 9 22 27 96 73 48 71 10 3 28 90 7 88 93 59 38 1 62 75 37 39 63 29 94 65 13 21 12 30 32 49 64 31 61 53 70 67 2 98 20 42 77 60 18 45 6 46 23 8 44 100 79 89 50 82 25 24 16 68 72 83 95 57 54 99 86 74 76 91 5 47 80 40 43 69 51 58 14 81 66 84 26 78 52 4 15 35 36 41 56...
output:
99 14 56 77 28 97 81 71 78 83 80 76 13 23 20 14 8 56 77 28 97 81 71 14 91 15 56 77 28 97 81 44 6 46 89 66 69 33 75 78 71 27 56 77 28 97 81 44 6 46 89 66 69 33 75 5 43 17 1 39 79 61 25 74 11 80 83 78 71 26 56 77 28 97 81 44 6 46 89 66 69 33 75 5 43 17 1 39 79 61 25 74 11 80 83 78 48 56 77 28 97 ...
result:
ok ok
Test #21:
score: 0
Accepted
time: 2ms
memory: 4020kb
input:
100 2000 80 44 21 79 94 70 73 26 32 5 31 23 77 34 96 17 56 4 50 99 47 57 43 60 66 85 54 84 80 53 74 75 14 29 78 13 7 95 58 24 83 72 68 88 9 92 37 41 19 87 40 89 33 61 82 20 39 35 16 46 22 91 25 1 93 59 55 36 15 48 86 2 51 67 62 44 11 63 38 42 10 65 6 90 64 100 81 12 69 28 98 76 27 71 52 49 30 3 45 8...
output:
99 24 80 19 43 24 11 28 50 41 62 57 51 45 64 90 54 60 69 100 97 33 56 7 46 78 35 80 19 43 24 11 28 50 41 62 57 51 45 64 90 54 60 69 100 97 33 56 7 46 40 73 26 30 74 52 93 86 71 68 27 35 5 80 19 43 24 11 52 80 19 43 24 66 17 81 8 2 15 68 27 47 14 46 40 73 26 30 74 52 93 86 90 54 60 69 100 97 33 56...
result:
ok ok
Test #22:
score: 0
Accepted
time: 2ms
memory: 3848kb
input:
100 2000 4 84 60 95 87 8 29 23 44 83 40 31 75 72 93 76 92 89 18 47 65 1 51 54 27 28 57 46 66 2 14 74 30 9 71 98 13 38 64 56 78 88 85 52 82 62 20 41 16 77 33 25 67 3 73 80 59 90 42 26 21 24 61 100 22 48 19 10 94 58 49 36 70 63 32 7 35 84 79 15 96 34 12 81 50 39 55 6 4 37 68 86 45 5 43 69 97 91 11 53 ...
output:
99 94 4 58 75 48 90 16 31 26 74 95 5 18 14 73 70 67 53 82 51 43 89 80 99 86 15 8 81 97 22 42 68 85 83 50 79 7 25 1 60 92 33 12 11 17 57 36 21 19 52 91 63 29 47 87 23 38 55 72 3 37 39 28 6 69 41 56 54 45 88 78 66 65 2 35 100 98 93 94 10 76 62 32 40 49 64 71 96 13 30 59 9 77 44 34 7 4 58 75 48 90 16 ...
result:
ok ok
Test #23:
score: -100
Wrong Answer
time: 24ms
memory: 5808kb
input:
1000 1090 275 327 456 596 614 985 283 487 268 939 197 47 966 915 5 541 806 874 847 26 172 200 413 35 883 778 505 569 179 425 420 390 728 738 174 140 386 395 32 353 163 95 723 750 667 675 297 299 433 102 649 773 431 253 417 584 332 160 169 504 955 845 275 186 262 545 444 484 539 426 66 716 898 498 50...
output:
999 546 275 859 740 976 690 548 696 587 415 628 584 200 938 544 685 736 308 14 131 995 597 662 712 954 268 667 697 23 142 670 747 377 723 366 9 962 375 73 42 202 384 429 525 82 191 958 182 536 590 100 355 238 992 396 280 417 634 263 527 789 535 519 931 358 399 589 767 332 777 737 316 190 306 839 92 ...
result:
wrong answer not a path at operation #1