結果

問題 No.1207 グラフX
ユーザー o2co2c
提出日時 2020-09-11 18:30:24
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 7,121 bytes
コンパイル時間 10,941 ms
コンパイル使用メモリ 382,628 KB
実行使用メモリ 55,064 KB
最終ジャッジ日時 2024-06-07 23:05:37
合計ジャッジ時間 20,048 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 182 ms
37,852 KB
testcase_01 AC 202 ms
37,888 KB
testcase_02 AC 197 ms
37,760 KB
testcase_03 AC 196 ms
37,760 KB
testcase_04 AC 198 ms
37,752 KB
testcase_05 AC 235 ms
54,996 KB
testcase_06 AC 245 ms
55,040 KB
testcase_07 AC 225 ms
54,928 KB
testcase_08 AC 115 ms
22,528 KB
testcase_09 AC 128 ms
29,056 KB
testcase_10 AC 228 ms
45,056 KB
testcase_11 AC 219 ms
55,064 KB
testcase_12 AC 116 ms
24,784 KB
testcase_13 AC 48 ms
7,168 KB
testcase_14 AC 153 ms
36,024 KB
testcase_15 AC 121 ms
27,776 KB
testcase_16 AC 50 ms
8,960 KB
testcase_17 AC 92 ms
21,120 KB
testcase_18 AC 80 ms
22,144 KB
testcase_19 AC 81 ms
13,696 KB
testcase_20 AC 157 ms
36,996 KB
testcase_21 AC 7 ms
5,376 KB
testcase_22 AC 93 ms
21,632 KB
testcase_23 AC 101 ms
25,088 KB
testcase_24 AC 68 ms
19,840 KB
testcase_25 AC 152 ms
36,608 KB
testcase_26 AC 118 ms
27,392 KB
testcase_27 AC 142 ms
32,384 KB
testcase_28 AC 127 ms
29,184 KB
testcase_29 AC 133 ms
32,256 KB
testcase_30 AC 58 ms
13,440 KB
testcase_31 AC 37 ms
5,376 KB
testcase_32 AC 50 ms
14,720 KB
testcase_33 AC 52 ms
13,952 KB
testcase_34 AC 125 ms
27,392 KB
testcase_35 AC 7 ms
5,376 KB
testcase_36 AC 124 ms
28,764 KB
testcase_37 AC 100 ms
22,656 KB
testcase_38 AC 25 ms
7,040 KB
testcase_39 AC 56 ms
15,488 KB
testcase_40 AC 25 ms
5,376 KB
testcase_41 AC 75 ms
14,208 KB
testcase_42 AC 0 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 1 ms
5,376 KB
testcase_45 AC 150 ms
37,740 KB
testcase_46 AC 142 ms
37,672 KB
testcase_47 AC 139 ms
37,760 KB
testcase_48 AC 148 ms
37,760 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
   --> src/main.rs:173:5
    |
172 | struct UnionFind {
    |        --------- field in this struct
173 |     n: usize,
    |     ^
    |
    = note: `#[warn(dead_code)]` on by default

warning: methods `size` and `sizes` are never used
   --> src/main.rs:210:8
    |
177 | impl UnionFind {
    | -------------- methods in this implementation
...
210 |     fn size(&mut self, x: usize) -> usize {
    |        ^^^^
...
214 |     fn sizes(&mut self) -> Vec<usize> {
    |        ^^^^^

ソースコード

diff #

pub fn readln() -> String {
    let mut line = String::new();
    ::std::io::stdin()
        .read_line(&mut line)
        .unwrap_or_else(|e| panic!("{}", e));
    line
}
macro_rules !read {($($t :tt ) ,*;$n :expr ) =>{{let stdin =::std ::io ::stdin () ;let ret =::std ::io ::BufRead ::lines (stdin .lock () ) .take ($n ) .map (|line |{let line =line .unwrap () ;let mut it =line .split_whitespace () ;_read !(it ;$($t ) ,*) } ) .collect ::<Vec <_ >>() ;ret } } ;($($t :tt ) ,*) =>{{let line =readln () ;let mut it =line .split_whitespace () ;_read !(it ;$($t ) ,*) } } ;}
macro_rules !_read {($it :ident ;[char ] ) =>{_read !($it ;String ) .chars () .collect ::<Vec <_ >>() } ;($it :ident ;[u8 ] ) =>{Vec ::from (_read !($it ;String ) .into_bytes () ) } ;($it :ident ;usize1 ) =>{$it .next () .unwrap_or_else (||panic !("input mismatch" ) ) .parse ::<usize >() .unwrap_or_else (|e |panic !("{}" ,e ) ) -1 } ;($it :ident ;[usize1 ] ) =>{$it .map (|s |s .parse ::<usize >() .unwrap_or_else (|e |panic !("{}" ,e ) ) -1 ) .collect ::<Vec <_ >>() } ;($it :ident ;[$t :ty ] ) =>{$it .map (|s |s .parse ::<$t >() .unwrap_or_else (|e |panic !("{}" ,e ) ) ) .collect ::<Vec <_ >>() } ;($it :ident ;$t :ty ) =>{$it .next () .unwrap_or_else (||panic !("input mismatch" ) ) .parse ::<$t >() .unwrap_or_else (|e |panic !("{}" ,e ) ) } ;($it :ident ;$($t :tt ) ,+) =>{($(_read !($it ;$t ) ) ,*) } ;}

macro_rules! mint {
    ($a :expr ) => {
        Mint::new({ $a })
    };
}
const MOD: i64 = 1000000007;
#[derive(Copy, Clone)]
pub struct Mint(i64);
impl Mint {
    fn new(x: i64) -> Self {
        Mint(x.rem_euclid(MOD))
    }
    fn pow(self, n: usize) -> Self {
        match n {
            0 => Mint::new(1),
            _ => {
                let mut a = self.pow(n >> 1);
                a *= a;
                if n & 1 == 1 {
                    a *= self;
                }
                a
            }
        }
    }
    fn inv(self) -> Self {
        self.pow((MOD - 2) as usize)
    }
}
impl std::ops::Neg for Mint {
    type Output = Mint;
    fn neg(self) -> Self::Output {
        Self::new(-self.0)
    }
}
impl std::ops::AddAssign<Mint> for Mint {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::AddAssign<i64> for Mint {
    fn add_assign(&mut self, rhs: i64) {
        *self += Mint::new(rhs);
    }
}
impl std::ops::AddAssign<usize> for Mint {
    fn add_assign(&mut self, rhs: usize) {
        *self += Mint::new(rhs as i64);
    }
}
impl<T> std::ops::Add<T> for Mint
where
    Mint: std::ops::AddAssign<T>,
{
    type Output = Self;
    fn add(self, other: T) -> Self {
        let mut res = self;
        res += other;
        res
    }
}
impl std::ops::SubAssign<Mint> for Mint {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
        if self.0 < 0 {
            self.0 += MOD;
        }
    }
}
impl std::ops::SubAssign<i64> for Mint {
    fn sub_assign(&mut self, rhs: i64) {
        *self -= Mint::new(rhs);
    }
}
impl std::ops::SubAssign<usize> for Mint {
    fn sub_assign(&mut self, rhs: usize) {
        *self -= Mint::new(rhs as i64);
    }
}
impl<T> std::ops::Sub<T> for Mint
where
    Mint: std::ops::SubAssign<T>,
{
    type Output = Self;
    fn sub(self, other: T) -> Self {
        let mut res = self;
        res -= other;
        res
    }
}
impl std::ops::MulAssign<Mint> for Mint {
    fn mul_assign(&mut self, rhs: Self) {
        self.0 *= rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::MulAssign<i64> for Mint {
    fn mul_assign(&mut self, rhs: i64) {
        *self *= Mint::new(rhs);
    }
}
impl std::ops::MulAssign<usize> for Mint {
    fn mul_assign(&mut self, rhs: usize) {
        *self *= Mint::new(rhs as i64);
    }
}
impl<T> std::ops::Mul<T> for Mint
where
    Mint: std::ops::MulAssign<T>,
{
    type Output = Self;
    fn mul(self, other: T) -> Self {
        let mut res = self;
        res *= other;
        res
    }
}
impl std::ops::DivAssign<Mint> for Mint {
    fn div_assign(&mut self, rhs: Self) {
        *self *= rhs.inv();
    }
}
impl std::ops::DivAssign<i64> for Mint {
    fn div_assign(&mut self, rhs: i64) {
        *self /= Mint::new(rhs);
    }
}
impl std::ops::DivAssign<usize> for Mint {
    fn div_assign(&mut self, rhs: usize) {
        *self /= Mint::new(rhs as i64);
    }
}
impl<T> std::ops::Div<T> for Mint
where
    Mint: std::ops::DivAssign<T>,
{
    type Output = Self;
    fn div(self, other: T) -> Self {
        let mut res = self;
        res /= other;
        res
    }
}
impl std::fmt::Display for Mint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl std::ops::Deref for Mint {
    type Target = i64;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl std::ops::DerefMut for Mint {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

struct UnionFind {
    n: usize,
    par: Vec<usize>,
    rank: Vec<usize>,
}
impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n,
            par: (0..n).collect(),
            rank: vec![1; n],
        }
    }
    fn unite(&mut self, x: usize, y: usize) {
        let rx = self.root(x);
        let ry = self.root(y);
        if rx == ry {
            return;
        }
        if self.rank[x] > self.rank[y] {
            self.par[ry] = rx;
            self.rank[rx] += self.rank[ry];
        } else {
            self.par[rx] = ry;
            self.rank[ry] += self.rank[rx];
        }
    }
    fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    fn root(&mut self, x: usize) -> usize {
        if self.par[x] == x {
            x
        } else {
            self.par[x] = self.root(self.par[x]);
            self.par[x]
        }
    }
    fn size(&mut self, x: usize) -> usize {
        let r = self.root(x);
        self.rank[r]
    }
    fn sizes(&mut self) -> Vec<usize> {
        let mut s = vec![];
        for x in 0..self.n {
            if x == self.root(x) {
                s.push(self.rank[x]);
            }
        }
        s
    }
}

fn main() {
    let (n, m, r) = read!(usize, usize, i64);
    let r = mint!(r);
    let mut uf = UnionFind::new(n);
    let mut to = vec![vec![]; n];
    let mut i = 0;
    for (x, y, z) in (0..m).map(|_| read!(usize1, usize1, usize)) {
        if uf.same(x, y) {
            continue;
        }
        uf.unite(x, y);
        to[x].push((y, i, z));
        to[y].push((x, i, z));
        i += 1;
    }
    let mut cost = vec![r; i];
    dfs(n, 0, None, &to, &mut cost);
    let mut ans = mint!(0);
    for c in cost {
        ans += c;
    }
    println!("{}", ans);
}

fn dfs(
    n: usize,
    u: usize,
    p: Option<usize>,
    to: &Vec<Vec<(usize, usize, usize)>>,
    cost: &mut Vec<Mint>,
) -> usize {
    let mut d = 1;
    for &(v, i, z) in to[u].iter() {
        if p == Some(v) {
            continue;
        }
        let m = dfs(n, v, Some(u), to, cost);
        cost[i] = cost[i].pow(z) * ((n - m) * m) as i64;
        d += m;
    }
    d
}
0