-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchromatic_number.rs
34 lines (34 loc) · 970 Bytes
/
chromatic_number.rs
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
// https://www.cs.helsinki.fi/u/mkhkoivi/publications/sicomp-2009.pdf
// O(2^n n)
// Note: this implementation is probabilisitic and therefore CAN FAIL.
fn chromatic_number(g: &[usize]) -> usize {
let n = g.len();
let mut dp = vec![MInt::new(0); 1 << n];
for bits in (0..(1 << n) - 1).rev() {
for i in 0..n {
if (bits & 1 << i) == 0 {
dp[bits] = dp[bits | 1 << i] + dp[bits | g[i] | 1 << i] + 1;
break;
}
}
}
let mut cur = vec![MInt::new(0); 1 << n];
for bits in 0usize..1 << n {
if bits.count_ones() % 2 == 0 {
cur[bits] += 1;
} else {
cur[bits] -= 1;
}
}
for i in 0..n + 1 {
let mut tot = MInt::new(0);
for bits in 0usize..1 << n {
tot += cur[bits];
cur[bits] *= dp[bits];
}
if tot != MInt::new(0) {
return i;
}
}
unreachable!();
}