-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChromaticNumber.cpp
More file actions
26 lines (26 loc) · 1.01 KB
/
ChromaticNumber.cpp
File metadata and controls
26 lines (26 loc) · 1.01 KB
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
int ChromaticNumber(const vector<int> &g) {
int n = g.size();
if (n == 0) return 0;
//randomly choose a large prime
const int modulo = 1077563119;
int all = 1 << n;
vector<int> ind(all), s(all);
for (int i = 0; i < all; i ++) s[i] = ((n - __builtin_popcount(i)) & 1 ? -1 : 1);
ind[0] = 1;
for (int i = 1; i < all; i ++) {
int ctz = __builtin_ctz(i);
ind[i] = ind[i - (1 << ctz)] + ind[(i - (1 << ctz)) & ~g[ctz]];
if (ind[i] >= modulo) ind[i] -= modulo;
}
//compute the chromatic number (= \sum (-1)^(n - |i|) * (ind[i])^k)
for (int k = 1; k < n; k ++) {
long long sum = 0;
for (int i = 0; i < all; i ++) {
long long cur = ((s[i] * (long long) ind[i]) % modulo);
s[i] = (int) cur;
sum += cur;
}
if (sum % modulo != 0) return k;
}
return n;
}