QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#398892 | #4805. Grammy Sorting | Naganohara_Yoimiya | WA | 1ms | 3780kb | C++14 | 3.6kb | 2024-04-25 19:33:43 | 2024-04-25 19:33:47 |
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])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: 0
Wrong Answer
time: 1ms
memory: 3780kb
input:
5 6 1 2 1 2 3 4 5 1 3 2 3 1 4 2 4 1 5 3 5
output:
-1
result:
wrong answer Jury has answer but participant has not.