結果
問題 | No.1640 簡単な色塗り |
ユーザー | akakimidori |
提出日時 | 2021-08-06 23:25:54 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 133 ms / 2,000 ms |
コード長 | 4,741 bytes |
コンパイル時間 | 12,584 ms |
コンパイル使用メモリ | 402,676 KB |
実行使用メモリ | 25,244 KB |
最終ジャッジ日時 | 2024-06-29 16:20:49 |
合計ジャッジ時間 | 22,395 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 31 ms
16,992 KB |
testcase_05 | AC | 30 ms
16,992 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 31 ms
10,692 KB |
testcase_11 | AC | 25 ms
9,184 KB |
testcase_12 | AC | 22 ms
8,368 KB |
testcase_13 | AC | 53 ms
15,492 KB |
testcase_14 | AC | 53 ms
15,588 KB |
testcase_15 | AC | 16 ms
6,940 KB |
testcase_16 | AC | 20 ms
7,688 KB |
testcase_17 | AC | 42 ms
13,576 KB |
testcase_18 | AC | 3 ms
6,940 KB |
testcase_19 | AC | 22 ms
8,308 KB |
testcase_20 | AC | 29 ms
10,400 KB |
testcase_21 | AC | 24 ms
8,964 KB |
testcase_22 | AC | 3 ms
6,940 KB |
testcase_23 | AC | 32 ms
11,176 KB |
testcase_24 | AC | 9 ms
6,940 KB |
testcase_25 | AC | 22 ms
8,472 KB |
testcase_26 | AC | 37 ms
12,420 KB |
testcase_27 | AC | 16 ms
6,944 KB |
testcase_28 | AC | 55 ms
14,948 KB |
testcase_29 | AC | 37 ms
12,556 KB |
testcase_30 | AC | 6 ms
6,944 KB |
testcase_31 | AC | 51 ms
25,244 KB |
testcase_32 | AC | 43 ms
20,732 KB |
testcase_33 | AC | 33 ms
14,760 KB |
testcase_34 | AC | 40 ms
18,496 KB |
testcase_35 | AC | 19 ms
11,808 KB |
testcase_36 | AC | 8 ms
6,944 KB |
testcase_37 | AC | 13 ms
6,940 KB |
testcase_38 | AC | 37 ms
19,156 KB |
testcase_39 | AC | 17 ms
10,672 KB |
testcase_40 | AC | 14 ms
8,704 KB |
testcase_41 | AC | 36 ms
17,128 KB |
testcase_42 | AC | 16 ms
10,284 KB |
testcase_43 | AC | 24 ms
12,508 KB |
testcase_44 | AC | 25 ms
12,084 KB |
testcase_45 | AC | 19 ms
10,324 KB |
testcase_46 | AC | 5 ms
6,940 KB |
testcase_47 | AC | 6 ms
6,940 KB |
testcase_48 | AC | 67 ms
23,344 KB |
testcase_49 | AC | 2 ms
6,944 KB |
testcase_50 | AC | 0 ms
6,944 KB |
testcase_51 | AC | 1 ms
6,940 KB |
testcase_52 | AC | 63 ms
23,388 KB |
testcase_53 | AC | 133 ms
19,556 KB |
07_evil_01.txt | AC | 137 ms
32,480 KB |
07_evil_02.txt | AC | 241 ms
49,544 KB |
ソースコード
struct BipartitieMatching { graph: Vec<Vec<usize>>, left: usize, right: usize, } impl BipartitieMatching { fn new(left: usize, right: usize) -> Self { assert!(left > 0 && right > 0); BipartitieMatching { graph: vec![vec![]; left], left: left, right: right, } } fn add_edge(&mut self, a: usize, b: usize) { assert!(a < self.left && b <self.right); self.graph[a].push(b); } fn bfs(&self, used: &[bool], assign: &[Option<usize>], depth: &mut [u32]) { let mut que = std::collections::VecDeque::new(); for (v, (&used, depth)) in used.iter().zip(depth.iter_mut()).enumerate() { if !used { *depth = 0; que.push_back(v); } } while let Some(v) = que.pop_front() { let d = depth[v] + 1; for &u in self.graph[v].iter() { if let Some(k) = assign[u] { if depth[k] > d { depth[k] = d; que.push_back(k); } } } } } fn dfs(&self, v: usize, it: &mut [usize], used: &mut [bool], assign: &mut [Option<usize>], depth: &[u32]) -> bool { let d = depth[v] + 1; for (k, &u) in self.graph[v].iter().enumerate().skip(it[v]) { let ok = assign[u].map_or(true, |k| { assert!(used[k]); depth[k] == d && self.dfs(k, it, used, assign, depth) }); if ok { assign[u] = Some(v); used[v] = true; return true; } it[v] = k + 1; } false } fn solve(&self) -> Vec<(usize, usize)> { let mut used = vec![false; self.left]; let mut assign = vec![None; self.right]; let mut depth = Vec::with_capacity(self.left); let mut it = Vec::with_capacity(self.left); loop { depth.clear(); depth.resize(self.left, std::u32::MAX / 2); self.bfs(&used, &assign, &mut depth); it.clear(); it.resize(self.left, 0); let mut update = false; for v in 0..self.left { if !used[v] { update |= self.dfs(v, &mut it, &mut used, &mut assign, &depth); } } if !update { break; } } let mut ans = vec![]; for (r, a) in assign.into_iter().enumerate() { if let Some(l) = a { ans.push((l, r)); } } ans } } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::<Vec<u8>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- fn run() { input! { n: usize, e: [(usize1, usize1); n], } let mut g = BipartitieMatching::new(n, n); for (i, &(a, b)) in e.iter().enumerate() { g.add_edge(i, a); g.add_edge(i, b); } let f = g.solve(); if f.len() != n { println!("No"); return; } use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); writeln!(out, "Yes").ok(); let mut ans = vec![0; n]; for e in f { ans[e.0] = e.1 + 1; } for a in ans { writeln!(out, "{}", a).ok(); } } fn main() { run(); }