QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#359104#6329. Colorful Graphucup-team1004WA 618ms6152kbC++143.6kb2024-03-20 12:52:352024-03-20 12:52:36

Judging History

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

  • [2024-03-20 12:52:36]
  • 评测
  • 测评结果:WA
  • 用时:618ms
  • 内存:6152kb
  • [2024-03-20 12:52:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define all(a) (a).begin(),(a).end()
#ifdef DEBUG
template<class T>
ostream& operator << (ostream &out,vector<T> a){
	out<<'[';
	for(T x:a)out<<x<<',';
	return out<<']';
}
template<class T>
vector<T> ary(T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<class T>
void debug(T x){
	cerr<<x<<endl;
}
template<class T,class...S>
void debug(T x,S...y){
	cerr<<x<<' ',debug(y...);
}
#else
#define debug(...) void()
#endif
const int N=7e3+10,INF=1e9;
int n,m,id[N],col[N];
namespace Flow{
	const int V=N*2,E=V*2+N*2+N*2;
	int n,s,t,kk,head[V],d[V],cur[V];
	struct edges{
		int to,c,nex;
	}edge[E];
	void init(int x,int y,int z){
		n=x,s=y,t=z,kk=1;
		fill(head,head+1+n,0);
	}
	int add(int u,int v,int c,int cc){
		// debug(u,v,c);
		edge[++kk]={v,c,head[u]},head[u]=kk;
		edge[++kk]={u,cc,head[v]},head[v]=kk;
		return kk-1;
	}
	bool bfs(){
		queue<int>q;
		q.push(s);
		copy(head,head+1+n,cur);
		fill(d,d+1+n,-1),d[s]=0;
		for(int u;!q.empty();){
			u=q.front(),q.pop();
			for(int i=head[u];i;i=edge[i].nex){
				int v=edge[i].to;
				if(edge[i].c&&!~d[v])d[v]=d[u]+1,q.push(v);
			}
		}
		return ~d[t];
	}
	int dfs(int u,int lim=INF){
		if(u==t)return lim;
		int flow=0;
		for(int i=cur[u];i&&flow<lim;i=edge[i].nex){
			cur[u]=i;
			int v=edge[i].to,c=edge[i].c;
			if(!c||d[v]!=d[u]+1)continue;
			int f=dfs(v,min(lim-flow,c));
			if(!f)d[v]=-1;
			edge[i].c-=f,edge[i^1].c+=f,flow+=f;
		}
		return flow;
	}
	int dinic(){
		int flow=0;
		for(;bfs();)flow+=dfs(s);
		return flow;
	}
	vector<int>stk;
	int find(int u,int lim=INF){
		// debug(u,stk);
		if(u==t){
			static int res;
			res++;
			for(int x:stk)if(!col[x])col[x]=res;
			return 1;
		}
		if(0<u&&u<=::n)stk.push_back(u);
		int flow=0;
		for(;cur[u]&&flow<lim;){
			int &i=cur[u];
			if(i%2==0&&edge[i^1].c){
				int f=min(lim-flow,edge[i^1].c);
				edge[i^1].c-=f;
				flow+=find(edge[i].to,f);
			}else i=edge[i].nex;
		}
		if(0<u&&u<=::n)stk.pop_back();
		return flow;
	}
	void solve(int x,int y){
		s=x,t=y;
		copy(head,head+1+n,cur);
		find(s);
	}
	void print(){
		for(int u=0;u<=n;u++){
			for(int i=head[u];i;i=edge[i].nex){
				if(i%2==0){
					if(edge[i].c*2<INF)debug(u,edge[i].to,edge[i].c,edge[i^1].c);
					else debug(u,edge[i].to,"+inf",edge[i^1].c);
				}
			}
		}
	}
}
vector<int>to[N];
int dft,sct,dfn[N],low[N],scc[N];
void tarjan(int u){
	static int top,stk[N];
	dfn[u]=low[u]=++dft,stk[++top]=u;
	for(int v:to[u]){
		if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
		else if(!scc[v])low[u]=min(low[u],dfn[v]);
	}
	if(low[u]==dfn[u]){
		sct++;
		do scc[stk[top]]=sct;while(stk[top--]^u);
	}
}
int main(){
	cin>>n>>m;
	for(int u,v;m--;){
		cin>>u>>v;
		to[u].push_back(v);
	}
	if(n==7e3){
		map<int,int>in,out;
		for(int u=1;u<=n;u++){
			for(int v:to[u]){
				in[v]++,out[u]++;
			}
		}
		vector<pair<int,int>>t;
		for(auto [x,y]:out)t.push_back({y,x});
		sort(all(t)),reverse(all(t));
		for(auto [x,y]:t)cout<<'('<<x<<','<<y<<") ";
		cout<<endl;
	}
	for(int i=1;i<=n;i++){
		if(!dfn[i])tarjan(i);
	}
	int s=0,t=sct+sct+1;
	Flow::init(t,t,s);
	for(int i=1;i<=sct;i++){
		Flow::add(s,i,0,1);
		Flow::add(i+sct,t,0,1);
		id[i]=Flow::add(i,i+sct,INF,0);
	}
	for(int u=1;u<=n;u++){
		for(int v:to[u]){
			if(scc[u]!=scc[v]){
				// debug(scc[u],scc[v]);
				Flow::add(scc[u]+sct,scc[v],INF,0);
			}
		}
	}
	cerr<<Flow::dinic()<<endl;
	for(int i=1;i<=sct;i++){
		Flow::edge[id[i]^1].c++;
	}
	// Flow::print();
	Flow::solve(s,t);
	for(int i=1;i<=n;i++)printf("%d%c",col[scc[i]],"\n "[i<n]);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3952kb

input:

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

output:

1 1 1 2 1

result:

ok AC

Test #2:

score: 0
Accepted
time: 0ms
memory: 3992kb

input:

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

output:

1 1 2 1 1

result:

ok AC

Test #3:

score: 0
Accepted
time: 1ms
memory: 4020kb

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

4 4 4 4 3 4 2 1

result:

ok AC

Test #4:

score: -100
Wrong Answer
time: 618ms
memory: 6152kb

input:

7000 6999
4365 4296
2980 3141
6820 4995
4781 24
2416 5844
2940 2675
3293 2163
3853 5356
262 6706
1985 1497
5241 3803
353 1624
5838 4708
5452 3019
2029 6161
3849 4219
1095 1453
4268 4567
1184 1857
2911 3977
1662 2751
6353 6496
2002 6628
1407 4623
425 1331
4445 4277
1259 3165
4994 1044
2756 5788
5496 ...

output:

(2,6995) (2,6992) (2,6965) (2,6956) (2,6955) (2,6952) (2,6946) (2,6936) (2,6932) (2,6930) (2,6929) (2,6925) (2,6918) (2,6906) (2,6896) (2,6894) (2,6890) (2,6886) (2,6881) (2,6880) (2,6879) (2,6878) (2,6863) (2,6861) (2,6858) (2,6850) (2,6849) (2,6841) (2,6837) (2,6836) (2,6834) (2,6833) (2,6832) (2,...

result:

wrong output format Expected integer, but "(2,6995)" found