結果

問題 No.1744 Selfish Spies 1 (à la Princess' Perfectionism)
ユーザー koba-e964koba-e964
提出日時 2021-11-14 21:18:55
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 891 ms / 5,000 ms
コード長 6,918 bytes
コンパイル時間 16,695 ms
コンパイル使用メモリ 390,292 KB
実行使用メモリ 12,972 KB
最終ジャッジ日時 2024-05-07 13:09:17
合計ジャッジ時間 20,817 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,948 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 4 ms
6,944 KB
testcase_13 AC 5 ms
6,944 KB
testcase_14 AC 17 ms
6,940 KB
testcase_15 AC 24 ms
6,940 KB
testcase_16 AC 27 ms
6,940 KB
testcase_17 AC 90 ms
6,944 KB
testcase_18 AC 89 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 5 ms
6,944 KB
testcase_24 AC 107 ms
6,944 KB
testcase_25 AC 34 ms
6,940 KB
testcase_26 AC 33 ms
6,940 KB
testcase_27 AC 111 ms
6,944 KB
testcase_28 AC 856 ms
12,972 KB
testcase_29 AC 113 ms
6,944 KB
testcase_30 AC 119 ms
6,940 KB
testcase_31 AC 113 ms
6,940 KB
testcase_32 AC 118 ms
6,944 KB
testcase_33 AC 889 ms
12,828 KB
testcase_34 AC 891 ms
12,732 KB
testcase_35 AC 852 ms
12,392 KB
testcase_36 AC 867 ms
12,448 KB
testcase_37 AC 874 ms
12,400 KB
testcase_38 AC 872 ms
12,396 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `ma`
   --> src/main.rs:173:10
    |
173 |     let (ma, _cut) = din.max_flow(0, 1);
    |          ^^ help: if this is intentional, prefix it with an underscore: `_ma`
    |
    = note: `#[warn(unused_variables)]` on by default

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

/**
 * Dinic's algorithm for maximum flow problem.
 * Verified by: yukicoder No.177 (http://yukicoder.me/submissions/148371)
 * Min-cut (the second element of max_flow's returned values) is not verified.
 */

#[derive(Clone)]
struct Edge<T> {
    to: usize,
    cap: T,
    rev: usize, // rev is the position of the reverse edge in graph[to]
}

struct Dinic<T> {
    graph: Vec<Vec<Edge<T>>>,
    iter: Vec<usize>,
    zero: T,
}

impl<T> Dinic<T>
    where T: Clone,
          T: Copy,
          T: Ord,
          T: std::ops::AddAssign,
          T: std::ops::SubAssign,
{
    fn bfs(&self, s: usize, level: &mut [Option<usize>]) {
        let n = level.len();
        for i in 0 .. n {
            level[i] = None;
        }
        let mut que = std::collections::VecDeque::new();
        level[s] = Some(0);
        que.push_back(s);
        while let Some(v) = que.pop_front() {
            for e in self.graph[v].iter() {
	        if e.cap > self.zero && level[e.to] == None {
	            level[e.to] = Some(level[v].unwrap() + 1);
	            que.push_back(e.to);
                }
            }
	}
    }
    /* search augment path by dfs.
     * if f == None, f is treated as infinity.
     */
    fn dfs(&mut self, v: usize, t: usize, f: Option<T>, level: &mut [Option<usize>]) -> T {
        if v == t {
            return f.unwrap();
        }
        while self.iter[v] < self.graph[v].len() {
            let i = self.iter[v];
            let e = self.graph[v][i].clone();
            if e.cap > self.zero && level[v] < level[e.to] {
                let newf = std::cmp::min(f.unwrap_or(e.cap), e.cap);
                let d = self.dfs(e.to, t, Some(newf), level);
                if d > self.zero {
                    self.graph[v][i].cap -= d;
                    self.graph[e.to][e.rev].cap += d;
                    return d;
                }
            }
            self.iter[v] += 1;
        }
        self.zero
    }
    pub fn new(n: usize, zero: T) -> Self {
        Dinic {
            graph: vec![Vec::new(); n],
            iter: vec![0; n],
            zero: zero,
        }
    }
    pub fn add_edge(&mut self, from: usize, to: usize, cap: T) {
        let added_from = Edge { to: to, cap: cap,
                            rev: self.graph[to].len() };
        let added_to = Edge { to: from, cap: self.zero,
                            rev: self.graph[from].len() };
        self.graph[from].push(added_from);
        self.graph[to].push(added_to);
    }
    pub fn max_flow(&mut self, s: usize, t: usize) -> (T, Vec<usize>) {
        let mut flow = self.zero;
        let n = self.graph.len();
        let mut level = vec![None; n];
        loop {
            self.bfs(s, &mut level);
            if level[t] == None {
                let ret = (0 .. n).filter(|&i| level[i] == None)
                    .collect();
                return (flow, ret);
            }
            self.iter.clear();
            self.iter.resize(n, 0);
            loop {
                let f = self.dfs(s, t, None, &mut level);
                if f <= self.zero { break; }
                flow += f;
            }
        }
    }
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        n: usize, m: usize, l: usize,
        ab: [(usize1, usize1); l],
    }
    let mut din = Dinic::new(2 + n + m, 0);
    for i in 0..n {
        din.add_edge(0, 2 + i, 1);
    }
    for i in 0..m {
        din.add_edge(2 + n + i, 1, 1);
    }
    for &(a, b) in &ab {
        din.add_edge(2 + a, 2 + n + b, 1);
    }
    let (ma, _cut) = din.max_flow(0, 1);
    let mut to = vec![None; n];
    let mut used = vec![false; m];
    for i in 0..m {
        for e in &din.graph[2 + n + i] {
            if e.to >= 2 && e.cap == 1 {
                to[e.to - 2] = Some(i);
                used[i] = true;
            }
        }
    }
    // eprintln!("ma = {}, to = {:?}", ma, to);
    for i in 0..l {
        let (a, b) = ab[i];
        if to[a] != Some(b) {
            puts!("Yes\n");
            continue;
        }
        let mut g = vec![vec![]; 2 + n + m];
        for i in 0..n {
            if to[i].is_some() {
                g[2 + i].push(0);
            } else {
                g[0].push(2 + i);
            }
        }
        for i in 0..m {
            if used[i] {
                g[1].push(2 + n + i);
            } else {
                g[2 + n + i].push(1);
            }
        }
        for j in 0..l {
            if i == j { continue; }
            let (c, d) = ab[j];
            if to[c] == Some(d) {
                g[2 + n + d].push(2 + c);
            } else {
                g[2 + c].push(2 + n + d);
            }
        }
        let mut que = VecDeque::new();
        let mut vis = vec![false; 2 + n + m];
        que.push_back(2 + a);
        while let Some(v) = que.pop_front() {
            if vis[v] { continue; }
            vis[v] = true;
            for &w in &g[v] {
                if !vis[w] {
                    que.push_back(w);
                }
            }
        }
        puts!("{}\n", if vis[2 + n + b] { "Yes" } else { "No" });
    }
}
0