QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#327328#4805. Grammy SortingwullaaaWA 0ms3820kbC++141.6kb2024-02-14 21:35:372024-02-14 21:35:37

Judging History

你现在查看的是最新测评结果

  • [2024-02-14 21:35:37]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3820kb
  • [2024-02-14 21:35:37]
  • 提交

answer

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N=1e3+5;
int n,m,A,B,a[N];
int head[N],cnt=1;
struct node{ int nxt,v; }tree[N<<2];
void add(int u,int v){
	tree[++cnt]={head[u],v},head[u]=cnt;
	tree[++cnt]={head[v],u},head[v]=cnt;
}
int dfn[N],low[N],tot,fa[N],pos[N];
bool flag;
void tarjan(int x,int f){
	dfn[x]=low[x]=++tot,pos[tot]=x;
	if(x==A) tarjan(B,A);
	for(int i=head[x],y;i;i=tree[i].nxt) if((y=tree[i].v)!=f){
		if(!dfn[y]){
			if(x==A){ flag=true; break; }
			fa[y]=x,tarjan(y,x),low[x]=min(low[x],low[y]);
		}else low[x]=min(low[x],dfn[y]);
		if(low[x]==dfn[x]&&x!=A){ flag=true; break; }
	}
}
int topo[N],top,pre[N],nxt[N],path[N];
void solve(){
	nxt[A]=B,pre[B]=A;
	for(int i=3,x;i<=n;++i){
		x=pos[i],nxt[x]=fa[x],pre[x]=pre[fa[x]],nxt[pre[fa[x]]]=x,pre[fa[x]]=x;
		if(!path[fa[x]]) path[fa[x]]=x;
	}
	for(int i=1;i<=n;++i) if(i!=A&&i!=B&&!path[i]) path[i]=A;
	for(int i=1,x=A;i<=n;x=nxt[x],++i) topo[++top]=x;
}
int stk[N],tp;
void pt(int x){
	for(tp=0;x;x=path[x]) stk[++tp]=x;
	printf("%d ",tp);
	int t=a[1];
	for(int i=tp;i;--i) printf("%d%c",stk[i],(i==1)?'\n':' '),(i>1)&&(a[stk[i]]=a[stk[i-1]]);
	a[stk[1]]=t;
}
int main(){
	scanf("%d%d%d%d",&n,&m,&A,&B);
	for(int i=1;i<=n;++i) scanf("%d",&a[i]);
	for(int i=1,u,v;i<=m;++i) scanf("%d%d",&u,&v),add(u,v);
	tarjan(A,0); if(flag) return puts("-1"),0;
	solve();
	printf("%d\n",n-1);
	for(int i=n,x;i>1;--i){
		x=topo[i];
		if(!fa[x]||a[1]<a[fa[x]]) pt(x);
		else{
			int p=x;
			for(int t=x;t;t=fa[t]){
				if(fa[t]) path[fa[t]]=t;
				if(a[t]<a[1]) p=t;
			}
			pt(p);
		}
	}



	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3820kb

input:

5 6 1 2
1 2 3 4 5
1 3
2 3
1 4
2 4
1 5
3 5

output:

4
3 1 4 2
4 1 5 3 2
4 1 5 3 2
2 1 4

result:

wrong answer vertex 5 cannot be reached from A