結果

問題 No.1640 簡単な色塗り
ユーザー akakimidoriakakimidori
提出日時 2021-08-06 23:25:54
言語 Rust
(1.77.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 4,741 bytes
コンパイル時間 1,505 ms
コンパイル使用メモリ 155,960 KB
実行使用メモリ 26,212 KB
最終ジャッジ日時 2023-09-12 03:18:57
合計ジャッジ時間 12,299 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 33 ms
17,936 KB
testcase_05 AC 32 ms
17,964 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 45 ms
11,064 KB
testcase_11 AC 33 ms
9,496 KB
testcase_12 AC 28 ms
8,488 KB
testcase_13 AC 87 ms
16,520 KB
testcase_14 AC 87 ms
15,664 KB
testcase_15 AC 18 ms
6,548 KB
testcase_16 AC 23 ms
7,868 KB
testcase_17 AC 66 ms
13,600 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 27 ms
8,476 KB
testcase_20 AC 46 ms
10,688 KB
testcase_21 AC 31 ms
9,516 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 48 ms
11,356 KB
testcase_24 AC 10 ms
4,504 KB
testcase_25 AC 28 ms
8,408 KB
testcase_26 AC 54 ms
12,656 KB
testcase_27 AC 17 ms
6,144 KB
testcase_28 AC 79 ms
15,052 KB
testcase_29 AC 56 ms
12,604 KB
testcase_30 AC 7 ms
4,552 KB
testcase_31 AC 81 ms
26,212 KB
testcase_32 AC 82 ms
20,664 KB
testcase_33 AC 58 ms
15,320 KB
testcase_34 AC 70 ms
18,600 KB
testcase_35 AC 25 ms
12,392 KB
testcase_36 AC 9 ms
5,092 KB
testcase_37 AC 13 ms
6,904 KB
testcase_38 AC 60 ms
19,044 KB
testcase_39 AC 21 ms
10,848 KB
testcase_40 AC 15 ms
8,756 KB
testcase_41 AC 59 ms
17,324 KB
testcase_42 AC 18 ms
10,404 KB
testcase_43 AC 40 ms
12,908 KB
testcase_44 AC 42 ms
12,188 KB
testcase_45 AC 25 ms
10,456 KB
testcase_46 AC 6 ms
4,656 KB
testcase_47 AC 6 ms
4,568 KB
testcase_48 AC 120 ms
24,232 KB
testcase_49 AC 2 ms
4,384 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 1 ms
4,380 KB
testcase_52 AC 96 ms
24,088 KB
testcase_53 AC 228 ms
20,344 KB
07_evil_01.txt AC 190 ms
34,120 KB
07_evil_02.txt AC 308 ms
49,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
}
0