QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#621162#9449. New School TermAPROHACK124WA 15ms4540kbC++144.1kb2024-10-08 10:18:102024-10-08 10:18:23

Judging History

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

  • [2024-10-08 10:18:23]
  • 评测
  • 测评结果:WA
  • 用时:15ms
  • 内存:4540kb
  • [2024-10-08 10:18:10]
  • 提交

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 ;
	set<int>others;
	vector<int>allOthers;
	for(int i = 1 ; i <= 2 * n ; i ++){
		int rep = nodes[i].rep;
		if(important.find(rep) != important.end())continue;
		others.insert(rep);
	}
	for(auto i : others)allOthers.pb(i);
	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 = findd(allOthers[i]);
			bitmask = ((bitmask << nodes[node].cnt_color[0]) |(bitmask << nodes[node].cnt_color[1]) );
		}
	};
	getBitmask();

	function<bool (set<int>, int)> check_ok = [&] (set<int>ignoring, int howMany){
		if(howMany < 0)return false;
		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]) );
		}
		if(aux[howMany])return true;
		return false;
	};
	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;
		//cout << "exec " << i.ff << " " << i.ss << endl;
		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.
			#ifdef LOCAL
			cout << i.ff << " en diferente a " << i.ss << endl;
			#endif
			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{
			#ifdef LOCAL
			cout << i.ff << " en el mismo que " << i.ss << endl;
			#endif
			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;



}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 3940kb

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: 0ms
memory: 3980kb

input:

1 0

output:

01

result:

ok Output is valid. OK

Test #4:

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

input:

1 1
1 2

output:

01

result:

ok Output is valid. OK

Test #5:

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

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: 4004kb

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: 0
Accepted
time: 0ms
memory: 3936kb

input:

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

output:

01010110

result:

ok Output is valid. OK

Test #8:

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

input:

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

output:

1101000101

result:

ok Output is valid. OK

Test #9:

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

input:

6 13
4 5
2 9
3 8
4 8
4 11
10 12
3 4
3 9
5 11
2 8
5 10
5 8
1 11

output:

110110001001

result:

ok Output is valid. OK

Test #10:

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

input:

12 153
1 24
16 18
7 14
1 16
20 21
9 14
21 22
4 5
17 24
4 12
5 17
13 24
14 15
12 23
12 16
8 11
14 24
9 16
2 5
6 19
11 17
4 22
4 7
6 16
7 20
8 15
5 24
2 10
10 21
21 24
1 12
11 19
18 21
18 24
12 17
13 22
7 9
13 23
4 9
11 13
15 21
5 7
2 4
15 16
17 19
11 16
11 20
7 8
4 15
13 14
6 18
2 19
9 13
23 24
4 21
...

output:

000011100110010001111110

result:

ok Output is valid. OK

Test #11:

score: -100
Wrong Answer
time: 15ms
memory: 4540kb

input:

259 33757
472 500
65 336
138 469
307 442
427 458
43 239
17 508
460 466
108 393
79 92
250 483
44 277
17 132
35 57
155 499
184 474
246 272
274 418
457 458
338 372
196 514
31 208
117 187
90 229
153 284
189 355
16 337
146 456
269 271
279 412
305 336
303 441
399 472
85 286
91 97
157 437
137 379
71 360
27...

output:

111111000100101100000100001001011000011100010111010111010011001101000001001001001110101010001101101101101000000000101100010010100011100001100101111111101001001110101101101010001100100001111110000111011111000110111001011101111000110010011110011001100111011101110001111000100101010110111001011011000010...

result:

wrong answer The division is not minimized.