QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#398896 | #4805. Grammy Sorting | Naganohara_Yoimiya | TL | 1ms | 4136kb | C++14 | 3.6kb | 2024-04-25 19:35:31 | 2024-04-25 19:35:31 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
return x*f;
}
const int mod=998244353;
int ksm(int x,ll y,int p=mod){
int ans=1;y%=(p-1);
for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}
template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}
const int N=1005;
int n,m,a[N],order[N],A,B,rk[N];
vector<pair<int,int> >edges;
namespace Bipolar{
vector<int>G[N];
int dfn[N],low[N],fa[N],son[N],ocnt,dfc=0,anc[N];
void adde(int u,int v){
G[u].emplace_back(v),G[v].emplace_back(u);
}
bool cut=0;
void dfs(int u){
anc[u]=u,dfn[u]=++dfc,low[u]=dfn[u];
// cout<<"dfs "<<u<<" dfn = "<<dfn[u]<<endl;
for(int v:G[u]){
if(!dfn[v]){
// cout<<" "<<u<<" -> son = "<<v<<endl;
fa[v]=u,dfs(v),cmin(low[u],low[v]),son[u]++;
// cout<<" low "<<v<<" = "<<low[v]<<" dfn = "<<dfn[u]<<endl;
if(low[v]>=dfn[u]&&u!=A)cut=true;
}
else{
if(dfn[v]<dfn[anc[u]])anc[u]=v,
cmin(low[u],dfn[v]);
}
}
}
vector<int>nxt[N];
bool vis[N];
void ins(int u){
order[++ocnt]=u,vis[u]=1;
for(int v:nxt[u])if(!vis[v])ins(v);
}
bool Solve(int s,int t){
adde(s,t);
dfs(s);
if(son[s]>=2||cut)return false;
G[s].erase(--G[s].end()),G[t].erase(--G[t].end());
memset(dfn,0,sizeof(dfn)),memset(low,0,sizeof(low)),dfc=0;
dfs(s);
queue<int>q;
for(int i=1;i<=n;i++)if(son[i]==0)q.push(i);
while(q.size()){
int x=q.front();q.pop();if(x==t)continue;
nxt[fa[x]].emplace_back(x),nxt[anc[x]].emplace_back(x);
if(--son[fa[x]]==0)q.push(fa[x]);
}
vector<int>nodes;
int x=t;nodes.emplace_back(t);
while(x!=s)x=fa[x],nodes.emplace_back(x);
reverse(nodes.begin(),nodes.end());
for(int x:nodes)ins(x);
return true;
}
}
vector<int>G[N];
int fa[N];
signed main(void){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
// freopen("out.out","w",stdout);
#endif
n=read(),m=read(),A=read(),B=read();edges.resize(m);
for(int i=1;i<=n;i++)a[i]=read();
for(int i=0;i<m;i++)edges[i].fi=read(),edges[i].se=read();
for(auto [u,v]:edges)Bipolar::adde(u,v);
if(!Bipolar::Solve(A,B))return puts("-1"),0;
// cout<<"order = ";for(int i=1;i<=n;i++)cout<<order[i]<<" ";puts("");
for(int i=1;i<=n;i++)rk[order[i]]=i;
for(auto [u,v]:edges){
if(rk[u]>rk[v])swap(u,v);
G[u].emplace_back(v),fa[v]=u;
}
vector<vector<int> >ans;
for(int i=n-1;i>=1;i--){
int x=order[i];bool chk=0;
for(int y:G[x])if(a[y]<a[x])chk=1;
if(!chk)continue;
// cout<<"x = "<<x<<endl;
vector<int>nodes(1,x);
while(x!=A)x=fa[x],nodes.emplace_back(x);
reverse(nodes.begin(),nodes.end());
// cout<<"now nodes = ";for(int x:nodes)cout<<x<<" ";puts("");
x=order[i];
while(x!=B){
int p=-1;
for(int y:G[x])if(p==-1||a[y]<a[p])p=y;
if(p==-1||a[p]>a[A])break;
nodes.emplace_back(p),x=p;
}
ans.emplace_back(nodes);
int cur=a[A];
for(int i=0;i+1<nodes.size();i++)a[nodes[i]]=a[nodes[i+1]];
a[nodes.back()]=cur;
}
cout<<ans.size()<<endl;
for(auto op:ans){
cout<<op.size()<<" ";
for(int x:op)cout<<x<<" ";puts("");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3708kb
input:
5 6 1 2 1 2 3 4 5 1 3 2 3 1 4 2 4 1 5 3 5
output:
3 3 1 5 3 4 1 5 3 2 3 1 5 3
result:
ok ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3848kb
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: 1ms
memory: 3944kb
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: 3884kb
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: 3952kb
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: 3892kb
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: 1ms
memory: 4136kb
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: 4084kb
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: 1ms
memory: 4020kb
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: 3932kb
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: 3896kb
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: 0ms
memory: 3852kb
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: -100
Time Limit Exceeded
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...