結果

問題 No.1123 Afforestation
ユーザー akakimidoriakakimidori
提出日時 2020-07-22 23:34:07
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 5,175 bytes
コンパイル時間 1,565 ms
コンパイル使用メモリ 156,036 KB
実行使用メモリ 257,712 KB
最終ジャッジ日時 2023-09-05 04:59:12
合計ジャッジ時間 27,772 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 21 ms
9,736 KB
testcase_05 AC 393 ms
66,664 KB
testcase_06 AC 861 ms
257,484 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 10 ms
5,168 KB
testcase_12 AC 200 ms
32,504 KB
testcase_13 AC 903 ms
257,632 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 25 ms
10,380 KB
testcase_20 AC 524 ms
66,028 KB
testcase_21 AC 912 ms
257,220 KB
testcase_22 AC 909 ms
257,232 KB
testcase_23 AC 897 ms
257,604 KB
testcase_24 AC 914 ms
257,648 KB
testcase_25 AC 917 ms
257,600 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 TLE -
testcase_29 WA -
testcase_30 AC 2 ms
4,376 KB
testcase_31 TLE -
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 8 ms
5,092 KB
testcase_43 AC 19 ms
10,192 KB
testcase_44 AC 40 ms
16,172 KB
testcase_45 AC 42 ms
16,120 KB
testcase_46 AC 52 ms
18,028 KB
testcase_47 AC 51 ms
18,024 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 3 ms
4,376 KB
testcase_56 AC 3 ms
4,376 KB
testcase_57 AC 3 ms
4,376 KB
testcase_58 AC 8 ms
4,912 KB
testcase_59 AC 19 ms
10,060 KB
testcase_60 AC 44 ms
16,188 KB
testcase_61 AC 41 ms
16,204 KB
testcase_62 AC 52 ms
18,020 KB
testcase_63 AC 52 ms
18,016 KB
testcase_64 AC 1 ms
4,376 KB
testcase_65 AC 1 ms
4,380 KB
testcase_66 AC 2 ms
4,380 KB
testcase_67 AC 2 ms
4,376 KB
testcase_68 AC 3 ms
4,380 KB
testcase_69 AC 5 ms
4,380 KB
testcase_70 AC 8 ms
4,916 KB
testcase_71 AC 20 ms
10,368 KB
testcase_72 AC 1 ms
4,376 KB
testcase_73 AC 1 ms
4,380 KB
testcase_74 AC 1 ms
4,376 KB
testcase_75 AC 1 ms
4,376 KB
testcase_76 AC 1 ms
4,376 KB
testcase_77 AC 1 ms
4,380 KB
testcase_78 AC 1 ms
4,380 KB
testcase_79 WA -
testcase_80 AC 1 ms
4,376 KB
testcase_81 AC 1 ms
4,380 KB
testcase_82 AC 1 ms
4,376 KB
testcase_83 AC 1 ms
4,376 KB
testcase_84 AC 924 ms
257,400 KB
testcase_85 AC 976 ms
257,556 KB
testcase_86 AC 1 ms
4,376 KB
testcase_87 AC 1 ms
4,384 KB
testcase_88 AC 361 ms
257,476 KB
testcase_89 AC 425 ms
257,696 KB
testcase_90 AC 1 ms
4,380 KB
testcase_91 WA -
testcase_92 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

type F = i32;
pub const INF: F = 4_000_000;
const ZERO: F = 0;
fn non_zero(f: F) -> bool {
    f > ZERO
}
fn min(a: F, b: F) -> F {
    if a < b {
        a
    } else {
        b
    }
}
pub struct Graph {
    n: usize,
    depth: Vec<usize>,
    it: Vec<usize>,
    g: Vec<Vec<(usize, F, usize)>>,
}

impl Graph {
    pub fn new(n: usize) -> Graph {
        Graph {
            n: n,
            depth: vec![n + 1; n],
            it: vec![0; n],
            g: vec![vec![]; n],
        }
    }
    pub fn add_edge(&mut self, s: usize, t: usize, f: F) -> usize {
        let x = self.g[s].len();
        let y = self.g[t].len();
        let k = self.g[s].len();
        self.g[s].push((t, f, y));
        self.g[t].push((s, ZERO, x));
        k
    }
    fn bfs(&mut self, src: usize, dst: usize) {
        let g = &self.g;
        let n = self.n;
        let dp = &mut self.depth;
        dp.clear();
        dp.resize(n, n + 1);
        dp[src] = 0;
        let mut q = std::collections::VecDeque::new();
        q.push_back(src);
        'outer: while let Some(v) = q.pop_front() {
            for &(u, f, _) in g[v].iter() {
                if dp[u] > dp[v] + 1 && non_zero(f) {
                    dp[u] = dp[v] + 1;
                    if u == dst {
                        break 'outer;
                    }
                    q.push_back(u);
                }
            }
        }
    }
    fn dfs(&mut self, v: usize, src: usize, f: F) -> F {
        if v == src {
            return f;
        }
        while self.it[v] < self.g[v].len() {
            let (u, _, inv) = self.g[v][self.it[v]];
            if self.depth[u] < self.depth[v] && non_zero(self.g[u][inv].1) {
                let capa = min(f, self.g[u][inv].1);
                let c = self.dfs(u, src, capa);
                if non_zero(c) {
                    self.g[v][self.it[v]].1 += c;
                    self.g[u][inv].1 -= c;
                    return c;
                }
            }
            self.it[v] += 1;
        }
        ZERO
    }
    pub fn flow(&mut self, src: usize, dst: usize) -> F {
        let mut ans = ZERO;
        loop {
            self.bfs(src, dst);
            if self.depth[dst] > self.n {
                break;
            }
            self.it.clear();
            self.it.resize(self.n, 0);
            loop {
                let f = self.dfs(dst, src, INF);
                if non_zero(f) {
                    ans += f;
                } else {
                    break;
                }
            }
        }
        ans
    }
}

//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")
    };
}

//

fn run() {
    input! {
        h: usize,
        w: usize,
        a: [i32; h],
        b: [i32; w],
        k: usize,
        p: [(usize1, usize1); k],
    }
    let mut p = p;
    p.sort();
    let mut solver = Graph::new(h + w + 2);
    let src = h + w + 1;
    let dst = h + w;
    for (i, a) in a.iter().enumerate() {
        solver.add_edge(src, i, *a);
    }
    for (j, b) in b.iter().enumerate() {
        solver.add_edge(j + h, dst, *b);
    }
    let mut index = vec![vec![None; w]; h];
    for i in 0..h {
        for j in 0..w {
            if p.binary_search(&(i, j)).is_err() {
                let k = solver.add_edge(i, j + h, 1);
                index[i][j] = Some(k);
            }
        }
    }
    if solver.flow(src, dst) < a.iter().sum::<i32>() {
        println!(":(");
        return;
    }
    println!("Yay!");
    for i in 0..h {
        let mut s = String::new();
        for j in 0..w {
            if p.binary_search(&(i, j)).is_ok() {
                s.push('x');
                continue;
            }
            let k = index[i][j].unwrap();
            if solver.g[i][k].1 == 0 {
                s.push('o');
            } else {
                s.push('.');
            }
        }
        println!("{}", s);
    }
}

fn main() {
    run();
}
0