-
Notifications
You must be signed in to change notification settings - Fork 0
/
L.cpp
80 lines (79 loc) · 1.54 KB
/
L.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
int a[maxn][maxn];
int col[maxn];
int deg[maxn];
bool vis[maxn];
int n;
void recurse() {
int piv = -1;
for(int i = 1; i <= n; i++) {
if(vis[i]) continue;
if(deg[i] % 2 == 1) {
piv = i;
break;
}
}
if(piv == -1) {
for(int i = 1; i <= n; i++) {
if(vis[i]) continue;
col[i] = 0;
}
return ;
}
vis[piv] = true;
vector <int> adj;
for(int i = 1; i <= n; i++) {
if(vis[i]) continue;
if(a[piv][i] == 1) {
a[piv][i] = a[i][piv] = 0;
adj.push_back(i);
deg[i] -= 1;
}
}
for(auto i : adj) {
for(auto j : adj) {
if(i == j) continue;
if(a[i][j] == 1) {
a[i][j] = 0;
deg[i] -= 1;
} else {
a[i][j] = 1;
deg[i] += 1;
}
}
}
recurse();
int cnt[] = {0, 0};
for(int i : adj) {
cnt[col[i]] += 1;
}
col[piv] = cnt[0] % 2 == 0 ? 0 : 1;
}
void solve() {
int m;
cin >> n >> m;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[i][j] = 0;
memset(deg, 0, sizeof deg);
memset(vis, false, sizeof vis);
for(int i = 1; i <= m; i++) {
int p, q;
cin >> p >> q;
a[p][q] = a[q][p] = 1;
deg[p] += 1;
deg[q] += 1;
}
recurse();
cout << *max_element(col + 1, col + n + 1) + 1 << endl;
for(int i = 1; i <= n; i++) cout << 1 + col[i] << ' ';
cout << endl;
}
int main() {
int t;
cin >> t;
while(t--) {
solve();
}
return 0;
}