UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#200880#165. 染色yizhiming1002984ms30280kbC++112.7kb2024-01-14 09:49:182024-01-14 12:14:28

answer

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int N = 4e5+5;
int n,m;
int in[N],col[N];
vector<int>ed[N];
int gcd(int a,int b){
	return !b?a:gcd(b,a%b);
}
void rs(int u){
	int sz = ed[u].size();
	for(int i=0;i<sz;i++){
		int x = ed[u][i];
		if(col[x]){
			continue;
		}
		col[x] = col[u]*-1;
		rs(x);
	}
} 
struct aa{
	int id,x,y;
}E[N],S[N],E1[N],E2[N];//S不变,E分治 
struct bb{
	int id,nxt;
}e[N*2];
int cnt,tote,tot1,tot2;
int head[N];bool vis[N];
void link(int u,int i){
	e[++tote] = (bb){i,head[u]};head[u] = tote;
//	cout<<"TOT:"<<u<<" "<<tote<<"\n"; 
}
void oula(int u){
//	cout<<"U:"<<u<<" "<<head[u]<<"\n"; 
	for(int &i=head[u];i;i=e[i].nxt){
//		head[u] = i; 
		aa x = S[e[i].id];
		if(vis[x.id]){
			continue;
		}
		vis[x.id] = 1;
		if(col[u]==1){
			E1[++tot1] = x;
		}else{
			E2[++tot2] = x;
		}
		if(x.x==u){
			oula(x.y);
		}else{
			oula(x.x);
		}
	}
}
int ans[N];
void sol(int k,int l,int r){
//	cout<<"K:"<<k<<" "<<l<<" "<<r<<"\n"; 
	if(!k){
		cnt++;
		for(int i=l;i<=r;i++){
			ans[E[i].id] = cnt;
		}
		return;
	}
	for(int i=l;i<=r;i++){
		head[E[i].x] = head[E[i].y] = 0;
	}
	tote = 0;
	for(int i=l;i<=r;i++){
		link(E[i].x,E[i].id);link(E[i].y,E[i].id);
	}
	tot1 = tot2 = 0;
//	cout<<"there\n"; 
	for(int i=l;i<=r;i++){
		oula(E[i].x);
		oula(E[i].y);
	}
//	cout<<"hrere\n"; 
	for(int i=l;i<=r;i++){
		vis[E[i].id] = 0;
	}
	int mid = (l+r)/2;
	for(int i=l;i<=mid;i++){
		E[i] = E1[i-l+1];
	}
	for(int i=mid+1;i<=r;i++){
		E[i] = E2[i-mid];
	}
	sol(k-1,l,mid);sol(k-1,mid+1,r);
}
void init(){
	for(int i=1;i<=n;i++){
		in[i] = 0;
		ed[i].clear();
		col[i] = 0;
	}
	cnt = 0; 
	for(int i=1;i<=m;i++){
		int x,y;
		x = read();y = read();
		in[x]++;in[y]++;E[i] = (aa){i,x,y};S[i] = E[i];
		ed[x].push_back(y);ed[y].push_back(x);
	}
	for(int i=1;i<=n;i++){
		if((in[i]&1)||(!in[i])){
			cout<<-1<<"\n";
			return;
		}
		if(!col[i]){
			col[i] = 1;
			rs(i);
		}
	}
	int now = 0;
	for(int i=1;i<=n;i++){
		now = gcd(now,in[i]);
	} 
	int k = 0;
	while(now%2==0){
		k++;now>>=1;
	}
	cout<<k<<"\n";
	sol(k,1,m);
	for(int i=1;i<=m;i++){
		cout<<ans[i]<<" ";
	}
	cout<<"\n";
	
}
int main(){
//	freopen("ex_coloring1.in","r",stdin);
//	freopen("me.txt","w",stdout); 
	while(1){
		n = read();m = read();
		if(n==0&&m==0){
			break;
		} 
//		cout<<1<<"\n"; 
		init();
//		cout<<2<<"\n";
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 10628kb

input:

2 2
1 2
1 2
2 2
2 1
2 1
4 6
1 2
2 1
3 2
3 4
1 2
1 4
4 6
3 2
4 1
2 1
3 4
4 3
3 4
6 2
4 3
4 3
6 2
1 2
...

output:

1
2 1 
1
1 2 
1
2 1 1 2 2 1 
1
2 2 1 1 2 1 
-1
-1
-1
1
2 1 1 2 1 2 
1
2 1 
1
2 1 2 1 2 1 

result:

ok Correct

Test #2:

score: 5
Accepted
time: 3ms
memory: 10636kb

input:

50 100
28 29
39 44
3 24
46 9
1 37
32 21
13 5
37 4
43 35
34 8
38 30
7 20
39 44
12 42
28 46
15 48
12 1...

output:

2
1 3 1 4 3 1 1 1 3 1 3 1 2 1 2 1 3 3 1 4 3 2 4 2 1 1 1 1 4 3 4 4 1 2 1 4 3 4 3 3 1 3 2 3 1 4 3 2 4 ...

result:

ok Correct

Test #3:

score: 5
Accepted
time: 0ms
memory: 11424kb

input:

2 64
1 2
2 1
2 1
2 1
2 1
1 2
2 1
2 1
1 2
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
2 1
1 2
2 1
1 2
1 2
2 1
2 1...

output:

6
44 18 50 1 38 31 58 14 48 23 53 7 36 25 64 12 41 19 51 3 39 30 60 15 45 21 55 5 34 28 61 10 43 17 ...

result:

ok Correct

Test #4:

score: 5
Accepted
time: 0ms
memory: 11428kb

input:

18 100
16 3
5 11
3 16
17 1
8 4
18 11
14 9
4 8
16 1
8 3
4 9
8 14
14 17
5 6
1 16
8 3
4 17
2 10
16 4
3 ...

output:

2
2 3 4 3 3 1 1 2 1 1 4 4 2 1 3 3 1 4 2 2 4 2 3 1 4 1 3 3 1 3 4 4 1 4 4 1 2 4 1 4 3 2 4 1 2 2 3 2 4 ...

result:

ok Correct

Test #5:

score: 5
Accepted
time: 0ms
memory: 10640kb

input:

14 96
7 14
7 14
9 4
9 3
9 6
11 6
12 10
4 2
7 8
7 14
12 8
2 5
14 3
6 11
3 8
13 7
2 3
4 2
14 12
11 7
1...

output:

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

result:

ok Correct

Test #6:

score: 5
Accepted
time: 0ms
memory: 11660kb

input:

28 960
8 28
16 12
26 9
21 8
19 27
7 28
7 20
17 4
22 15
5 16
24 6
2 3
1 12
3 2
20 8
13 11
7 3
16 5
13...

output:

4
9 10 6 16 6 2 11 5 11 11 5 6 2 14 1 2 14 4 11 5 13 4 6 13 16 11 11 2 4 14 8 3 16 16 14 15 11 8 10 ...

result:

ok Correct

Test #7:

score: 5
Accepted
time: 0ms
memory: 11628kb

input:

100000 1000
67206 47147
79800 59716
1206 51819
73296 22150
88550 91876
55703 81024
50775 94209
39276...

output:

-1
2
2 4 4 4 2 2 2 4 4 2 2 4 4 2 2 2 4 2 4 4 4 4 4 4 2 2 4 2 4 1 4 2 4 2 2 4 2 2 2 4 2 2 2 4 2 3 2 4...

result:

ok Correct

Test #8:

score: 5
Accepted
time: 7ms
memory: 10768kb

input:

26 992
15 11
12 6
2 6
4 8
5 2
8 4
10 3
2 6
14 1
22 24
8 4
18 10
3 10
12 5
24 26
24 25
15 22
2 6
2 5
...

output:

5
5 26 2 30 25 11 26 22 1 20 17 10 5 5 11 16 12 10 13 26 2 6 32 9 18 29 24 16 31 32 5 32 18 3 31 24 ...

result:

ok Correct

Test #9:

score: 5
Accepted
time: 8ms
memory: 11752kb

input:

2 512
1 2
2 1
1 2
2 1
1 2
2 1
2 1
1 2
2 1
2 1
1 2
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
1 2
1 2
2 1
2 1
2 ...

output:

9
512 139 344 11 414 193 291 118 457 161 374 33 427 227 267 65 491 153 329 22 400 219 313 99 475 179...

result:

ok Correct

Test #10:

score: 5
Accepted
time: 105ms
memory: 21056kb

input:

3 65538
1 3
1 3
3 1
3 1
3 1
3 1
1 3
3 1
1 3
1 3
3 1
3 1
1 3
1 3
1 3
3 1
3 1
3 1
1 3
1 3
1 3
1 3
3 1
...

output:

1
2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 ...

result:

ok Correct

Test #11:

score: 5
Accepted
time: 97ms
memory: 19952kb

input:

59 63122
10 46
38 15
38 15
54 58
36 31
15 38
15 38
13 6
40 18
20 3
42 53
15 38
38 15
36 35
43 59
7 4...

output:

1
2 1 2 2 1 1 2 2 2 1 1 1 2 2 2 2 2 1 1 2 1 2 2 1 2 1 1 1 2 2 2 1 2 1 2 1 1 2 1 1 2 1 1 1 1 2 2 2 1 ...

result:

ok Correct

Test #12:

score: 5
Accepted
time: 170ms
memory: 21108kb

input:

5 44160
2 3
2 3
3 2
2 3
3 2
2 3
3 2
2 3
2 3
2 3
3 2
5 4
3 2
2 3
2 3
3 2
2 3
2 3
3 2
3 2
2 3
2 3
3 2
...

output:

1
1 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 1 2 1 2 1 2 1 ...

result:

ok Correct

Test #13:

score: 5
Accepted
time: 174ms
memory: 20880kb

input:

51 81396
24 47
16 19
18 12
15 17
17 24
41 38
41 38
39 15
6 28
41 38
1 45
8 40
12 18
32 18
32 9
1 20
...

output:

1
2 2 2 1 2 1 2 2 2 1 2 1 1 2 1 2 1 2 2 1 1 2 1 1 2 1 2 2 2 1 1 1 2 2 2 1 2 1 1 1 2 2 1 1 1 1 1 2 1 ...

result:

ok Correct

Test #14:

score: 5
Accepted
time: 405ms
memory: 28764kb

input:

18501 99464
17035 15227
16566 3728
6261 13464
4632 18396
12489 12196
1970 946
1184 18101
14549 3748
...

output:

-1
1
2 2 2 2 2 2 1 1 2 1 1 1 2 1 1 1 2 2 2 2 2 2 1 1 2 2 1 2 1 1 2 1 1 1 2 2 2 2 2 2 1 1 2 2 1 1 2 1...

result:

ok Correct

Test #15:

score: 5
Accepted
time: 386ms
memory: 30280kb

input:

100000 100000
87535 29423
11245 50490
4990 94798
76698 27810
91957 59594
15340 91794
53456 9890
1856...

output:

-1
2
2 2 2 2 3 2 3 3 3 3 3 2 2 2 2 2 3 2 2 3 2 2 3 3 3 2 3 3 2 2 2 3 2 3 3 3 3 2 2 2 3 3 3 2 2 3 2 2...

result:

ok Correct

Test #16:

score: 5
Accepted
time: 333ms
memory: 25776kb

input:

16177 99448
6697 7820
4382 1906
9663 7642
4964 3657
3115 5436
15939 15537
941 6960
10413 8806
15010 ...

output:

-1
2
3 2 3 2 2 2 3 4 2 3 3 4 3 2 2 1 4 2 3 4 3 3 4 1 2 4 1 4 4 3 3 4 3 2 2 4 2 2 2 3 1 3 4 1 2 2 1 3...

result:

ok Correct

Test #17:

score: 5
Accepted
time: 414ms
memory: 27980kb

input:

19909 99896
2069 35
4444 11432
12554 19811
11258 2892
9235 4216
4268 19722
6172 10040
12293 18027
71...

output:

-1
1
1 2 2 2 2 2 1 1 2 2 1 1 1 1 2 1 1 1 1 2 2 1 2 2 1 2 1 2 1 2 1 1 1 2 1 1 1 1 1 2 2 1 1 2 2 2 1 2...

result:

ok Correct

Test #18:

score: 5
Accepted
time: 427ms
memory: 29204kb

input:

19616 99048
10595 16065
18834 16198
1626 15646
14844 10647
386 1596
9093 17398
3676 13759
15325 1100...

output:

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

result:

ok Correct

Test #19:

score: 5
Accepted
time: 453ms
memory: 25996kb

input:

46 85248
2 22
23 9
16 22
42 23
22 9
38 33
22 7
29 30
23 7
23 9
2 22
27 26
12 34
25 45
15 46
21 4
23 ...

output:

5
22 18 12 8 7 20 3 22 28 9 8 10 26 3 28 16 32 22 27 27 12 23 15 24 7 7 28 27 8 19 22 16 24 16 32 25...

result:

ok Correct

Test #20:

score: 5
Accepted
time: 2ms
memory: 10632kb

input:

2 6
1 2
2 1
1 2
1 2
1 2
2 1
4 4
1 4
1 2
2 1
1 4
2 2
1 2
1 2
6 6
1 2
1 4
2 5
2 5
5 4
2 5
2 4
1 2
2 1
...

output:

1
2 1 2 1 2 1 
-1
1
2 1 
-1
2
3 2 4 1 
-1
-1
-1
1
2 1 2 1 2 1 

result:

ok Correct

Extra Test:

score: 0
Extra Test Passed