結果

問題 No.1640 簡単な色塗り
ユーザー akakimidoriakakimidori
提出日時 2021-09-16 07:52:12
言語 Rust
(1.77.0 + proconio)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 8,316 bytes
コンパイル時間 23,314 ms
コンパイル使用メモリ 376,628 KB
実行使用メモリ 50,304 KB
最終ジャッジ日時 2024-06-29 17:10:38
合計ジャッジ時間 29,122 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 0 ms
5,376 KB
testcase_04 AC 49 ms
33,972 KB
testcase_05 AC 46 ms
33,972 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 182 ms
21,604 KB
testcase_11 AC 142 ms
17,660 KB
testcase_12 AC 109 ms
16,000 KB
testcase_13 AC 325 ms
31,356 KB
testcase_14 AC 327 ms
31,232 KB
testcase_15 AC 72 ms
12,160 KB
testcase_16 AC 93 ms
14,448 KB
testcase_17 AC 247 ms
26,408 KB
testcase_18 AC 10 ms
5,376 KB
testcase_19 AC 108 ms
15,872 KB
testcase_20 AC 169 ms
20,704 KB
testcase_21 AC 122 ms
17,152 KB
testcase_22 AC 7 ms
5,376 KB
testcase_23 AC 202 ms
22,400 KB
testcase_24 AC 33 ms
7,672 KB
testcase_25 AC 112 ms
16,084 KB
testcase_26 AC 215 ms
24,448 KB
testcase_27 AC 66 ms
11,776 KB
testcase_28 AC 314 ms
30,028 KB
testcase_29 AC 229 ms
24,192 KB
testcase_30 AC 11 ms
7,552 KB
testcase_31 AC 113 ms
50,304 KB
testcase_32 AC 72 ms
34,628 KB
testcase_33 AC 41 ms
22,364 KB
testcase_34 AC 77 ms
36,000 KB
testcase_35 AC 41 ms
23,524 KB
testcase_36 AC 10 ms
7,168 KB
testcase_37 AC 14 ms
8,692 KB
testcase_38 AC 83 ms
37,760 KB
testcase_39 AC 33 ms
20,224 KB
testcase_40 AC 26 ms
15,872 KB
testcase_41 AC 52 ms
26,040 KB
testcase_42 AC 31 ms
19,428 KB
testcase_43 AC 44 ms
23,932 KB
testcase_44 AC 32 ms
17,776 KB
testcase_45 AC 25 ms
14,784 KB
testcase_46 AC 10 ms
7,632 KB
testcase_47 AC 8 ms
5,632 KB
testcase_48 AC 92 ms
36,588 KB
testcase_49 AC 4 ms
5,376 KB
testcase_50 AC 1 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 162 ms
46,324 KB
testcase_53 TLE -
07_evil_01.txt AC 671 ms
67,956 KB
07_evil_02.txt AC 1,218 ms
105,676 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: function `run_length_encoding` is never used
 --> src/main.rs:2:4
  |
2 | fn run_length_encoding<T: Clone + Eq>(a: &[T]) -> Vec<(T, usize)> {
  |    ^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `rand_memory` is never used
  --> src/main.rs:14:4
   |
14 | fn rand_memory() -> usize {
   |    ^^^^^^^^^^^

warning: function `rand` is never used
  --> src/main.rs:18:4
   |
18 | fn rand() -> usize {
   |    ^^^^

warning: function `shuffle` is never used
  --> src/main.rs:31:4
   |
31 | fn shuffle<T>(a: &mut [T]) {
   |    ^^^^^^^

ソースコード

diff #

// ---------- begin Run Length Encoding ----------
fn run_length_encoding<T: Clone + Eq>(a: &[T]) -> Vec<(T, usize)> {
    let mut a = a.iter().map(|a| (a.clone(), 1)).collect::<Vec<_>>();
    a.dedup_by(|a, b| {
        a.0 == b.0 && {
            b.1 += a.1;
            true
        }
    });
    a
}
// ---------- end Run Length Encoding ----------

fn rand_memory() -> usize {
    Box::into_raw(Box::new("I hope this is a random number")) as usize
}

fn rand() -> usize {
    static mut X: usize = 0;
    unsafe {
        if X == 0 {
            X = rand_memory();
        }
        X ^= X << 13;
        X ^= X >> 17;
        X ^= X << 5;
        X
    }
}

fn shuffle<T>(a: &mut [T]) {
    for i in 1..a.len() {
        let p = rand() % (i + 1);
        a.swap(i, p);
    }
}

// ---------- begin max flow (Dinic) ----------
mod maxflow {
    pub trait MaxFlowCapacity:
        Copy + Ord + std::ops::Add<Output = Self> + std::ops::Sub<Output = Self> + std::fmt::Display
    {
        fn zero() -> Self;
        fn inf() -> Self;
    }

    macro_rules! impl_primitive_integer_capacity {
        ($x:ty, $y:expr) => {
            impl MaxFlowCapacity for $x {
                fn zero() -> Self {
                    0
                }
                fn inf() -> Self {
                    $y
                }
            }
        };
    }

    impl_primitive_integer_capacity!(u32, std::u32::MAX);
    impl_primitive_integer_capacity!(u64, std::u64::MAX);
    impl_primitive_integer_capacity!(i32, std::i32::MAX);
    impl_primitive_integer_capacity!(i64, std::i64::MAX);

    #[derive(Clone)]
    struct Edge<Cap> {
        to_: u32,
        inv_: u32,
        cap_: Cap,
    }

    impl<Cap> Edge<Cap> {
        fn new(to: usize, inv: usize, cap: Cap) -> Self {
            Edge {
                to_: to as u32,
                inv_: inv as u32,
                cap_: cap,
            }
        }
        fn to(&self) -> usize {
            self.to_ as usize
        }
        fn inv(&self) -> usize {
            self.inv_ as usize
        }
    }

    impl<Cap: MaxFlowCapacity> Edge<Cap> {
        fn add(&mut self, cap: Cap) {
            self.cap_ = self.cap_ + cap;
        }
        fn sub(&mut self, cap: Cap) {
            self.cap_ = self.cap_ - cap;
        }
        fn cap(&self) -> Cap {
            self.cap_
        }
    }

    pub struct Graph<Cap> {
        graph: Vec<Vec<Edge<Cap>>>,
    }

    #[allow(dead_code)]
    pub struct EdgeIndex {
        src: usize,
        dst: usize,
        x: usize,
        y: usize,
    }

    impl<Cap: MaxFlowCapacity> Graph<Cap> {
        pub fn new(size: usize) -> Self {
            Self {
                graph: vec![vec![]; size],
            }
        }
        pub fn add_edge(&mut self, src: usize, dst: usize, cap: Cap) -> EdgeIndex {
            assert!(src.max(dst) < self.graph.len());
            assert!(cap >= Cap::zero());
            assert!(src != dst);
            let x = self.graph[src].len();
            let y = self.graph[dst].len();
            self.graph[src].push(Edge::new(dst, y, cap));
            self.graph[dst].push(Edge::new(src, x, Cap::zero()));
            EdgeIndex { src, dst, x, y }
        }
        // src, dst, used, intial_capacity
        #[allow(dead_code)]
        pub fn get_edge(&self, e: &EdgeIndex) -> (usize, usize, Cap, Cap) {
            let max = self.graph[e.src][e.x].cap() + self.graph[e.dst][e.y].cap();
            let used = self.graph[e.dst][e.y].cap();
            (e.src, e.dst, used, max)
        }
        pub fn flow(&mut self, src: usize, dst: usize) -> Cap {
            let size = self.graph.len();
            assert!(src.max(dst) < size);
            assert!(src != dst);
            let mut queue = std::collections::VecDeque::new();
            let mut level = vec![0; size];
            let mut it = vec![0; size];
            let mut ans = Cap::zero();
            for _ in 0.. {
                (|| {
                    level.clear();
                    level.resize(size, 0);
                    level[src] = 1;
                    queue.clear();
                    queue.push_back(src);
                    while let Some(v) = queue.pop_front() {
                        let d = level[v] + 1;
                        for e in self.graph[v].iter() {
                            let u = e.to();
                            if e.cap() > Cap::zero() && level[u] == 0 {
                                level[u] = d;
                                if u == dst {
                                    return;
                                }
                                queue.push_back(u);
                            }
                        }
                    }
                })();
                if level[dst] == 0 {
                    break;
                }
                it.clear();
                it.resize(size, 0);
                loop {
                    let f = self.dfs(src, dst, Cap::inf(), &mut it, &level);
                    if f == Cap::zero() {
                        break;
                    }
                    ans = ans + f;
                }
            }
            ans
        }
        fn dfs(&mut self, v: usize, dst: usize, cap: Cap, it: &mut [usize], level: &[u32]) -> Cap {
            if v == dst {
                return cap;
            }
            while let Some((u, c, inv)) = self.graph[v].get(it[v]).map(|p| (p.to(), p.cap(), p.inv())) {
                if level[u] == level[v] + 1 && cap > Cap::zero() {
                    let cap = cap.min(c);
                    let c = self.dfs(u, dst, cap, it, level);
                    if c > Cap::zero() {
                        self.graph[v][it[v]].sub(c);
                        self.graph[u][inv].add(c);
                        return c;
                    }
                }
                it[v] += 1;
            }
            Cap::zero()
        }
    }
}
// ---------- end max flow (Dinic) ----------
// ---------- 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 = maxflow::Graph::new(2 * n + 2);
    let mut edge = vec![];
    let src = 2 * n;
    let dst = 2 * n + 1;
    for (i, &(a, b)) in e.iter().enumerate() {
        let x = g.add_edge(i, a + n, 1);
        let y = g.add_edge(i, b + n, 1);
        edge.push((x, y));
    }
    for i in 0..n {
        g.add_edge(src, i, 1);
        g.add_edge(i + n, dst, 1);
    }
    let f = g.flow(src, dst);
    if f != n as u32 {
        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();
    for (x, y) in edge {
        for x in [x, y].iter() {
            let e = g.get_edge(x);
            if e.2 == 1 {
                writeln!(out, "{}", e.1 - n + 1).ok();
            }
        }
    }
}

fn main() {
    run();
}

0