Submission #2508991
Source Code Expand
use std::io::*; use std::str::FromStr; use std::collections::HashMap; #[allow(dead_code)] fn read<T: FromStr>() -> T { let stdin = stdin(); let stdin = stdin.lock(); let token: String = stdin .bytes() .map(|c| c.expect("failed to read char") as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().expect("failed to parse token") } type Weight = i32; #[allow(dead_code)] #[derive(Clone, Debug, Eq, PartialEq)] struct Edge { index: usize, src: usize, dest: usize, weight: Weight, } #[allow(dead_code)] impl Edge { fn new(index: usize, src: usize, dest: usize, weight: Weight) -> Edge { Edge { index: index, src: src, dest: dest, weight: weight, } } } impl std::cmp::Ord for Edge { fn cmp(&self, other: &Self) -> std::cmp::Ordering { (self.weight, self.src, self.dest).cmp(&(other.weight, other.src, other.dest)) } } impl std::cmp::PartialOrd for Edge { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { Some(self.cmp(other)) } } type Edges = Vec<Edge>; type Graph = Vec<Edges>; #[allow(dead_code)] fn graph_new(n: usize) -> Graph { vec![vec![]; n] } #[allow(dead_code)] fn add_bi_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) { graph[src].push(Edge::new(*index, src, dest, weight)); *index += 1; graph[dest].push(Edge::new(*index, dest, src, weight)); *index += 1; } struct Solver { n: usize, g: Graph, edge_index: HashMap<(usize, usize), usize>, initial_colors: Vec<usize>, memo: Vec<Vec<i64>>, memo_all_black: Vec<Option<bool>>, memo_total: Vec<(usize, i64)>, memo_childs: Vec<Vec<(i64, i64, i64, usize)>>, memo_all_black_total: Vec<i64>, visit_cnt: Vec<Vec<i64>>, visit_cnt_all_black: Vec<i64>, } impl Solver { fn new() -> Solver { let n = read::<usize>(); let mut g = graph_new(n); let mut edge_index = HashMap::<(usize, usize), usize>::new(); let mut e = 0; for _ in 0..n - 1 { let x = read::<usize>() - 1; let y = read::<usize>() - 1; edge_index.insert((x, y), e); edge_index.insert((y, x), e + 1); add_bi_edge(&mut g, &mut e, x, y, 0); } let initial_colors = read::<String>() .chars() .map(|c| if c == 'W' { 0 } else { 1 }) .collect::<Vec<usize>>(); Solver { n: n, g: g, edge_index: edge_index, initial_colors: initial_colors, memo: vec![vec![-1; 2 * (n - 1)]; 2], memo_all_black: vec![None; 2 * (n - 1)], memo_total: vec![(0, -1); n], memo_childs: vec![vec![]; n], memo_all_black_total: vec![-1; n], visit_cnt: vec![vec![0; n]; 2], visit_cnt_all_black: vec![0; n], } } fn move_edge_all_black(&mut self, edge: &Edge) -> bool { if self.memo_all_black[edge.index].is_some() { return self.memo_all_black[edge.index].unwrap(); } let ret = self.all_black(edge.dest, edge.src); self.memo_all_black[edge.index] = Some(ret); ret } fn all_black(&mut self, from: usize, parent: usize) -> bool { if self.initial_colors[from] == 0 { return false; } self.visit_cnt_all_black[from] += 1; if self.visit_cnt_all_black[from] > 2 { if self.memo_all_black_total[from] == -1 { let mut sum = 0; for edge in self.g[from].clone().iter() { sum += if self.move_edge_all_black(edge) { 1 } else { 0 }; } self.memo_all_black_total[from] = sum; } let rev_index = self.edge_index[&(from, parent)]; let mut cnt = self.memo_all_black_total[from]; cnt -= if self.memo_all_black[rev_index].unwrap() { 1 } else { 0 }; return cnt == self.g[from].len() as i64 - 1; } for edge in self.g[from].clone().iter() { if edge.dest == parent { continue; } if !self.move_edge_all_black(edge) { return false; } } true } fn move_edge(&mut self, back: usize, edge: &Edge) -> i64 { if self.memo[back][edge.index] != -1 { return self.memo[back][edge.index]; } let c = self.initial_colors[edge.dest] ^ 1; let ret = back as i64 + 1 + self.dfs(back, c, edge.dest, edge.src); self.memo[back][edge.index] = ret; ret } fn dfs(&mut self, back: usize, c: usize, from: usize, parent: usize) -> i64 { self.visit_cnt[back][from] += 1; if c != self.initial_colors[from] && self.visit_cnt[back][from] > 2 { let mut c = c; let mut childs = vec![]; if self.memo_total[from].1 == -1 { let mut sum = 0; for edge in self.g[from].clone().iter() { if self.move_edge_all_black(edge) { continue; } c ^= 1; let one_way = self.move_edge(0, edge); let need_back = self.move_edge(1, edge); let diff = need_back - one_way; childs.push((diff, need_back, one_way, edge.dest)); sum += need_back; } childs.sort(); childs.reverse(); self.memo_total[from] = (c, sum); self.memo_childs[from] = childs; } let rev_index = self.edge_index[&(from, parent)]; let mut c = self.memo_total[from].0; let mut ret = self.memo_total[from].1; if !self.memo_all_black[rev_index].unwrap() { c ^= 1; ret -= self.memo[1][rev_index]; } if back == 0 && self.memo_childs[from].len() > 0 { if self.memo_childs[from][0].3 != parent { c ^= 1; ret -= self.memo_childs[from][0].0; } else if self.memo_childs[from].len() > 1 { c ^= 1; ret -= self.memo_childs[from][1].0; } } if c == 0 { ret += 1; } return ret; } let mut c = c; let mut ret = 0; let mut childs = vec![]; for edge in self.g[from].clone().iter() { if edge.dest == parent || self.move_edge_all_black(edge) { continue; } c ^= 1; let one_way = self.move_edge(0, edge); let need_back = self.move_edge(1, edge); let diff = need_back - one_way; childs.push((diff, need_back, one_way)); ret += need_back; } childs.sort(); childs.reverse(); if back == 0 && childs.len() > 0 { // println!("{} {:?}", from, childs); c ^= 1; ret -= childs[0].0; } if c == 0 { ret += 1; } ret } fn solve(&mut self) { let mut ans = 1 << 60; for i in 0..self.n { let c = self.initial_colors[i]; let nret = self.dfs(0, c, i, i); ans = std::cmp::min(ans, nret); } println!("{}", ans); } } fn main() { let mut solver = Solver::new(); solver.solve(); }
Submission Info
Submission Time | |
---|---|
Task | F - Monochrome Cat |
User | cos |
Language | Rust (1.15.1) |
Score | 800 |
Code Size | 7995 Byte |
Status | AC |
Exec Time | 269 ms |
Memory | 89724 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 800 / 800 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt |
All | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt, 1_036.txt, 1_037.txt, 1_038.txt, 1_039.txt, 1_040.txt, 1_041.txt, 1_042.txt, 1_043.txt, 1_044.txt, 1_045.txt, 1_046.txt, 1_047.txt, 1_048.txt, 1_049.txt, 1_050.txt, 1_051.txt, 1_052.txt, 1_053.txt, 1_054.txt, 1_055.txt, 1_056.txt, 1_057.txt, 1_058.txt, 1_059.txt, 1_060.txt, 1_061.txt, 1_062.txt, 1_063.txt, 1_064.txt, 1_065.txt, 1_066.txt, 1_067.txt, 1_068.txt, 1_069.txt, 1_070.txt, 1_071.txt, 1_072.txt, 1_073.txt, 1_074.txt, 1_075.txt, 1_076.txt, 1_077.txt, 1_078.txt, 1_079.txt, 1_080.txt, 1_081.txt, 1_082.txt, 1_083.txt, 1_084.txt, 1_085.txt, 1_086.txt, 1_087.txt, 1_088.txt, 1_089.txt, 1_090.txt, 1_091.txt, 1_092.txt, 1_093.txt, 1_094.txt, 1_095.txt, 1_096.txt, 1_097.txt, 1_098.txt, 1_099.txt, 1_100.txt, 1_101.txt, 1_102.txt, 1_103.txt, 1_104.txt, 1_105.txt, 1_106.txt, 1_107.txt, 1_108.txt, 1_109.txt, 1_110.txt, 1_111.txt, 1_112.txt, 1_113.txt, 1_114.txt, 1_115.txt, 1_116.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_000.txt | AC | 2 ms | 4352 KB |
0_001.txt | AC | 2 ms | 4352 KB |
0_002.txt | AC | 2 ms | 4352 KB |
0_003.txt | AC | 2 ms | 4352 KB |
1_003.txt | AC | 2 ms | 4352 KB |
1_004.txt | AC | 2 ms | 4352 KB |
1_005.txt | AC | 2 ms | 4352 KB |
1_006.txt | AC | 2 ms | 4352 KB |
1_007.txt | AC | 2 ms | 4352 KB |
1_008.txt | AC | 2 ms | 4352 KB |
1_009.txt | AC | 2 ms | 4352 KB |
1_010.txt | AC | 2 ms | 4352 KB |
1_011.txt | AC | 2 ms | 4352 KB |
1_012.txt | AC | 2 ms | 4352 KB |
1_013.txt | AC | 2 ms | 4352 KB |
1_014.txt | AC | 2 ms | 4352 KB |
1_015.txt | AC | 2 ms | 4352 KB |
1_016.txt | AC | 2 ms | 4352 KB |
1_017.txt | AC | 2 ms | 4352 KB |
1_018.txt | AC | 2 ms | 4352 KB |
1_019.txt | AC | 2 ms | 4352 KB |
1_020.txt | AC | 2 ms | 4352 KB |
1_021.txt | AC | 2 ms | 4352 KB |
1_022.txt | AC | 2 ms | 4352 KB |
1_023.txt | AC | 2 ms | 4352 KB |
1_024.txt | AC | 2 ms | 4352 KB |
1_025.txt | AC | 2 ms | 4352 KB |
1_026.txt | AC | 2 ms | 4352 KB |
1_027.txt | AC | 2 ms | 4352 KB |
1_028.txt | AC | 2 ms | 4352 KB |
1_029.txt | AC | 2 ms | 4352 KB |
1_030.txt | AC | 2 ms | 4352 KB |
1_031.txt | AC | 2 ms | 4352 KB |
1_032.txt | AC | 2 ms | 4352 KB |
1_033.txt | AC | 2 ms | 4352 KB |
1_034.txt | AC | 2 ms | 4352 KB |
1_035.txt | AC | 2 ms | 4352 KB |
1_036.txt | AC | 2 ms | 4352 KB |
1_037.txt | AC | 2 ms | 4352 KB |
1_038.txt | AC | 2 ms | 4352 KB |
1_039.txt | AC | 2 ms | 4352 KB |
1_040.txt | AC | 2 ms | 4352 KB |
1_041.txt | AC | 2 ms | 4352 KB |
1_042.txt | AC | 2 ms | 4352 KB |
1_043.txt | AC | 2 ms | 4352 KB |
1_044.txt | AC | 2 ms | 4352 KB |
1_045.txt | AC | 69 ms | 30644 KB |
1_046.txt | AC | 95 ms | 41664 KB |
1_047.txt | AC | 151 ms | 55804 KB |
1_048.txt | AC | 155 ms | 54256 KB |
1_049.txt | AC | 165 ms | 56444 KB |
1_050.txt | AC | 152 ms | 52348 KB |
1_051.txt | AC | 146 ms | 52976 KB |
1_052.txt | AC | 28 ms | 14320 KB |
1_053.txt | AC | 112 ms | 45296 KB |
1_054.txt | AC | 52 ms | 24696 KB |
1_055.txt | AC | 142 ms | 52604 KB |
1_056.txt | AC | 64 ms | 26616 KB |
1_057.txt | AC | 4 ms | 4604 KB |
1_058.txt | AC | 84 ms | 29240 KB |
1_059.txt | AC | 45 ms | 19688 KB |
1_060.txt | AC | 149 ms | 43516 KB |
1_061.txt | AC | 189 ms | 51836 KB |
1_062.txt | AC | 85 ms | 31288 KB |
1_063.txt | AC | 125 ms | 35068 KB |
1_064.txt | AC | 83 ms | 24748 KB |
1_065.txt | AC | 8 ms | 6396 KB |
1_066.txt | AC | 141 ms | 47272 KB |
1_067.txt | AC | 32 ms | 12540 KB |
1_068.txt | AC | 101 ms | 26872 KB |
1_069.txt | AC | 42 ms | 14588 KB |
1_070.txt | AC | 78 ms | 22776 KB |
1_071.txt | AC | 123 ms | 37116 KB |
1_072.txt | AC | 86 ms | 22780 KB |
1_073.txt | AC | 16 ms | 8444 KB |
1_074.txt | AC | 10 ms | 6396 KB |
1_075.txt | AC | 72 ms | 22816 KB |
1_076.txt | AC | 186 ms | 45436 KB |
1_077.txt | AC | 110 ms | 37116 KB |
1_078.txt | AC | 46 ms | 14588 KB |
1_079.txt | AC | 75 ms | 22788 KB |
1_080.txt | AC | 77 ms | 22904 KB |
1_081.txt | AC | 235 ms | 71932 KB |
1_082.txt | AC | 269 ms | 73596 KB |
1_083.txt | AC | 174 ms | 61776 KB |
1_084.txt | AC | 209 ms | 68732 KB |
1_085.txt | AC | 259 ms | 87764 KB |
1_086.txt | AC | 236 ms | 89724 KB |
1_087.txt | AC | 187 ms | 66344 KB |
1_088.txt | AC | 170 ms | 58348 KB |
1_089.txt | AC | 132 ms | 53488 KB |
1_090.txt | AC | 150 ms | 53488 KB |
1_091.txt | AC | 202 ms | 66344 KB |
1_092.txt | AC | 146 ms | 53488 KB |
1_093.txt | AC | 209 ms | 60668 KB |
1_094.txt | AC | 227 ms | 69244 KB |
1_095.txt | AC | 148 ms | 51832 KB |
1_096.txt | AC | 195 ms | 58876 KB |
1_097.txt | AC | 244 ms | 64376 KB |
1_098.txt | AC | 221 ms | 66552 KB |
1_099.txt | AC | 209 ms | 53324 KB |
1_100.txt | AC | 200 ms | 53208 KB |
1_101.txt | AC | 156 ms | 53500 KB |
1_102.txt | AC | 170 ms | 53500 KB |
1_103.txt | AC | 212 ms | 53228 KB |
1_104.txt | AC | 187 ms | 52952 KB |
1_105.txt | AC | 190 ms | 49532 KB |
1_106.txt | AC | 200 ms | 47480 KB |
1_107.txt | AC | 135 ms | 45304 KB |
1_108.txt | AC | 173 ms | 45304 KB |
1_109.txt | AC | 199 ms | 49532 KB |
1_110.txt | AC | 204 ms | 49532 KB |
1_111.txt | AC | 205 ms | 49532 KB |
1_112.txt | AC | 189 ms | 47484 KB |
1_113.txt | AC | 136 ms | 45308 KB |
1_114.txt | AC | 169 ms | 45304 KB |
1_115.txt | AC | 194 ms | 49404 KB |
1_116.txt | AC | 189 ms | 49404 KB |