QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#84258#4513. Slide ParadeAppleblue1735 ✓3283ms74840kbC++142.5kb2023-03-06 07:44:332023-03-06 07:44:37

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 07:44:37]
  • 评测
  • 测评结果:35
  • 用时:3283ms
  • 内存:74840kb
  • [2023-03-06 07:44:33]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=440,M=5500,INF=1e9;


struct abc{
	int to,nxt,f;
}e[M*4];
int head[N],cnt=1;
void add(int u,int v,int f){
	e[++cnt]={v,head[u],f};
	head[u]=cnt;
}
void qadd(int u,int v,int f){
	add(u,v,f);
	add(v,u,0);
}

int S,T,sav[M];

int dis[N];
queue <int> q;

bool bfs(){
	for(int i=1;i<=T;i++) dis[i]=INF;
	dis[S]=0;
	q.push(S);
	while(q.size()){
		int u=q.front(); q.pop();
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(e[i].f && dis[v]==INF){
				dis[v]=dis[u]+1;
				q.push(v);
			}
		}
	}
	return (dis[T]<INF);
}

int cur[N];

int dfs(int u,int flow){
	if(u==T) return flow;
	int delta=flow;
	for(int i=cur[u];i;i=e[i].nxt){
		cur[u]=i;
		int v=e[i].to;
		if(e[i].f && dis[v]==dis[u]+1){
			int x=dfs(v,min(delta,e[i].f));
			e[i].f-=x,e[i^1].f+=x;
			delta-=x;
			if(!delta) break;
		}
	}
	return flow-delta;
}
void clr(){
	for(int i=1;i<=T;i++) head[i]=0;
	cnt=1;
}
int dinic(){
	int tot=0;
	while(bfs()){
		for(int i=1;i<=T;i++) cur[i]=head[i];
		tot+=dfs(S,INF);
	}
	return tot;
}


int T_,n,m;
int U[M],V[M];
int in[N],out[N];

vector <int> G[N];
stack <int> pat;
void dfs(int u){
	for(int i=cur[u];i<(int)G[u].size();i=cur[u]){
		cur[u]++;
		dfs(G[u][i]);
	}
	pat.push(u);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin>>T_;
	for(int T__=1;T__<=T_;T__++){
		cout<<"Case #"<<T__<<": ";
		cin>>n>>m;
		for(int i=1;i<=n;i++) in[i]=out[i]=0,G[i].clear();
		for(int i=1;i<=m;i++){
			int u,v; cin>>u>>v;
			U[i]=u,V[i]=v;
			out[u]++,in[v]++;
		}
		
		int L=-INF;
		for(int i=1;i<=n;i++) L=max({L,in[i],out[i]});
		
		bool fl=0;
		for(int d=m;d<=m;d++){
			clr();
			S=2*n+1,T=2*n+2;
			for(int i=1;i<=n;i++) qadd(S,i,d-out[i]);
			for(int i=1;i<=n;i++) qadd(i+n,T,d-in[i]);
			for(int i=1;i<=m;i++){
				int u=U[i],v=V[i];
				qadd(u,v+n,INF);
				sav[i]=cnt;
			}
			
			int val=dinic();
			if(val==d*n-m){
//				cout<<'\n';
//				cout<<" "<<"d: "<<d<<'\n';
//				for(int i=1;i<=m;i++){
//					cout<<e[sav[i]].f+1<<" ";
//				}
				for(int i=1;i<=m;i++){
					int u=U[i],v=V[i],c=e[sav[i]].f+1;
					while(c--) G[u].push_back(v);//,cout<<"edg "<<u<<" "<<v<<'\n';
				}
				for(int i=1;i<=n;i++) cur[i]=0;
				dfs(1);
				if(pat.size()!=d*n+1){
					while(pat.size()) pat.pop();
					break;
				}
				cout<<d*n+1<<'\n';
				while(pat.size()) cout<<pat.top()<<" ",pat.pop();
				cout<<'\n';
				fl=1;
				break;
			}
			
		}
		if(!fl) cout<<"IMPOSSIBLE\n";
	}
	
}

详细

Test #1:

score: 11
Accepted
time: 1ms
memory: 3352kb

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

result:

ok  (100 test cases)

Test #2:

score: 24
Accepted
time: 3283ms
memory: 74840kb

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 18 13 9 5 3 7 4 17 7 5 3 7 6 1 27 15 2 5 3 7 11 1 28 9 21 17 11 1 38 3 7 17 11 1 59 3 7 17 11 1 61 14 4 17 11 1 67 11 1 71 3 7 17 11 1 84 2 25 1 86 7 17 11 1 90 16 10 7 17 11 1 107 3 7 17 11 1 109 18 25 1 121 15 3 7 18 29 1 129 5 3 7 18 29 1...

result:

ok  (100 test cases)