結果

問題 No.2677 Minmax Independent Set
ユーザー koba-e964koba-e964
提出日時 2024-03-16 09:06:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 478 ms / 2,000 ms
コード長 5,684 bytes
コンパイル時間 2,527 ms
コンパイル使用メモリ 203,236 KB
実行使用メモリ 130,088 KB
最終ジャッジ日時 2024-03-16 09:06:38
合計ジャッジ時間 18,285 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 388 ms
91,060 KB
testcase_06 AC 381 ms
93,180 KB
testcase_07 AC 375 ms
93,216 KB
testcase_08 AC 375 ms
93,108 KB
testcase_09 AC 387 ms
95,228 KB
testcase_10 AC 318 ms
93,272 KB
testcase_11 AC 320 ms
93,272 KB
testcase_12 AC 308 ms
130,088 KB
testcase_13 AC 368 ms
105,272 KB
testcase_14 AC 356 ms
106,632 KB
testcase_15 AC 377 ms
115,204 KB
testcase_16 AC 442 ms
88,536 KB
testcase_17 AC 448 ms
88,536 KB
testcase_18 AC 271 ms
57,176 KB
testcase_19 AC 328 ms
67,180 KB
testcase_20 AC 96 ms
24,960 KB
testcase_21 AC 365 ms
74,276 KB
testcase_22 AC 21 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 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 294 ms
94,096 KB
testcase_29 AC 382 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 1 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 2 ms
6,676 KB
testcase_40 AC 5 ms
6,676 KB
testcase_41 AC 9 ms
6,676 KB
testcase_42 AC 19 ms
7,168 KB
testcase_43 AC 39 ms
12,416 KB
testcase_44 AC 89 ms
22,912 KB
testcase_45 AC 201 ms
43,776 KB
testcase_46 AC 445 ms
85,664 KB
testcase_47 AC 478 ms
86,956 KB
testcase_48 AC 451 ms
86,956 KB
testcase_49 AC 457 ms
87,080 KB
testcase_50 AC 107 ms
36,864 KB
testcase_51 AC 8 ms
6,676 KB
testcase_52 AC 266 ms
78,388 KB
testcase_53 AC 237 ms
72,316 KB
testcase_54 AC 7 ms
6,676 KB
testcase_55 AC 387 ms
94,132 KB
testcase_56 AC 385 ms
94,148 KB
testcase_57 AC 381 ms
94,136 KB
testcase_58 AC 386 ms
94,128 KB
testcase_59 AC 380 ms
94,132 KB
testcase_60 AC 191 ms
87,516 KB
testcase_61 AC 238 ms
88,276 KB
testcase_62 AC 207 ms
95,224 KB
testcase_63 AC 244 ms
114,708 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `acc` is never read
  --> Main.rs:45:5
   |
44 | struct MonoidalReroot<M: Monoid> {
   |        -------------- field in this struct
45 |     acc: Vec<Vec<M::E>>,
   |     ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

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

trait Monoid {
    type V;
    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> {
    acc: Vec<Vec<M::E>>,
    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()));
        }
    }
    #[inline]
    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