結果

問題 No.1911 Alternately Coloring
ユーザー akakimidoriakakimidori
提出日時 2022-04-22 22:03:18
言語 Rust
(1.77.0)
結果
AC  
実行時間 325 ms / 4,000 ms
コード長 5,319 bytes
コンパイル時間 4,825 ms
コンパイル使用メモリ 158,860 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 08:32:06
合計ジャッジ時間 11,139 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 263 ms
4,380 KB
testcase_07 AC 291 ms
4,376 KB
testcase_08 AC 238 ms
4,380 KB
testcase_09 AC 325 ms
4,376 KB
testcase_10 AC 298 ms
4,376 KB
testcase_11 AC 275 ms
4,380 KB
testcase_12 AC 278 ms
4,376 KB
testcase_13 AC 215 ms
4,376 KB
testcase_14 AC 198 ms
4,380 KB
testcase_15 AC 276 ms
4,380 KB
testcase_16 AC 276 ms
4,380 KB
testcase_17 AC 270 ms
4,380 KB
testcase_18 AC 195 ms
4,376 KB
testcase_19 AC 217 ms
4,376 KB
testcase_20 AC 248 ms
4,376 KB
testcase_21 AC 81 ms
4,380 KB
testcase_22 AC 302 ms
4,376 KB
testcase_23 AC 269 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 264 ms
4,380 KB
testcase_28 AC 77 ms
4,376 KB
testcase_29 AC 217 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn run() {
    input! {
        n: usize,
        m: usize,
        e: [(usize1, usize1); m],
        d: [[i64; n]; 2],
    }
    let mut dsu = QuickFind::new(2 * n);
    for &(a, b) in e.iter() {
        dsu.unite(a, b + n);
        dsu.unite(b, a + n);
    }
    if (0..n).all(|x| !dsu.same(x, x + n)) {
        let mut s = 0;
        for &v in dsu.enumerate(0) {
            if v >= n {
                s += d[1][v - n];
            } else {
                s += d[0][v];
            }
        }
        let sum = d.iter().flatten().sum::<i64>();
        let ans = s.max(sum - s);
        println!("{}", ans);
        return;
    }
    let mut g = vec![vec![]; n];
    let mut ans = 0;
    let mut d = d;
    let all = (0..n).map(|x| d[0][x].max(d[1][x])).sum::<i64>();
    for _ in 0..2 {
        for (i, &(a, b)) in e.iter().enumerate() {
            g.iter_mut().for_each(|g| g.clear());
            for (j, &(a, b)) in e.iter().enumerate() {
                if i != j {
                    g[a].push(b);
                    g[b].push(a);
                }
            }
            let mut dp = vec![vec![std::i64::MAX / 2; n]; 2];
            dp[0][a] = d[0][a].max(d[1][a]) - d[0][a];
            let mut h = std::collections::BinaryHeap::new();
            h.push((-dp[0][a], 0, a));
            while let Some((dis, p, v)) = h.pop() {
                let dis = -dis;
                if dis > dp[p][v] {
                    continue;
                }
                for &u in g[v].iter() {
                    let cost = d[0][u].max(d[1][u]) - d[p ^ 1][u];
                    assert!(cost >= 0);
                    let dis = dis + cost;
                    if dp[p ^ 1][u].chmin(dis) {
                        h.push((-dis, p ^ 1, u));
                    }
                }
            }
            ans = ans.max(all - dp[0][b]);
        }
        d.swap(0, 1);
    }
    println!("{}", ans);
}

fn main() {
    run();
}

// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
// ---------- begin quick find ----------
pub struct QuickFind {
    size: usize,
    id: Vec<usize>,
    list: Vec<Vec<usize>>,
}

impl QuickFind {
    pub fn new(size: usize) -> Self {
        let id = (0..size).collect::<Vec<_>>();
        let list = (0..size).map(|x| vec![x]).collect::<Vec<_>>();
        QuickFind { size, id, list }
    }
    pub fn root(&self, x: usize) -> usize {
        assert!(x < self.size);
        self.id[x]
    }
    pub fn same(&self, x: usize, y: usize) -> bool {
        assert!(x < self.size);
        assert!(y < self.size);
        self.root(x) == self.root(y)
    }
    pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
        assert!(x < self.size);
        assert!(y < self.size);
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y {
            return None;
        }
        if (self.list[x].len(), x) < (self.list[y].len(), y) {
            std::mem::swap(&mut x, &mut y);
        }
        let mut z = std::mem::take(&mut self.list[y]);
        z.iter().for_each(|y| self.id[*y] = x);
        self.list[x].append(&mut z);
        Some((x, y))
    }
    pub fn enumerate(&self, x: usize) -> &[usize] {
        assert!(x < self.size);
        &self.list[self.root(x)]
    }
    pub fn size(&self, x: usize) -> usize {
        assert!(x < self.size);
        self.enumerate(x).len()
    }
}
// ---------- end quick find ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
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_export]
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_export]
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 ----------
0