QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#402214#4805. Grammy SortingZxc200611RE 0ms3668kbC++143.1kb2024-04-30 08:05:402024-04-30 08:05:41

Judging History

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

  • [2024-04-30 08:05:41]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3668kb
  • [2024-04-30 08:05:40]
  • 提交

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)
{
	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;
			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[aid])
				aid=i;
		}
		if(!rchS)
		{
			assert(fid!=-1&&aid!=-1&&dfn[aid]<dfn[u]);
			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;
}
/*
3 3 1 3
1 3
1 2
2 3

13 20 10 12
11 4
6 9
2 11
1 5
13 2
7 2
5 7
4 6
4 9
9 12
12 8
3 12
8 9
8 3
9 3
7 13
10 7
2 4
1 10
2 6

5 6 1 2
1 2 3 4 5
1 3
2 3
1 4
2 4
1 5
3 5
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
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: -100
Runtime Error

input:

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

output:


result: