QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84991#4513. Slide ParadeXZTmaxsmall6735 ✓4061ms106632kbC++231.5kb2023-03-06 21:34:242023-03-06 21:34:42

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-06 21:34:42]
  • 评测
  • 测评结果:35
  • 用时:4061ms
  • 内存:106632kb
  • [2023-03-06 21:34:24]
  • 提交

answer

#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
const int N=1e6+100;
int n,m;
int vis[N],lc[N],rc[N];
vector<int>g[N];
int dfs(int x)
{
	for(auto y:g[x])
	{
		if(vis[y])continue;vis[y]=1;
		if(rc[y]==-1||dfs(rc[y]))
		{
			lc[x]=y;rc[y]=x;
			return 1;
		}
	}
	return 0;
}
vector<pii>e;
stack<int>st;
vector<int>f[N];
void dfs1(int x)
{
	while(!f[x].empty())
	{
		int y=f[x].back();f[x].pop_back();
		dfs1(y);
	}
	st.push(x);
}
void solve()
{
	scanf("%d%d",&n,&m);e.clear();
	while(!st.empty())st.pop();
	for(int i=1;i<=n;i++)g[i].clear(),f[i].clear(),lc[i]=rc[i]=-1;
	for(int i=1,x,y;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		g[x].pb(y);e.pb(mp(x,y));
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)vis[j]=0;
		if(!dfs(i))return puts("IMPOSSIBLE"),void();
	}
	for(auto t:e)
	{
		int x,y;tie(x,y)=t;
		if(lc[x]!=y)
		{
			rc[lc[x]]=-1;
			for(int i=1;i<=n;i++)vis[i]=0;vis[y]=1;
			if(!dfs(rc[y]))return puts("IMPOSSIBLE"),void();
			else lc[x]=y,rc[y]=x;
		}
		for(int i=1;i<=n;i++)f[i].push_back(lc[i]);
	}
	dfs1(1);
	if(st.size()!=n*m+1)return puts("IMPOSSIBLE"),void();
	printf("%d\n",(int)st.size());
	while(!st.empty())printf("%d ",st.top()),st.pop();
}
int main()
{
	int _;scanf("%d",&_);
	for(int i=1;i<=_;i++)
		printf("Case #%d: ",i),solve();
	return 0;
}
/*
1
4 6
1 2
1 4
2 3
3 2
3 4
4 1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 11
Accepted
time: 8ms
memory: 50472kb

input:

100
5 8
1 2
1 3
1 4
1 5
2 1
3 1
4 1
5 1
5 10
1 3
1 4
2 3
2 5
3 1
3 4
3 5
4 2
5 1
5 3
5 10
1 4
2 3
2 5
3 1
3 5
4 2
4 3
4 5
5 1
5 2
3 6
1 2
1 3
2 1
2 3
3 1
3 2
5 10
1 2
1 5
2 3
2 4
3 1
4 3
4 5
5 2
5 3
5 4
4 10
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 4
4 2
4 3
5 10
1 2
1 3
2 1
2 4
3 1
3 5
4 2
4 5
5 3
5 4
5 10
1 ...

output:

Case #1: IMPOSSIBLE
Case #2: 51
1 4 2 5 3 1 4 2 3 5 1 4 2 3 5 1 4 2 3 5 1 3 4 2 5 1 4 2 5 3 1 4 2 5 3 1 4 2 3 5 1 4 2 5 3 1 3 4 2 5 1 Case #3: 51
1 4 3 1 4 2 5 2 3 5 1 4 5 2 3 1 4 3 1 4 2 5 2 3 5 1 4 2 3 5 1 4 3 1 4 3 1 4 2 5 2 5 2 3 5 1 4 2 3 5 1 Case #4: 19
1 3 2 1 2 3 1 2 3 1 3 2 1 3 2 1 2 3 1 Ca...

result:

ok  (100 test cases)

Test #2:

score: 24
Accepted
time: 4061ms
memory: 106632kb

input:

100
199 4980
1 95
1 96
1 105
1 124
1 130
1 131
1 135
1 137
1 139
1 140
1 141
1 147
1 155
1 172
1 174
1 179
1 183
1 188
1 193
1 196
1 197
2 3
2 5
2 13
2 14
2 17
2 20
2 24
2 26
2 30
2 41
2 44
2 45
2 52
2 56
2 67
2 70
2 71
2 74
2 78
2 84
2 85
2 90
2 92
2 93
2 97
2 107
2 111
2 113
2 122
2 124
2 128
2 13...

output:

Case #1: IMPOSSIBLE
Case #2: IMPOSSIBLE
Case #3: 1000001
1 67 142 75 191 179 161 23 100 22 40 99 82 68 129 90 113 27 160 88 172 52 57 110 86 194 62 49 143 192 181 156 174 35 123 58 152 133 137 108 6 186 105 36 157 38 8 2 148 4 39 43 195 29 154 73 41 115 72 176 190 30 45 101 171 112 121 117 141 187 8...

result:

ok  (100 test cases)