結果

問題 No.2677 Minmax Independent Set
ユーザー koba-e964koba-e964
提出日時 2024-03-16 09:02:28
言語 Rust
(1.77.0)
結果
AC  
実行時間 505 ms / 2,000 ms
コード長 5,988 bytes
コンパイル時間 10,270 ms
コンパイル使用メモリ 179,184 KB
実行使用メモリ 130,088 KB
最終ジャッジ日時 2024-03-16 09:02:56
合計ジャッジ時間 27,702 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 423 ms
91,060 KB
testcase_06 AC 455 ms
93,180 KB
testcase_07 AC 418 ms
93,216 KB
testcase_08 AC 405 ms
93,108 KB
testcase_09 AC 424 ms
95,228 KB
testcase_10 AC 364 ms
93,272 KB
testcase_11 AC 369 ms
93,272 KB
testcase_12 AC 319 ms
130,088 KB
testcase_13 AC 408 ms
105,272 KB
testcase_14 AC 421 ms
106,632 KB
testcase_15 AC 410 ms
115,204 KB
testcase_16 AC 481 ms
88,536 KB
testcase_17 AC 505 ms
88,536 KB
testcase_18 AC 284 ms
57,176 KB
testcase_19 AC 339 ms
67,180 KB
testcase_20 AC 99 ms
24,960 KB
testcase_21 AC 454 ms
74,276 KB
testcase_22 AC 24 ms
8,320 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 322 ms
94,096 KB
testcase_29 AC 419 ms
115,672 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 1 ms
6,676 KB
testcase_33 AC 1 ms
6,676 KB
testcase_34 AC 1 ms
6,676 KB
testcase_35 AC 5 ms
6,676 KB
testcase_36 AC 1 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
testcase_39 AC 3 ms
6,676 KB
testcase_40 AC 5 ms
6,676 KB
testcase_41 AC 10 ms
6,676 KB
testcase_42 AC 19 ms
7,168 KB
testcase_43 AC 43 ms
12,416 KB
testcase_44 AC 95 ms
22,912 KB
testcase_45 AC 221 ms
43,776 KB
testcase_46 AC 485 ms
85,664 KB
testcase_47 AC 488 ms
86,956 KB
testcase_48 AC 479 ms
86,956 KB
testcase_49 AC 483 ms
87,080 KB
testcase_50 AC 115 ms
36,864 KB
testcase_51 AC 8 ms
6,676 KB
testcase_52 AC 265 ms
78,388 KB
testcase_53 AC 245 ms
72,316 KB
testcase_54 AC 7 ms
6,676 KB
testcase_55 AC 437 ms
94,132 KB
testcase_56 AC 424 ms
94,148 KB
testcase_57 AC 397 ms
94,136 KB
testcase_58 AC 410 ms
94,128 KB
testcase_59 AC 433 ms
94,132 KB
testcase_60 AC 195 ms
87,516 KB
testcase_61 AC 242 ms
88,276 KB
testcase_62 AC 210 ms
95,224 KB
testcase_63 AC 246 ms
114,708 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `BufWriter`
 --> Main.rs:5:22
  |
5 | use std::io::{Write, BufWriter};
  |                      ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `Write`
 --> Main.rs:5:15
  |
5 | use std::io::{Write, BufWriter};
  |               ^^^^^

warning: 2 warnings emitted

ソースコード

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, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Monoid {
    type V: Clone;
    type E: Clone;
    fn add(a: &Self::E, b: &Self::E) -> Self::E;
    fn e() -> Self::E;
    fn put_edge(a: &Self::V, eidx: EIdx) -> Self::E;
    fn put_vertex(a: &Self::E, vidx: usize) -> Self::V;
}

// Ported from https://trap.jp/post/1702/
struct MonoidalReroot<M: Monoid> {
    #[allow(unused)]
    pub acc: Vec<Vec<M::E>>,
    #[allow(unused)]
    pub acc_rev: Vec<Vec<M::E>>,
}
type EIdx = usize;

impl<M: Monoid> MonoidalReroot<M> {
    pub fn new(g: &[Vec<(usize, EIdx)>]) -> Self {
        let n = g.len();
        let mut acc = vec![vec![]; n];
        let mut acc_rev = vec![vec![]; n];
        let mut ch = vec![vec![]; n];
        for i in 0..n {
            acc[i] = vec![M::e(); g[i].len() + 1];
            acc_rev[i] = vec![M::e(); g[i].len() + 1];
        }
        Self::dfs1(0, n, &g, &mut ch);
        for i in 0..n {
            let l = ch[i].len();
            ch[i].sort_unstable_by_key(|&(eidx, _)| eidx);
            for j in 0..l {
                let val = M::add(&acc[i][j], &ch[i][j].1);
                acc[i][j + 1] = val;
            }
            for j in (0..l).rev() {
                let val = M::add(&acc_rev[i][j + 1], &ch[i][j].1);
                acc_rev[i][j] = val;
            }
        }
        Self::dfs2(0, n, &g, &mut ch, &acc, &acc_rev, M::e());
        for i in 0..n {
            let l = ch[i].len();
            ch[i].sort_unstable_by_key(|&(eidx, _)| eidx);
            for j in 0..l {
                let val = M::add(&acc[i][j], &ch[i][j].1);
                acc[i][j + 1] = val;
            }
            for j in (0..l).rev() {
                let val = M::add(&acc_rev[i][j + 1], &ch[i][j].1);
                acc_rev[i][j] = val;
            }
        }
        MonoidalReroot {
            acc: acc,
            acc_rev: acc_rev,
        }
    }
    fn dfs1(
        v: usize, par: usize, g: &[Vec<(usize, EIdx)>],
        ch: &mut [Vec<(EIdx, M::E)>],
    ) -> M::V {
        let mut me = M::e();
        for i in 0..g[v].len() {
            let (w, eidx) = g[v][i];
            if w == par { continue; }
            let chval = Self::dfs1(w, v, g, ch);
            let chval = M::put_edge(&chval, eidx);
            ch[v].push((eidx, chval.clone()));
            me = M::add(&me, &chval);
        }
        M::put_vertex(&me, v)
    }
    fn dfs2(
        v: usize, par: usize, g: &[Vec<(usize, EIdx)>],
        ch: &mut [Vec<(EIdx, M::E)>],
        acc: &[Vec<M::E>],
        acc_rev: &[Vec<M::E>],
        passed: M::E,
    ) {
        let mut parenteidx = None;
        for i in 0..g[v].len() {
            let (w, eidx) = g[v][i];
            if w == par {
                parenteidx = Some(eidx);
                continue;
            }
            let j = ch[v].binary_search_by_key(&eidx, |&(eidx, _)| eidx).unwrap();
            let inherited = M::add(&acc[v][j], &acc_rev[v][j + 1]);
            let inherited = if par >= g.len() {
                inherited
            } else {
                M::add(&inherited, &passed)
            };
            let inherited = M::put_vertex(&inherited, v);
            let inherited = M::put_edge(&inherited, eidx);
            Self::dfs2(w, v, g, ch, acc, acc_rev, inherited);
        }
        if let Some(eidx) = parenteidx {
            ch[v].push((eidx, passed.clone()));
        }
    }
    pub fn query(&self, v: usize) -> M::V {
        M::put_vertex(&self.acc_rev[v][0], v)
    }
}

enum IndepMonoid {}

impl Monoid for IndepMonoid {
    type V = [i64; 2];
    type E = [i64; 2];
    fn add(a: &Self::E, b: &Self::E) -> Self::E {
        [a[0] + b[0], a[1] + b[1]]
    }
    fn e() -> Self::E {
        [0, 0]
    }
    fn put_edge(&a: &Self::V, _eidx: EIdx) -> Self::E {
        [a[0], std::cmp::max(a[0], a[1])]
    }
    fn put_vertex(a: &Self::E, _vidx: usize) -> Self::V {
        [a[1], a[0] + 1]
    }
}

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

// Tags: rerooting
fn solve() {
    input! {
        n: usize,
        uv: [(usize1, usize1); n - 1],
    }
    let mut g = vec![vec![]; n];
    for i in 0..n - 1 {
        let (u, v) = uv[i];
        g[u].push((v, i));
        g[v].push((u, i + n));
    }
    let rr = MonoidalReroot::<IndepMonoid>::new(&g);
    let mut ans = rr.query(0)[1];
    for i in 1..n {
        ans = std::cmp::min(ans, rr.query(i)[1]);
    }
    println!("{}", ans);
}
0