QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621086#9449. New School TermAPROHACK124RE 1ms4204kbC++143.9kb2024-10-08 08:35:182024-10-08 08:35:18

Judging History

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

  • [2024-10-08 08:35:18]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:4204kb
  • [2024-10-08 08:35:18]
  • 提交

answer

#include<bits/stdc++.h>

#define ff first
#define ss second 
#define pb push_back
using ll = long long;
using ld = long double;
using namespace std;
int n, m;
vector<pair<int, int > > friends;
struct node{
	int rep;
	int color;
	vector<int>group;
	int cnt_color[2];
	void append(vector<int>&g){
		for(auto &i : g){
			group.pb(i);
		}
	}
};
vector<pair<int, int> > exec;

int sq = 140;

node nodes[10004];

int findd(int u ){
	if(nodes[u].rep == u)return u;
	return nodes[u].rep = findd(nodes[u].rep);
}

int joinn(int a, int b){
	a = findd(a);
	b = findd(b);
	if(a == b)return a;
	if(nodes[a].group.size() < nodes[b].group.size()){
		swap(a, b);
	}
	// a <- b
	node &A = nodes[a];
	node &B = nodes[b];
	A.cnt_color[0] += B.cnt_color[0];
	A.cnt_color[1] += B.cnt_color[1];
	for(auto &i : B.group){
		A.group.pb(i);
	}
	B.group.clear();
	B.rep = A.rep;
	return B.rep;
}

void solve(set<int>&important){
	if(exec.empty())return ;
	vector<int>allOthers;
	for(int i = 1 ; i <= 2 * n ; i ++){
		int rep = nodes[i].rep;
		if(important.find(rep) != important.end())continue;
		allOthers.pb(rep);
	}
	bitset<5005>bitmask;
	auto getBitmask = [&] (){
		for(int i = 0 ; i <= n + 1 ; i ++){
			bitmask[i] = 0;
		}
		bitmask[0] = 1;
		for(int i = 0 ; i < allOthers.size() ; i ++){
			int node = nodes[allOthers[i]].rep;
			bitmask = ((bitmask << nodes[node].cnt_color[0]) |(bitmask << nodes[node].cnt_color[1]) );
		}
	};
	getBitmask();

	auto check_ok = [&] (set<int>ignoring, int howMany){
		bitset<5005>aux = bitmask;
		for(auto i : important){
			if(ignoring.find(i) != ignoring.end()) continue;
			if(nodes[i].rep != i)continue;
			int node = findd(i);
			
			aux = ((aux << nodes[node].cnt_color[0]) |(aux << nodes[node].cnt_color[1]) );
		}
		return aux[howMany];
	};
	auto check_and_erase = [&] (int u){
		if(nodes[u].rep != u){
			important.erase(u);
		}
	};
	for(auto i : exec){
		int u = i.ff, v = i.ss;
		u = findd(u), v = findd(v);
		if(u == v)continue;
		int c1 = nodes[i.ff].color, c2 = nodes[i.ss].color;
		int cnt0 = nodes[u].cnt_color[0], cnt1 = nodes[u].cnt_color[1];
		if(c1 == c2){
			cnt0 += nodes[v].cnt_color[1];
			cnt1 += nodes[v].cnt_color[0];
		}else{
			cnt0 += nodes[v].cnt_color[0];
			cnt1 += nodes[v].cnt_color[1];
		}

		if(check_ok({u, v}, n - cnt0)){
			// puedo hacer la union.
			//cout << i.ff << " en diferente a " << i.ss << endl;
			if(c1 == c2){
				// swapeo colores
				for(auto i : nodes[v].group){
					nodes[i].color = 1 - nodes[i].color;
				}
				swap(nodes[v].cnt_color[0], nodes[v].cnt_color[1]);
			}
			joinn(u, v);
			check_and_erase(u);
			check_and_erase(v);
		}else{
			//cout << i.ff << " en el mismo que " << i.ss << endl;
			if(c1 != c2){
				// swapeo colores
				
				for(auto i : nodes[v].group){
					nodes[i].color = 1 - nodes[i].color;
				}
				swap(nodes[v].cnt_color[0], nodes[v].cnt_color[1]);
			}
			joinn(u, v);
			check_and_erase(u);
			check_and_erase(v);
		}
	}
}

int main(){
    #ifdef LOCAL
		freopen("in","r", stdin);
	#endif
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m;
	for(int i = 1 ; i < 2 * n ; i ++){
		friends.pb({i, i + 1});
	}
	for(int i = 0 ; i < m ; i ++){
		int a, b;
		cin >> a >> b;
		friends.pb({a, b});
	}
	
	for(int i = 1 ; i <= 2 * n ; i ++){
		nodes[i].rep = i;
		nodes[i].color = 0;
		nodes[i].cnt_color[0] = 1;
		nodes[i].cnt_color[1] = 0;
		nodes[i].group.pb(i);
	}
	
	set<int>allRep;
	auto restart = [&] (){
		exec.clear();
		allRep.clear();
	};
	restart();

	while(!friends.empty()){
		allRep.insert(findd(friends.back().ff));
		allRep.insert(findd(friends.back().ss));
		exec.pb(friends.back());
		friends.pop_back();
		if(allRep.size() > sq){
			solve(allRep);
			restart();
		}
	}
	solve(allRep);
	for(int i = 1 ; i <= 2 * n ; i ++){
		cout << nodes[i].color ;
	}




	
    return 0;



}



详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3984kb

input:

2 4
1 3
2 4
1 4
1 2

output:

0101

result:

ok Output is valid. OK

Test #2:

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

input:

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

output:

011010

result:

ok Output is valid. OK

Test #3:

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

input:

1 0

output:

01

result:

ok Output is valid. OK

Test #4:

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

input:

1 1
1 2

output:

01

result:

ok Output is valid. OK

Test #5:

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

input:

2 3
2 4
3 4
1 2

output:

0110

result:

ok Output is valid. OK

Test #6:

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

input:

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

output:

010101

result:

ok Output is valid. OK

Test #7:

score: -100
Runtime Error

input:

4 9
4 7
3 8
1 5
2 7
2 8
6 8
7 8
1 4
1 6

output:


result: