QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#92686#4513. Slide Paradechenshi35 ✓3407ms50856kbC++1.8kb2023-03-30 20:52:472023-03-30 20:52:58

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-30 20:52:58]
  • 评测
  • 测评结果:35
  • 用时:3407ms
  • 内存:50856kb
  • [2023-03-30 20:52:47]
  • 提交

answer

#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int o=1e6+10;
int Tc,n,m,S,T,h[o],cur[o],cnt,d[o],ind[o],outd[o],flow,u[o],v[o],id[o],st[o],tp;queue<int> q;
struct Edge{int v,p,c;}e[o];
inline void ad_(int U,int V){e[++cnt].v=V;e[cnt].p=h[U];h[U]=cnt;}
inline void ad(int U,int V,int C){e[++cnt].v=V;e[cnt].p=h[U];e[h[U]=cnt].c=C;}
inline void add(int ST,int ED,int CA){ad(ST,ED,CA);ad(ED,ST,0);}
inline bool bfs(){
	for(int i=S;i<=T;++i) d[i]=0,cur[i]=h[i];
	for(q.push(S),d[S]=1;!q.empty();q.pop()) for(int i=h[q.front()];i;i=e[i].p)
		if(e[i].c&&!d[e[i].v]) d[e[i].v]=d[q.front()]+1,q.push(e[i].v);
	return d[T];
}
int dfs(int A,int B){
	if(A==T||!B) return B;
	int res=0,x;
	for(int&i=cur[A];i;i=e[i].p) if(e[i].c&&d[e[i].v]==d[A]+1)
		{x=dfs(e[i].v,min(B-res,e[i].c));e[i].c-=x;e[i^1].c+=x;if((res+=x)==B) return B;}
	return res;
}
void Dfs(int nw){int t;for(int&i=h[nw];i;) t=e[i].v,i=e[i].p,Dfs(t),st[++tp]=nw;}
inline void slv(){
	scanf("%d%d",&n,&m);S=0;T=n*2+1;cnt=1;flow=0;
	for(int i=S;i<=T;++i) h[i]=0;
	for(int i=1;i<=n;++i) ind[i]=outd[i]=0;
	for(int i=1;i<=m;++i) scanf("%d%d",&u[i],&v[i]),++ind[v[i]],++outd[u[i]],add(u[i],v[i]+n,o),id[i]=cnt;
	for(int i=1;i<=n;++i){
		if(ind[i]>m||outd[i]>m){printf("IMPOSSIBLE\n");return;}
		add(i+n,T,m-ind[i]);add(S,i,m-outd[i]);flow+=m-ind[i];
	}
	for(;bfs();flow-=dfs(S,o));
	if(flow){printf("IMPOSSIBLE\n");return;}
	cnt=0;
	for(int i=1;i<=n;++i) h[i]=0;
	for(int i=1;i<=m;++i) for(int j=e[id[i]].c+1;j--;) ad_(u[i],v[i]);
	st[tp=0]=1;Dfs(1);
	if(tp<n*m){printf("IMPOSSIBLE\n");return;}
	printf("%d\n",tp+1);
	for(int i=tp;i+1;--i) printf("%d ",st[i]);
	putchar('\n');
}
int main(){
	scanf("%d",&Tc);
	for(int i=1;i<=Tc;++i) printf("Case #%d: ",i),slv();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 11
Accepted
time: 2ms
memory: 22160kb

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 5 3 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 1 4 2 5 1 4 2 3 1 3 1 
Case #3: 51
1 4 5 2 5 2 3 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 5 1 4 3 1 4 2 3 1 
Case #4: 19
1 3 2 3 2 1 3 2 1 3 2 1 3 2 1 3 1 2 1 ...

result:

ok  (100 test cases)

Test #2:

score: 24
Accepted
time: 3407ms
memory: 50856kb

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 193 195 197 200 169 196 195 197 200 169 191 195 197 200 169 178 196 195 197 200 169 174 195 197 200 169 164 198 193 194 182 197 200 169 164 193 194 182 197 200 169 164 174 194 182 197 200 169 164 162 198 193 194 182 197 200 169 164 162 197 2...

result:

ok  (100 test cases)