結果

問題 No.828 全方位神童数
ユーザー koba-e964koba-e964
提出日時 2022-01-03 00:37:25
言語 Rust
(1.77.0)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 5,332 bytes
コンパイル時間 2,044 ms
コンパイル使用メモリ 178,816 KB
実行使用メモリ 49,240 KB
最終ジャッジ日時 2024-04-20 10:05:06
合計ジャッジ時間 11,102 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 11 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 9 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 7 ms
6,944 KB
testcase_11 AC 2 ms
6,948 KB
testcase_12 AC 8 ms
6,940 KB
testcase_13 AC 92 ms
21,376 KB
testcase_14 AC 231 ms
40,472 KB
testcase_15 AC 66 ms
16,256 KB
testcase_16 AC 97 ms
21,888 KB
testcase_17 AC 73 ms
17,792 KB
testcase_18 AC 140 ms
30,352 KB
testcase_19 AC 290 ms
49,068 KB
testcase_20 AC 186 ms
34,988 KB
testcase_21 AC 252 ms
43,268 KB
testcase_22 AC 43 ms
12,160 KB
testcase_23 AC 12 ms
6,944 KB
testcase_24 AC 123 ms
26,424 KB
testcase_25 AC 43 ms
12,032 KB
testcase_26 AC 258 ms
45,388 KB
testcase_27 AC 207 ms
39,452 KB
testcase_28 AC 272 ms
45,460 KB
testcase_29 AC 260 ms
43,924 KB
testcase_30 AC 122 ms
26,496 KB
testcase_31 AC 121 ms
25,736 KB
testcase_32 AC 254 ms
45,368 KB
testcase_33 AC 289 ms
49,240 KB
testcase_34 AC 208 ms
39,848 KB
testcase_35 AC 149 ms
27,244 KB
testcase_36 AC 172 ms
33,608 KB
testcase_37 AC 105 ms
22,912 KB
testcase_38 AC 147 ms
30,408 KB
testcase_39 AC 266 ms
44,820 KB
testcase_40 AC 114 ms
23,236 KB
testcase_41 AC 90 ms
19,612 KB
testcase_42 AC 310 ms
48,880 KB
testcase_43 AC 113 ms
39,268 KB
testcase_44 AC 45 ms
16,384 KB
testcase_45 AC 67 ms
23,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
// 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"));
}

// Quick-Find data structure.
// Verified by: https://atcoder.jp/contests/cf17-tournament-round3-open/submissions/22928265
// Verified by: https://atcoder.jp/contests/ttpc2019/submissions/23384553 (polymorphic version)
struct QuickFind<T = ()> {
    root: Vec<usize>, mem: Vec<Vec<usize>>,
    dat: Vec<T>, default: T,
}

impl QuickFind<()> {
    #[allow(unused)]
    fn new(n: usize) -> Self {
        Self::with_dat(n, ())
    }
    #[allow(unused)]
    fn unite(&mut self, x: usize, y: usize) {
        self.unite_with_hooks(x, y, |&(), _| (), |(), ()| ());
    }
}
impl<T: Clone> QuickFind<T> {
    fn with_dat(n: usize, def: T) -> Self {
        let root = (0..n).collect();
        let mut mem = vec![vec![]; n];
        for i in 0..n {
            mem[i] = vec![i];
        }
        QuickFind { root: root, mem: mem, dat: vec![def.clone(); n], default: def }
    }
    fn root(&self, x: usize) -> usize {
        self.root[x]
    }
    #[allow(unused)]
    fn set(&mut self, idx: usize, val: T) {
        self.apply(idx, move |me| *me = val);
    }
    #[allow(unused)]
    fn get(&mut self, idx: usize) -> T {
        let mut ans = self.default.clone();
        self.apply(idx, |me| ans = me.clone());
        ans
    }
    fn apply<F: FnOnce(&mut T)>(&mut self, idx: usize, f: F) {
        let r = self.root[idx];
        f(&mut self.dat[r]);
    }
    // unite always merges y to x if |x| >= |y|.
    fn unite_with_hooks<F: FnMut(&T, i64), G: FnMut(T, T) -> T>(
        &mut self, x: usize, y: usize,
        mut hook: F, mut merge: G) {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y { return }
        if self.mem[x].len() < self.mem[y].len() {
            std::mem::swap(&mut x, &mut y);
        }
        let memy = std::mem::replace(&mut self.mem[y], vec![]);
        for &v in &memy {
            self.root[v] = x;
        }
        self.mem[x].extend_from_slice(&memy);
        // hook
        hook(&self.dat[x], -1);
        hook(&self.dat[y], -1);
        self.dat[x] = merge(
            std::mem::replace(&mut self.dat[x], self.default.clone()),
            std::mem::replace(&mut self.dat[y], self.default.clone()),
        );
        hook(&self.dat[x], 1);
    }
    #[allow(unused)]
    fn is_same_set(&self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    #[allow(unused)]
    fn size(&self, x: usize) -> usize {
        let x = self.root(x);
        self.mem[x].len()
    }
}

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() {
    input! {
        n: usize,
        r: [i64; n],
        ab: [(usize1, usize1); n - 1],
    }
    let mut g = vec![vec![]; n];
    for &(a, b) in &ab {
        if a < b {
            g[b].push(a);
        } else {
            g[a].push(b);
        }
    }
    let mut qf = QuickFind::with_dat(n, (vec![1], 0));
    for i in 0..n {
        qf.set(i, (vec![i], 0));
    }
    let mut ans = vec![0; n];
    for i in 0..n {
        for &w in &g[i] {
            qf.unite_with_hooks(i, w,
                                |&(_, _), _coef| (),
                                |(mut t1, x1), (mut t2, x2)| {
                                    for &v in &t2 {
                                        ans[v] += x2 - x1;
                                    }
                                    t1.append(&mut t2);
                                    (t1, x1)
                                });
        }
        qf.apply(i, |elem| elem.1 += 1);
    }
    let bias = qf.get(0).1;
    let mut out = 1;
    for i in 0..n {
        let sol = ans[i] + bias;
        out = out * (sol + r[i]) % 1_000_000_007;
        if n < 10 {
            eprintln!("{}", sol);
        }
    }
    println!("{}", out);
}
0