QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#426421#6329. Colorful GraphEXODUSRE 0ms3988kbC++173.5kb2024-05-31 10:36:022024-05-31 10:36:03

Judging History

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

  • [2024-05-31 10:36:03]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3988kb
  • [2024-05-31 10:36:02]
  • 提交

answer


#include <vector>
#include <algorithm>
#include <cassert>
namespace exodus{
	namespace internal{
		class nothing{
		public:
			nothing(){}
			friend nothing operator +(const nothing&,const nothing&){return nothing();}
		};
	}
	template<class info=internal::nothing>
	struct dsu{
	public:
		struct node{
		public:
			node(){anc=-1,siz=1,ifo=info();}
			int anc,siz;
			info ifo;
		};
		dsu():_n(0){}
		explicit dsu(int n){
			_n=n;node.resize(n);
			for(int i=0;i<n;i++)
				node[i].anc=i;
		}
		void build(int n){
			_n=n;
			node.clear();
			node.resize(n);
			for(int i=0;i<n;i++)
				node[i].anc=i;
		}
		int find(int u){
			assert(0<=u&&u<_n);
			return __find(u);
		}
		void merge(int u,int v){
			assert(0<=u&&u<_n);
			assert(0<=v&&v<_n);
			u=__find(u),v=__find(v);
			if(node[u].siz<node[v].siz)std::swap(u,v);
			node[v].anc=u;
			node[u].ifo=node[u].ifo+node[v].ifo;
		}
		bool check(int u,int v){
			assert(0<=u&&u<_n);
			assert(0<=v&&v<_n);
			u=__find(u),v=__find(v);
			return u==v;
		}
	private:
		int _n;
		std::vector<node>node;
		int __find(int u){
			return u==node[u].anc?u:node[u].anc=__find(node[u].anc);
		}
	};
}

#include<bits/stdc++.h>
using namespace std;

bitset<7000>bts[7000],used;
vector<int>g[7000];
int main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	vector<pair<int,int>>edge(m);
	for(auto& [u,v]:edge)
		cin>>u>>v,--u,--v;
	for(auto [u,v]:edge){
		g[u].emplace_back(v);
	}
	auto bfs=[&](int s){
		static queue<int>q;
		q.emplace(s);
		bts[s][s]=true;
		while(!q.empty()){
			int u=q.front();
			q.pop();
			for(auto v:g[u]){
				if(!bts[s][v]){
					bts[s][v]=true;
					q.emplace(v);
				}
			}
		}
	};
	
	for(int i=0;i<n;i++)
		bfs(i);
	
	for(int i=0;i<n;i++){
		bts[i].reset(i);
		for(int j=i+1;j<n;j++){
			if(bts[i].test(j)&&bts[j].test(i)){
				bts[j].reset(i);
			}
		}
	}
	int cnt=n;
	vector<int>match(n);
	vector<int>nxt(n,-1);
	auto dfs=[&](auto self,int u)-> bool {
		auto caonima=bts[u]&used;
		for(int v=caonima._Find_first();v<n;v=caonima._Find_next(v)){
			used.set(v,false);
			if(nxt[v]==-1||self(self,nxt[v])){
				nxt[v]=u;
				return true;
			}
		}
		return false;
	};
	if(n>=5e3)
		assert(false);
	for(int i=0;i<n;i++){
		used.set();
		if(dfs(dfs,i))
			cnt--;
	}
#ifdef EXODUS
	cerr<<cnt<<'\n';
#endif
	exodus::dsu dsu(n);
	for(int u=0;u<n;u++){
		if(nxt[u]!=-1){
			dsu.merge(u,nxt[u]);
		}
	}
	vector<int>ans(n);
	for(int i=0;i<n;i++){
		static int colx=0;
		if(dsu.find(i)==i){
			ans[i]=++colx;
		}
	}
	for(int i=0;i<n;i++){
		ans[i]=ans[dsu.find(i)];
	}
	for(int i=0;i<n;i++)
		cout<<ans[i]<<' ';
	cout<<'\n';
#ifdef EXODUS
	cerr<<clock()<<'\n';
	vector<vector<int>>_g(n);
	vector<vector<int>>reach(n,vector<int>(n));
	for(auto [u,v]:edge){
		_g[u].emplace_back(v);
	}
	auto _bfs=[&](int s){
		static queue<int>q;
		q.emplace(s);
		reach[s][s]=true;
		while(!q.empty()){
			int u=q.front();
			q.pop();
			for(auto v:_g[u]){
				if(!reach[s][v]){
					reach[s][v]=true;
					q.emplace(v);
				}
			}
		}
	};
	for(int i=0;i<n;i++)
		_bfs(i);
	// for(int i=0;i<n;i++,cerr<<'\n')
	// 	for(int j=0;j<n;j++)
	// 		cerr<<reach[i][j]<<' ';
	vector<int>_lis;
	for(int i=1;i<=cnt;i++){
		_lis.clear();
		for(int j=0;j<n;j++)
			if(ans[j]==i)
				_lis.emplace_back(j);
		for(auto u:_lis)
			for(auto v:_lis){
				if(!reach[u][v]&&!reach[v][u]){
					cerr<<"Wrong Answer\n";
					return 1;
				}
			}
	}
	cerr<<"Accept\n";
#endif
	return 0;
}

详细

Test #1:

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

input:

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

output:

2 1 1 2 2 

result:

ok AC

Test #2:

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

input:

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

output:

1 1 2 2 1 

result:

ok AC

Test #3:

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

input:

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

output:

1 1 1 1 2 1 3 4 

result:

ok AC

Test #4:

score: -100
Runtime Error

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:


result: