QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#338773 | #4805. Grammy Sorting | chenxinyang2006 | WA | 1ms | 4072kb | C++14 | 5.4kb | 2024-02-26 11:40:44 | 2024-02-26 11:40:46 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;
template <class T>
void chkmax(T &x,T y){
if(x < y) x = y;
}
template <class T>
void chkmin(T &x,T y){
if(x > y) x = y;
}
inline int popcnt(int x){
return __builtin_popcount(x);
}
inline int ctz(int x){
return __builtin_ctz(x);
}
/*ll power(ll p,int k = mod - 2){
ll ans = 1;
while(k){
if(k % 2 == 1) ans = ans * p % mod;
p = p * p % mod;
k /= 2;
}
return ans;
}*/
int n,m,A,B;
int a[1005];
int _u[2005],_v[2005];
int cnt;
int head[2005];
struct eg{
int to,nxt;
}edge[4005];
void make(int u,int v){
edge[++cnt].to = v;
edge[cnt].nxt = head[u];
head[u] = cnt;
}
int dfn[1005],low[1005],N;
vector <int> sta,vcc[1005],eid[1005];
void tarjan(int u){
dfn[u] = low[u] = ++cnt;
sta.eb(u);
for(int i = head[u];i;i = edge[i].nxt){
int v = edge[i].to;
// printf("%d %d\n",u,v);
if(!dfn[v]){
tarjan(v);
chkmin(low[u],low[v]);
if(low[v] >= dfn[u]){
++N;
while(1){
int p = sta.back();
sta.pop_back();
vcc[N].eb(p);
if(p == v) break;
}
vcc[N].eb(u);
}
}else{
chkmin(low[u],dfn[v]);
}
}
}
int fa[2005],dep[2005];
void dfs(int u,int f){
fa[u] = f;
dep[u] = dep[f] + 1;
for(int i = head[u];i;i = edge[i].nxt){
int v = edge[i].to;
if(v == f) continue;
dfs(v,u);
}
}
vector <int> _S,temp;
void test(){
int p = A,q = B;
while(p != q){
if(dep[p] > dep[q]){
_S.eb(p);
p = fa[p];
}else{
temp.eb(q);
q = fa[q];
}
}
_S.eb(p);
while(!temp.empty()){
_S.eb(temp.back());
temp.pop_back();
}
}
vector <int> L[1005],ans;
int ord[1005],sum[1005],vis[1005];
void starjan(int u){
dfn[u] = low[u] = ++cnt;
ord[cnt] = u;
for(int i = head[u];i;i = edge[i].nxt){
int v = edge[i].to;
if(!dfn[v]){
fa[v] = u;
starjan(v);
sum[u] += sum[v];
chkmin(low[u],low[v]);
}else{
chkmin(low[u],dfn[v]);
}
}
// printf("%d %d %d %d\n",u,fa[u],ord[low[u]],sum[u]);
if(fa[u] && !sum[u]){
L[fa[u]].eb(u);
L[ord[low[u]]].eb(u);
}
}
void cover(int u){
if(vis[u]) return;
vis[u] = 1;
ans.eb(u);
for(int v:L[u]) cover(v);
}
void clr(int u){
head[u] = dfn[u] = low[u] = fa[u] = sum[u] = vis[u] = 0;
L[u].clear();
}
int rk[1005];
void solve(int c,int S,int T){
// printf("solve vcc %d S=%d T=%d\n",c,S,T);
cnt = 0;
for(int e:eid[c]){
clr(_u[e]);
clr(_v[e]);
// printf("%d %d\n",_u[e],_v[e]);
}
for(int e:eid[c]){
make(_u[e],_v[e]);
make(_v[e],_u[e]);
}
sum[T] = 1;
cnt = 0;
starjan(S);
temp.clear();
int p = T;
while(p){
temp.eb(p);
p = fa[p];
}
reverse(temp.begin(),temp.end());
for(int q:temp) cover(q);
}
int pst[1005];
vector <int> answer[1005];
int main(){
// freopen("test.in","r",stdin);
scanf("%d%d%d%d",&n,&m,&A,&B);
rep(i,1,n) scanf("%d",&a[i]);
rep(i,1,m){
scanf("%d%d",&_u[i],&_v[i]);
make(_u[i],_v[i]);
make(_v[i],_u[i]);
}
/* if(n == 2){
if(a[A] > a[B]){
printf("1\n");
printf("2 %d %d\n",A,B);
}else{
printf("0\n");
}
return 0;
}*/
cnt = 0;
tarjan(1);
cnt = 0;
memset(head,0,sizeof(head));
rep(c,1,N){
// printf("vcc %d\n",c);
for(int u:vcc[c]){
make(u,c + n);
make(c + n,u);
// printf("%d ",u);
}
// printf("\n");
}
// int rt = -1;
// rep(u,1,n) if(u != A && u != B) rt = u;
// assert(rt != -1);
// dfs(rt,0);
dfs(1,0);
test();
// for(int u:_S) printf("%d ",u);
// printf("\n");
if(SZ(_S) / 2 < N){
printf("-1\n");
return 0;
}
rep(i,1,m){
if(fa[_u[i]] == fa[_v[i]]) eid[fa[_u[i]] - n].eb(i);
else if(fa[fa[_u[i]]] == _v[i]) eid[fa[_u[i]] - n].eb(i);
else if(fa[fa[_v[i]]] == _u[i]) eid[fa[_v[i]] - n].eb(i);
}
// for(int k = 0;k + 2 < SZ(_S);k += 2) printf("%d ",_S[k]);
// printf("\n");
for(int k = 0;k + 2 < SZ(_S);k += 2){
if(k) ans.pop_back();
solve(_S[k + 1] - n,_S[k],_S[k + 2]);
}
for(int u:ans) printf("%d ",u);
printf("\n");
//ans 是一组合法双极定向
cnt = 0;
fill(head + 1,head + n + 1,0);
rep(k,0,n - 1) rk[ans[k]] = k + 1;
rep(i,1,m){
if(rk[_u[i]] > rk[_v[i]]) swap(_u[i],_v[i]);
make(_u[i],_v[i]);
pst[_v[i]] = _u[i];
}
// rep(u,1,n) printf("%d ",rk[u]);
// printf("\n");
per(k,n - 1,0){
int p = ans[k];
while(p != A){
answer[k].eb(p);
p = pst[p];
}
answer[k].eb(A);
reverse(answer[k].begin(),answer[k].end());
p = ans[k];
while(1){
int q = -1;
for(int i = head[p];i;i = edge[i].nxt){
int v = edge[i].to;
if(a[v] <= a[A] && (q == -1 || a[v] < a[q])) q = v;
}
if(q == -1) break;
answer[k].eb(q);
p = q;
}
rep(i,0,SZ(answer[k]) - 2) swap(a[answer[k][i]],a[answer[k][i + 1]]);
}
int ssum = 0;
rep(k,0,n - 1) ssum += !answer[k].empty();
printf("%d\n",ssum);
per(k,n - 1,0){
if(answer[k].empty()) continue;
printf("%d ",SZ(answer[k]));
for(int p:answer[k]) printf("%d ",p);
printf("\n");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 4072kb
input:
5 6 1 2 1 2 3 4 5 1 3 2 3 1 4 2 4 1 5 3 5
output:
1 4 5 3 2 5 3 1 4 2 4 1 5 3 2 4 1 5 3 2 2 1 4 2 1 5
result:
wrong answer a_1 != A at operation #1