結果

問題 No.1207 グラフX
ユーザー fukafukatanifukafukatani
提出日時 2020-08-31 21:39:16
言語 Rust
(1.77.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 8,024 bytes
コンパイル時間 13,096 ms
コンパイル使用メモリ 382,000 KB
実行使用メモリ 48,688 KB
最終ジャッジ日時 2024-04-28 11:14:37
合計ジャッジ時間 25,126 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 212 ms
33,172 KB
testcase_01 AC 228 ms
33,152 KB
testcase_02 AC 254 ms
33,152 KB
testcase_03 AC 214 ms
33,152 KB
testcase_04 AC 266 ms
33,076 KB
testcase_05 AC 258 ms
48,684 KB
testcase_06 AC 251 ms
48,688 KB
testcase_07 AC 245 ms
48,688 KB
testcase_08 AC 146 ms
22,732 KB
testcase_09 AC 186 ms
27,392 KB
testcase_10 AC 264 ms
38,832 KB
testcase_11 AC 249 ms
48,688 KB
testcase_12 AC 151 ms
24,192 KB
testcase_13 AC 63 ms
9,984 KB
testcase_14 AC 204 ms
33,192 KB
testcase_15 AC 164 ms
26,624 KB
testcase_16 AC 65 ms
10,752 KB
testcase_17 AC 121 ms
20,480 KB
testcase_18 AC 110 ms
20,352 KB
testcase_19 AC 102 ms
15,744 KB
testcase_20 AC 213 ms
33,644 KB
testcase_21 AC 11 ms
6,940 KB
testcase_22 AC 125 ms
20,864 KB
testcase_23 AC 140 ms
23,696 KB
testcase_24 AC 91 ms
18,304 KB
testcase_25 AC 206 ms
33,476 KB
testcase_26 AC 149 ms
25,584 KB
testcase_27 AC 191 ms
30,324 KB
testcase_28 AC 178 ms
27,672 KB
testcase_29 AC 185 ms
29,924 KB
testcase_30 AC 81 ms
13,568 KB
testcase_31 AC 53 ms
8,064 KB
testcase_32 AC 72 ms
13,952 KB
testcase_33 AC 72 ms
13,824 KB
testcase_34 AC 164 ms
27,456 KB
testcase_35 AC 12 ms
6,944 KB
testcase_36 AC 168 ms
27,112 KB
testcase_37 AC 137 ms
22,016 KB
testcase_38 AC 31 ms
7,040 KB
testcase_39 AC 76 ms
14,848 KB
testcase_40 AC 40 ms
6,940 KB
testcase_41 AC 104 ms
15,488 KB
testcase_42 AC 1 ms
6,944 KB
testcase_43 AC 1 ms
6,940 KB
testcase_44 AC 1 ms
6,944 KB
testcase_45 AC 204 ms
33,272 KB
testcase_46 AC 200 ms
33,152 KB
testcase_47 AC 197 ms
33,128 KB
testcase_48 AC 194 ms
34,196 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> src/main.rs:23:9
   |
23 |     for i in 0..m {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: associated items `set_modulus` and `inv` are never used
   --> src/main.rs:83:8
    |
82  | impl Modulo {
    | ----------- associated items in this implementation
83  |     fn set_modulus(m: i64) {
    |        ^^^^^^^^^^^
...
113 |     fn inv(self) -> Modulo {
    |        ^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: method `get_size` is never used
   --> src/main.rs:271:8
    |
248 | impl UnionFindTree {
    | ------------------ method in this implementation
...
271 |     fn get_size(&mut self, x: usize) -> usize {
    |        ^^^^^^^^

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i64>();
    let (n, m, x) = (v[0] as usize, v[1], v[2]);

    let mut edges = vec![];
    for i in 0..m {
        let v = read_vec::<i64>();
        let (x, y, z) = (v[0] as usize - 1, v[1] as usize - 1, v[2]);
        edges.push((x, y, z));
    }

    let mut uft = UnionFindTree::new(n);

    let mut graph = vec![vec![]; n];
    for (x, y, z) in edges {
        if uft.same(x, y) {
            continue;
        }
        graph[x].push((y, z));
        graph[y].push((x, z));
        uft.unite(x, y);
    }

    let mut ans = Modulo(0);
    dfs(0, 0, &graph, Modulo(x), n as i64, &mut ans);
    println!("{}", ans);
}

fn dfs(
    cur: usize,
    parent: usize,
    graph: &Vec<Vec<(usize, i64)>>,
    x: Modulo,
    n: i64,
    ans: &mut Modulo,
) -> i64 {
    let mut ret = 0;
    for &(to, cost) in graph[cur].iter() {
        if to == parent {
            continue;
        }
        let temp = dfs(to, cur, graph, x, n, ans);
        ret += temp;
        *ans += x.pow(cost) * temp * (n - temp);
    }
    ret + 1
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Modulo(i64);
static mut MODULUS: i64 = 1000000007;
impl Modulo {
    fn set_modulus(m: i64) {
        unsafe {
            MODULUS = m;
        }
    }
    fn get_modulus() -> i64 {
        unsafe { MODULUS }
    }
    fn new(x: i64) -> Modulo {
        let m = Modulo::get_modulus();
        if x < 0 {
            Modulo(x % m + m)
        } else if x < m {
            Modulo(x)
        } else {
            Modulo(x % m)
        }
    }
    fn pow(self, p: i64) -> Modulo {
        if p == 0 {
            Modulo(1)
        } else {
            let mut t = self.pow(p / 2);
            t *= t;
            if p & 1 == 1 {
                t *= self;
            }
            t
        }
    }
    fn inv(self) -> Modulo {
        self.pow(Modulo::get_modulus() - 2)
    }
}
impl std::fmt::Display for Modulo {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}
impl std::ops::AddAssign for Modulo {
    fn add_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 += other.0;
        if self.0 >= m {
            self.0 -= m;
        }
    }
}
impl std::ops::MulAssign for Modulo {
    fn mul_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 *= other.0;
        self.0 %= m;
    }
}
impl std::ops::SubAssign for Modulo {
    fn sub_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 += m - other.0;
        if self.0 >= m {
            self.0 -= m;
        }
    }
}
macro_rules! impl_modulo_ops {
    ($imp:ident, $method:ident, $assign_imp:ident, $assign_method:ident) => {
        impl<'a> std::ops::$assign_imp<&'a Modulo> for Modulo {
            fn $assign_method(&mut self, other: &'a Modulo) {
                std::ops::$assign_imp::$assign_method(self, *other);
            }
        }
        impl std::ops::$imp for Modulo {
            type Output = Modulo;
            fn $method(self, other: Modulo) -> Modulo {
                let mut x = self;
                std::ops::$assign_imp::$assign_method(&mut x, other);
                x
            }
        }
        impl<'a> std::ops::$imp<Modulo> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: Modulo) -> Modulo {
                std::ops::$imp::$method(*self, other)
            }
        }
        impl<'a> std::ops::$imp<&'a Modulo> for Modulo {
            type Output = Modulo;
            fn $method(self, other: &'a Modulo) -> Modulo {
                std::ops::$imp::$method(self, *other)
            }
        }
        impl<'a, 'b> std::ops::$imp<&'b Modulo> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: &'b Modulo) -> Modulo {
                std::ops::$imp::$method(*self, *other)
            }
        }
        impl std::ops::$assign_imp<i64> for Modulo {
            fn $assign_method(&mut self, other: i64) {
                std::ops::$assign_imp::$assign_method(self, Modulo::new(other));
            }
        }
        impl<'a> std::ops::$assign_imp<&'a i64> for Modulo {
            fn $assign_method(&mut self, other: &'a i64) {
                std::ops::$assign_imp::$assign_method(self, *other);
            }
        }
        impl std::ops::$imp<i64> for Modulo {
            type Output = Modulo;
            fn $method(self, other: i64) -> Modulo {
                let mut x = self;
                std::ops::$assign_imp::$assign_method(&mut x, other);
                x
            }
        }
        impl<'a> std::ops::$imp<&'a i64> for Modulo {
            type Output = Modulo;
            fn $method(self, other: &'a i64) -> Modulo {
                std::ops::$imp::$method(self, *other)
            }
        }
        impl<'a> std::ops::$imp<i64> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: i64) -> Modulo {
                std::ops::$imp::$method(*self, other)
            }
        }
        impl<'a, 'b> std::ops::$imp<&'b i64> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: &'b i64) -> Modulo {
                std::ops::$imp::$method(*self, *other)
            }
        }
    };
}
impl_modulo_ops!(Add, add, AddAssign, add_assign);
impl_modulo_ops!(Mul, mul, MulAssign, mul_assign);
impl_modulo_ops!(Sub, sub, SubAssign, sub_assign);

use std::iter::Sum;
impl Sum for Modulo {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = Modulo>,
    {
        iter.fold(Modulo(0), |a, b| a + b)
    }
}

impl<'a> Sum<&'a Modulo> for Modulo {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = &'a Self>,
    {
        iter.fold(Modulo(0), |a, b| a + b)
    }
}

#[derive(Debug, Clone)]
struct UnionFindTree {
    parent: Vec<isize>,
    size: Vec<usize>,
    height: Vec<u64>,
}

impl UnionFindTree {
    fn new(n: usize) -> UnionFindTree {
        UnionFindTree {
            parent: vec![-1; n],
            size: vec![1usize; n],
            height: vec![0u64; n],
        }
    }

    fn find(&mut self, index: usize) -> usize {
        if self.parent[index] == -1 {
            return index;
        }
        let idx = self.parent[index] as usize;
        let ret = self.find(idx);
        self.parent[index] = ret as isize;
        ret
    }

    fn same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }

    fn get_size(&mut self, x: usize) -> usize {
        let idx = self.find(x);
        self.size[idx]
    }

    fn unite(&mut self, index0: usize, index1: usize) -> bool {
        let a = self.find(index0);
        let b = self.find(index1);
        if a == b {
            false
        } else {
            if self.height[a] > self.height[b] {
                self.parent[b] = a as isize;
                self.size[a] += self.size[b];
            } else if self.height[a] < self.height[b] {
                self.parent[a] = b as isize;
                self.size[b] += self.size[a];
            } else {
                self.parent[b] = a as isize;
                self.size[a] += self.size[b];
                self.height[a] += 1;
            }
            true
        }
    }
}
0