結果

問題 No.1333 Squared Sum
ユーザー nebocconebocco
提出日時 2021-02-14 15:51:13
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 301 ms / 2,000 ms
コード長 17,690 bytes
コンパイル時間 12,347 ms
コンパイル使用メモリ 389,956 KB
実行使用メモリ 54,976 KB
最終ジャッジ日時 2024-07-22 00:35:56
合計ジャッジ時間 20,997 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 301 ms
50,680 KB
testcase_04 AC 209 ms
50,580 KB
testcase_05 AC 207 ms
50,628 KB
testcase_06 AC 240 ms
50,620 KB
testcase_07 AC 240 ms
50,580 KB
testcase_08 AC 175 ms
50,668 KB
testcase_09 AC 171 ms
50,720 KB
testcase_10 AC 161 ms
50,660 KB
testcase_11 AC 173 ms
50,612 KB
testcase_12 AC 167 ms
50,656 KB
testcase_13 AC 121 ms
50,276 KB
testcase_14 AC 213 ms
50,376 KB
testcase_15 AC 167 ms
50,232 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 1 ms
6,944 KB
testcase_26 AC 202 ms
49,916 KB
testcase_27 AC 192 ms
50,064 KB
testcase_28 AC 171 ms
50,104 KB
testcase_29 AC 134 ms
50,112 KB
testcase_30 AC 76 ms
22,144 KB
testcase_31 AC 22 ms
13,440 KB
testcase_32 AC 96 ms
31,232 KB
testcase_33 AC 59 ms
25,092 KB
testcase_34 AC 117 ms
41,216 KB
testcase_35 AC 112 ms
31,256 KB
testcase_36 AC 44 ms
19,584 KB
testcase_37 AC 45 ms
20,224 KB
testcase_38 AC 88 ms
23,040 KB
testcase_39 AC 91 ms
35,428 KB
testcase_40 AC 136 ms
54,976 KB
testcase_41 AC 129 ms
54,708 KB
testcase_42 AC 177 ms
54,932 KB
testcase_43 AC 168 ms
54,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut io = IO::new();
    let n = io.scan();
    if n == 1 {
        io.println(0);
        return;
    }
    let ed: Vec<(usize, usize, i64)> = io.scan_vec(n-1);
	type Fp = F1000000007;
	let mut g = UndirectedGraph::new(n);
	for &(u, v, w) in &ed {
		g.add_edge(u - 1, v - 1, w);
	}
    let root = (0..n).filter(|&i| g.edges_from(i).count() == 1).next().unwrap();
	let (dist, par, euler, size) = tree_dfs(&g, root);
    let dist = dist.into_iter().map(|x| Fp::new(x)).collect::<Vec<Fp>>();
    let size = size.into_iter().map(|x| Fp::new(x as i64)).collect::<Vec<Fp>>();
    let score = (0..n).map(|v| if par[v].is_none() {
        Fp::zero()
    } else {
        dist[v] - dist[par[v].unwrap()]
    }).collect::<Vec<Fp>>();

    let fpn = Fp::new(n as i64);
	let mut tsum = vec![Fp::zero(); n];
	let mut bsum = vec![Fp::zero(); n];
	for &v in euler.iter().skip(2).rev() {
        let u = par[v].unwrap();
		bsum[u] = bsum[u] + bsum[v] + score[v] * size[v];
	}
    for &v in euler.iter().skip(2) {
        let u = par[v].unwrap();
        tsum[v] = tsum[u] + score[u] * (fpn - size[u]) + bsum[u] - bsum[v] - score[v] * size[v];
    }

    let mut ans = Fp::zero();
    for &v in euler.iter().skip(1) {
        ans += score[v] * score[v] * size[v] * (fpn - size[v]) +
            tsum[v] * score[v] * size[v] + bsum[v] * score[v] * (fpn - size[v]);
    }
    println!("{}", ans);
}

pub fn tree_dfs<C: Cost, G: Graph<C>>(g: &G, root: usize)
-> (Vec<C>, Vec<Option<usize>>, Vec<usize>, Vec<usize>)
{
    let n = g.size();
    let mut euler = Vec::with_capacity(n);
	let mut dist = vec![C::MAX; n];
	dist[root] = C::zero();
    let mut par = vec![None; n];
	let mut size = vec![1; n];
    let mut q = vec![root];
    while let Some(v) = q.pop() {
        euler.push(v);
        for e in g.edges_from(v) {
            if par[v] == Some(e.to) { continue; }
            par[e.to] = Some(v);
            dist[e.to] = dist[v] + e.cost;
            q.push(e.to);
        }
    }
    for &i in euler.iter().skip(1).rev() {
        size[par[i].unwrap()] += size[i];
    }
    (dist, par, euler, size)
}

// ------------ fp start ------------

use std::{
    fmt::{Debug, Display},
    hash::Hash,
    iter,
    marker::PhantomData,
};

// NOTE: `crate::` がないとうまく展開できません。
crate::define_fp!(pub F998244353, Mod998244353, 998244353);
crate::define_fp!(pub F1000000007, Mod1000000007, 1000000007);

#[derive(Clone, PartialEq, Copy, Eq, Hash)]
pub struct Fp<T>(i64, PhantomData<T>);
pub trait Mod: Debug + Clone + PartialEq + Copy + Eq + Hash {
    const MOD: i64;
}
impl<T: Mod> Fp<T> {
    pub fn new(mut x: i64) -> Self {
        x %= T::MOD;
        Self::unchecked(if x < 0 { x + T::MOD } else { x })
    }
    pub fn into_inner(self) -> i64 {
        self.0
    }
    pub fn r#mod() -> i64 {
        T::MOD
    }
    pub fn inv(self) -> Self {
        assert_ne!(self.0, 0, "Zero division");
        let (sign, x) = if self.0 * 2 < T::MOD {
            (1, self.0)
        } else {
            (-1, T::MOD - self.0)
        };
        let (g, _a, b) = ext_gcd(T::MOD, x);
        let ans = sign * b;
        assert_eq!(g, 1);
        Self::unchecked(if ans < 0 { ans + T::MOD } else { ans })
    }
    pub fn frac(x: i64, y: i64) -> Self {
        Fp::new(x) / Fp::new(y)
    }
    pub fn pow(mut self, mut p: u64) -> Self {
        let mut ans = Fp::new(1);
        while p != 0 {
            if p & 1 == 1 {
                ans *= self;
            }
            self *= self;
            p >>= 1;
        }
        ans
    }
    fn unchecked(x: i64) -> Self {
        Self(x, PhantomData)
    }
}
impl<T: Mod> iter::Sum<Fp<T>> for Fp<T> {
    fn sum<I>(iter: I) -> Self
    where
        I: iter::Iterator<Item = Fp<T>>,
    {
        iter.fold(Fp::new(0), Add::add)
    }
}

impl<'a, T: 'a + Mod> iter::Sum<&'a Fp<T>> for Fp<T> {
    fn sum<I>(iter: I) -> Self
    where
        I: iter::Iterator<Item = &'a Fp<T>>,
    {
        iter.fold(Fp::new(0), Add::add)
    }
}

impl<T: Mod> iter::Product<Fp<T>> for Fp<T> {
    fn product<I>(iter: I) -> Self
    where
        I: iter::Iterator<Item = Fp<T>>,
    {
        iter.fold(Self::new(1), Mul::mul)
    }
}

impl<'a, T: 'a + Mod> iter::Product<&'a Fp<T>> for Fp<T> {
    fn product<I>(iter: I) -> Self
    where
        I: iter::Iterator<Item = &'a Fp<T>>,
    {
        iter.fold(Self::new(1), Mul::mul)
    }
}
impl<T: Mod> Debug for Fp<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(f, "{}", self.0)
    }
}
impl<T: Mod> Display for Fp<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(f, "{}", self.0)
    }
}

// ax + by = gcd(x, y) なる、互いに素な (a, b) を一組探して、(g, a, b) を返します。
//
// | 0  -x |   | y  -x | | x  0 |
// | 1   b | = | a   b | | y  1 |
fn ext_gcd(x: i64, y: i64) -> (i64, i64, i64) {
    let (b, g) = {
        let mut x = x;
        let mut y = y;
        let mut u = 0;
        let mut v = 1;
        while x != 0 {
            let q = y / x;
            y -= q * x;
            v -= q * u;
            std::mem::swap(&mut x, &mut y);
            std::mem::swap(&mut u, &mut v);
        }
        (v, y)
    };
    assert_eq!((g - b * y) % x, 0);
    let a = (g - b * y) / x;
    (g, a, b)
}

#[macro_export]
macro_rules! define_fp {
    ($vis:vis $fp:ident, $t:ident, $mod:expr) => {
        #[derive(Debug, Clone, PartialEq, Copy, Eq, Hash)]
        $vis struct $t;
        // NOTE: `$crate::` があるとうまく展開できません。
        impl Mod for $t {
            const MOD: i64 = $mod;
        }
        // NOTE: `$crate::` があるとうまく展開できません。
        $vis type $fp = Fp<$t>;
    }
}

// ------------ impl arith start ------------

impl<T: Mod> Associative for Fp<T> {}

impl<T: Mod> Zero for Fp<T> {
    fn zero() -> Self { Self::unchecked(0) }
    fn is_zero(&self) -> bool { self.0 == 0 }
}

impl<T: Mod> One for Fp<T> {
    fn one() -> Self { Self::unchecked(1) }
    fn is_one(&self) -> bool { self.0 == 1 }
}

impl<T: Mod> Add for Fp<T> {
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        let res = self.0 + rhs.0;
        Self::unchecked(if T::MOD <= res { res - T::MOD } else { res })
    }
}

impl<T: Mod> Sub for Fp<T> {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        let res = self.0 - rhs.0;
        Self::unchecked(if res < 0 { res + T::MOD } else { res })
    }
}

impl<T: Mod> Mul for Fp<T> {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self {
        Self::new(self.0 * rhs.0)
    }
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl<T: Mod> Div for Fp<T> {
    type Output = Self;
    fn div(self, rhs: Self) -> Self {
        self * rhs.inv()
    }
}

impl<M: Mod> Neg for Fp<M> {
    type Output = Self;
    fn neg(self) -> Self {
        if self.0 == 0 {
            Self::unchecked(0)
        } else {
            Self::unchecked(M::MOD - self.0)
        }
    }
}

impl<M: Mod> Neg for &Fp<M> {
    type Output = Fp<M>;
    fn neg(self) -> Self::Output {
        if self.0 == 0 {
            Fp::unchecked(0)
        } else {
            Fp::unchecked(M::MOD - self.0)
        }
    }
}

macro_rules! forward_assign_biop {
    ($(impl $trait:ident, $fn_assign:ident, $fn:ident)*) => {
        $(
            impl<M: Mod> $trait for Fp<M> {
                fn $fn_assign(&mut self, rhs: Self) {
                    *self = self.$fn(rhs);
                }
            }
        )*
    };
}

forward_assign_biop! {
    impl AddAssign, add_assign, add
    impl SubAssign, sub_assign, sub
    impl MulAssign, mul_assign, mul
    impl DivAssign, div_assign, div
}

macro_rules! forward_ref_binop {
    ($(impl $imp:ident, $method:ident)*) => {
        $(
            impl<'a, T: Mod> $imp<Fp<T>> for &'a Fp<T> {
                type Output = Fp<T>;
                fn $method(self, other: Fp<T>) -> Self::Output {
                    $imp::$method(*self, other)
                }
            }

            impl<'a, T: Mod> $imp<&'a Fp<T>> for Fp<T> {
                type Output = Fp<T>;
                fn $method(self, other: &Fp<T>) -> Self::Output {
                    $imp::$method(self, *other)
                }
            }

            impl<'a, T: Mod> $imp<&'a Fp<T>> for &'a Fp<T> {
                type Output = Fp<T>;
                fn $method(self, other: &Fp<T>) -> Self::Output {
                    $imp::$method(*self, *other)
                }
            }
        )*
    };
}

forward_ref_binop! {
    impl Add, add
    impl Sub, sub
    impl Mul, mul
    impl Div, div
}


// ------------ impl arith end ------------

// ------------ fp end ------------

// ------------ algebraic traits start ------------
use std::marker::Sized;
use std::ops::*;

/// 元
pub trait Element: Sized + Clone + PartialEq {}
impl<T: Sized + Clone + PartialEq> Element for T {}

/// 結合性
pub trait Associative: Magma {}

/// マグマ
pub trait Magma: Element + Add<Output=Self> {}
impl<T: Element + Add<Output=Self>> Magma for T {}

/// 半群
pub trait SemiGroup: Magma + Associative {}
impl<T: Magma + Associative> SemiGroup for T {}

/// モノイド
pub trait Monoid: SemiGroup + Zero {}
impl<T: SemiGroup + Zero> Monoid for T {}

pub trait ComMonoid: Monoid + AddAssign {}
impl<T: Monoid + AddAssign> ComMonoid for T {}

/// 群
pub trait Group: Monoid + Neg<Output=Self> {}
impl<T: Monoid + Neg<Output=Self>> Group for T {}

pub trait ComGroup: Group + ComMonoid {}
impl<T: Group + ComMonoid> ComGroup for T {}

/// 半環
pub trait SemiRing: ComMonoid + Mul<Output=Self> + One {}
impl<T: ComMonoid + Mul<Output=Self> + One> SemiRing for T {}

/// 環
pub trait Ring: ComGroup + SemiRing {}
impl<T: ComGroup + SemiRing> Ring for T {}

pub trait ComRing: Ring + MulAssign {}
impl<T: Ring + MulAssign> ComRing for T {}

/// 体
pub trait Field: ComRing + Div<Output=Self> + DivAssign {}
impl<T: ComRing + Div<Output=Self> + DivAssign> Field for T {}

/// 加法単元
pub trait Zero: Element {
    fn zero() -> Self;
    fn is_zero(&self) -> bool {
        *self == Self::zero()
    }
}

/// 乗法単元
pub trait One: Element {
    fn one() -> Self;
    fn is_one(&self) -> bool {
        *self == Self::one()
    }
}

macro_rules! impl_integer {
    ($($T:ty,)*) => {
        $(
            impl Associative for $T {}

            impl Zero for $T {
                fn zero() -> Self { 0 }
                fn is_zero(&self) -> bool { *self == 0 }
            }

            impl<'a> Zero for &'a $T {
                fn zero() -> Self { &0 }
                fn is_zero(&self) -> bool { *self == &0 }
            }

            impl One for $T {
                fn one() -> Self { 1 }
                fn is_one(&self) -> bool { *self == 1 }
            }

            impl<'a> One for &'a $T {
                fn one() -> Self { &1 }
                fn is_one(&self) -> bool { *self == &1 }
            }
        )*
    };
}

impl_integer! {
    i8, i16, i32, i64, i128, isize,
    u8, u16, u32, u64, u128, usize,
}
// ------------ algebraic traits end ------------

// ------------ Graph impl start ------------

pub trait Cost:
    Element
    + Clone + Copy + std::fmt::Display
    + Eq + Ord
    + Zero + One
    + Add<Output = Self> + AddAssign
    + Sub<Output = Self>
    + Neg<Output = Self>
{
    const MAX: Self;
}

#[derive(Copy, Clone)]
pub struct Edge<C = Void> {
    // pub from: usize,
    pub to: usize,
    pub cost: C,
    pub id: usize
}

pub struct UndirectedGraph<C>(pub Vec<Vec<Edge<C>>>, pub usize);
pub struct DirectedGraph<C>{
    pub forward: Vec<Vec<Edge<C>>>,
    pub backward: Vec<Vec<Edge<C>>>,
    pub count: usize,
}

pub trait Graph<C: Element> {
    fn new(size: usize) -> Self;
    fn size(&self) -> usize;
    fn add_edge(&mut self, u: usize, v: usize, cost: C);
    fn edges_from(&self, v: usize) -> std::slice::Iter<Edge<C>>;
}

impl<C: Element> Graph<C> for UndirectedGraph<C> {
    fn new(size: usize) -> Self {
        Self(vec![Vec::<Edge<C>>::new(); size], 0)
    }

    fn size(&self) -> usize {
        self.0.len()
    }

    fn add_edge(&mut self, u: usize, v: usize, cost: C) {
        self.0[u].push(Edge{ to: v, cost: cost.clone(), id: self.1 });
        self.0[v].push(Edge{ to: u, cost: cost.clone(), id: self.1 });
        self.1 += 1;
    }

    fn edges_from(&self, v: usize) -> std::slice::Iter<Edge<C>> {
        self.0[v].iter()
    }
}

impl<C: Element> Graph<C> for DirectedGraph<C> {
    fn new(size: usize) -> Self {
        Self {
            forward: vec![Vec::<Edge<C>>::new(); size],
            backward: vec![Vec::<Edge<C>>::new(); size],
            count: 0
        }
    }

    fn size(&self) -> usize {
        self.forward.len()
    }

    fn add_edge(&mut self, u: usize, v: usize, cost: C) {
        self.forward[u].push(Edge{ to: v, cost: cost.clone(), id: self.count });
        self.backward[v].push(Edge{ to: u, cost: cost.clone(), id: self.count });
        self.count += 1;
    }

    fn edges_from(&self, v: usize) -> std::slice::Iter<Edge<C>> {
        self.forward[v].iter()
    }
}

impl<C: Element> DirectedGraph<C> {
    pub fn edges_to(&self, u: usize) -> std::slice::Iter<Edge<C>> {
        self.backward[u].iter()
    }

    pub fn reverse(&self) -> Self {
        Self {
            forward: self.backward.clone(),
            backward: self.forward.clone(),
            count: self.count,
        }
    }
}

macro_rules! impl_cost {
    ($($T:ident,)*) => {
        $(
            impl Cost for $T { const MAX: Self = std::$T::MAX; }
        )*
    };
}

impl_cost! {
    i8, i16, i32, i64, i128, isize,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Void();

impl std::fmt::Display for Void {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "")
    }
}

impl Zero for Void {
    fn zero() -> Self { Void() }
    fn is_zero(&self) -> bool { true }
}

impl One for Void {
    fn one() -> Self { Void() }
    fn is_one(&self) -> bool { true }
}

impl Add for Void {
    type Output = Self;
    fn add(self, _: Self) -> Self { Void() }
}

impl AddAssign for Void {
    fn add_assign(&mut self, _: Self) {}
}

impl Sub for Void {
    type Output = Self;
    fn sub(self, _: Self) -> Self { Void() }
}

impl Neg for Void {
    type Output = Self;
    fn neg(self) -> Self { Void() }
}

impl Cost for Void { const MAX: Self = Void(); }

// ------------ Graph impl end ------------

// ------------ io module start ------------

use std::io::{stdout, BufWriter, Read, StdoutLock, Write};

pub struct IO {
	iter: std::str::SplitAsciiWhitespace<'static>,
	buf: BufWriter<StdoutLock<'static>>,
}

impl IO {
	pub fn new() -> Self {
		let mut input = String::new();
		std::io::stdin().read_to_string(&mut input).unwrap();
		let input = Box::leak(input.into_boxed_str());
		let out = Box::new(stdout());
		IO {
			iter: input.split_ascii_whitespace(),
			buf: BufWriter::new(Box::leak(out).lock()),
		}
	}
	fn scan_str(&mut self) -> &'static str {
		self.iter.next().unwrap()
	}
	fn scan_raw(&mut self) -> &'static [u8] {
		self.scan_str().as_bytes()
	}
	pub fn scan<T: Scan>(&mut self) -> T {
		T::scan(self)
	}
	pub fn scan_vec<T: Scan>(&mut self, n: usize) -> Vec<T> {
		(0..n).map(|_| self.scan()).collect()
	}
}

impl IO {
	pub fn print<T: Print>(&mut self, x: T) {
		T::print(self, x);
	}
	pub fn println<T: Print>(&mut self, x: T) {
		self.print(x);
		self.print("\n");
	}
	pub fn iterln<T: Print, I: Iterator<Item = T>>(&mut self, mut iter: I, delim: &str) {
		if let Some(v) = iter.next() {
			self.print(v);
			for v in iter {
				self.print(delim);
				self.print(v);
			}
		}
		self.print("\n");
	}
	pub fn flush(&mut self) {
		self.buf.flush().unwrap();
	}
}

impl Default for IO {
	fn default() -> Self {
		Self::new()
	}
}

pub trait Scan {
	fn scan(io: &mut IO) -> Self;
}

macro_rules! impl_parse_int {
	($($t:tt),*) => {
		$(
			impl Scan for $t {
				fn scan(s: &mut IO) -> Self {
					let mut res = 0;
					let mut neg = false;
					for d in s.scan_raw() {
						if *d == b'-' {
							neg = true;
						} else {
							res *= 10;
							res += (*d - b'0') as $t;
						}
					}
					if neg { res = res.wrapping_neg(); }
					res
				}
			}
		)*
	};
}

impl_parse_int!(i16, i32, i64, isize, u16, u32, u64, usize);

impl<T: Scan, U: Scan> Scan for (T, U) {
	fn scan(s: &mut IO) -> Self {
		(T::scan(s), U::scan(s))
	}
}

impl<T: Scan, U: Scan, V: Scan> Scan for (T, U, V) {
	fn scan(s: &mut IO) -> Self {
		(T::scan(s), U::scan(s), V::scan(s))
	}
}

impl<T: Scan, U: Scan, V: Scan, W: Scan> Scan for (T, U, V, W) {
	fn scan(s: &mut IO) -> Self {
		(T::scan(s), U::scan(s), V::scan(s), W::scan(s))
	}
}

pub trait Print {
	fn print(w: &mut IO, x: Self);
}

macro_rules! impl_print_int {
	($($t:ty),*) => {
		$(
			impl Print for $t {
				fn print(w: &mut IO, x: Self) {
					w.buf.write_all(x.to_string().as_bytes()).unwrap();
				}
			}
		)*
	};
}

impl_print_int!(i16, i32, i64, isize, u16, u32, u64, usize);

impl Print for u8 {
	fn print(w: &mut IO, x: Self) {
		w.buf.write_all(&[x]).unwrap();
	}
}

impl Print for &[u8] {
	fn print(w: &mut IO, x: Self) {
		w.buf.write_all(x).unwrap();
	}
}

impl Print for &str {
	fn print(w: &mut IO, x: Self) {
		w.print(x.as_bytes());
	}
}

impl<T: Print, U: Print> Print for (T, U) {
	fn print(w: &mut IO, (x, y): Self) {
		w.print(x);
		w.print(" ");
		w.print(y);
	}
}

impl<T: Print, U: Print, V: Print> Print for (T, U, V) {
	fn print(w: &mut IO, (x, y, z): Self) {
		w.print(x);
		w.print(" ");
		w.print(y);
		w.print(" ");
		w.print(z);
	}
}

// ------------ io module end ------------
0