結果

問題 No.1843 Tree ANDistance
ユーザー Moss_LocalMoss_Local
提出日時 2022-02-18 21:42:39
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 884 ms / 2,000 ms
コード長 14,996 bytes
コンパイル時間 15,955 ms
コンパイル使用メモリ 378,816 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2024-06-29 08:33:10
合計ジャッジ時間 32,115 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 880 ms
9,600 KB
testcase_01 AC 881 ms
9,600 KB
testcase_02 AC 879 ms
9,728 KB
testcase_03 AC 882 ms
9,728 KB
testcase_04 AC 884 ms
9,728 KB
testcase_05 AC 695 ms
9,728 KB
testcase_06 AC 699 ms
9,600 KB
testcase_07 AC 833 ms
9,600 KB
testcase_08 AC 850 ms
9,600 KB
testcase_09 AC 827 ms
9,472 KB
testcase_10 AC 816 ms
9,472 KB
testcase_11 AC 866 ms
9,728 KB
testcase_12 AC 595 ms
9,344 KB
testcase_13 AC 645 ms
9,344 KB
testcase_14 AC 57 ms
5,376 KB
testcase_15 AC 59 ms
5,376 KB
testcase_16 AC 44 ms
5,376 KB
testcase_17 AC 43 ms
5,376 KB
testcase_18 AC 38 ms
5,376 KB
testcase_19 AC 58 ms
5,376 KB
testcase_20 AC 45 ms
5,376 KB
testcase_21 AC 35 ms
5,376 KB
testcase_22 AC 35 ms
5,376 KB
testcase_23 AC 35 ms
5,376 KB
testcase_24 AC 35 ms
5,376 KB
testcase_25 AC 35 ms
5,376 KB
testcase_26 AC 36 ms
5,376 KB
testcase_27 AC 36 ms
5,376 KB
testcase_28 AC 657 ms
8,832 KB
testcase_29 AC 450 ms
7,680 KB
testcase_30 AC 440 ms
7,552 KB
testcase_31 AC 717 ms
9,088 KB
testcase_32 AC 674 ms
8,960 KB
testcase_33 AC 229 ms
6,272 KB
testcase_34 AC 528 ms
7,808 KB
testcase_35 AC 35 ms
5,376 KB
testcase_36 AC 36 ms
5,376 KB
testcase_37 AC 35 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unnecessary parentheses around type
  --> src/main.rs:70:15
   |
70 | fn readi() -> (i64) {
   |               ^   ^
   |
   = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
   |
70 - fn readi() -> (i64) {
70 + fn readi() -> i64 {
   |

warning: unnecessary parentheses around assigned value
   --> src/main.rs:347:19
    |
347 |         self.0 *= (rhs.0 % MOD);
    |                   ^           ^
    |
help: remove these parentheses
    |
347 -         self.0 *= (rhs.0 % MOD);
347 +         self.0 *= rhs.0 % MOD;
    |

warning: unused variable: `a2`
   --> src/main.rs:498:18
    |
498 |             let (a2, e2) = self.mod_fact(k, p);
    |                  ^^ help: if this is intentional, prefix it with an underscore: `_a2`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `moi`
   --> src/main.rs:503:21
    |
503 |                 let moi = self.mod_inverse(a3 % p, p);
    |                     ^^^ help: if this is intentional, prefix it with an underscore: `_moi`

warning: unused variable: `a2`
   --> src/main.rs:523:18
    |
523 |             let (a2, e2) = self.mod_fact(k, p);
    |                  ^^ help: if this is intentional, prefix it with an underscore: `_a2`

warning: unused variable: `a3`
   --> src/main.rs:524:18
    |
524 |             let (a3, e3) = self.mod_fact(n - k, p);
    |                  ^^ help: if this is intentional, prefix it with an underscore: `_a3`

warning: unused variable: `i`
   --> src/main.rs:569:9
    |
569 |     for i in 0..n - 1 {
    |         ^ help: if this is intentional, prefix it with an underscore: `_i`

ソースコード

diff #
プレゼンテーションモードにする

// -*- coding:utf-8-unix -*-
// #![feature(map_first_last)]
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_macros)]
use std::any::Any;
use std::cmp::Ordering::*;
use std::collections::*;
use std::convert::*;
use std::convert::{From, Into};
use std::error::Error;
use std::f64::consts::PI;
use std::fmt::Debug;
use std::fmt::Display;
use std::fs::File;
use std::hash::Hash;
use std::io::prelude::*;
use std::io::*;
use std::iter::Filter;
use std::marker::Copy;
use std::mem::*;
use std::ops::Bound::*;
use std::ops::RangeBounds;
use std::ops::{Add, Mul, Neg, Sub};
use std::slice::from_raw_parts;
use std::str;
use std::vec;
const INF: i64 = 1223372036854775807;
const UINF: usize = INF as usize;
// const FINF: f64 = 122337203685.0;
const LINF: i64 = 2147483647;
const FINF: f64 = LINF as f64;
const INF128: i128 = 1223372036854775807000000000000;
const MOD: i64 = 1000000007;
// const MOD: i64 = 998244353;
const MPI: f64 = 3.14159265358979323846264338327950288f64;
// const MOD: i64 = INF;
const UMOD: usize = MOD as usize;
use std::cmp::*;
use std::collections::*;
use std::io::stdin;
use std::io::stdout;
use std::io::Write;
macro_rules! p {
($x:expr) => {
println!("{}", $x);
};
}
macro_rules! d {
($x:expr) => {
println!("{:?}", $x);
};
}
// use str::Chars;
// use str::Chars;
#[allow(dead_code)]
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()
}
#[allow(dead_code)]
fn readi() -> (i64) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
iter.next().unwrap().parse::<i64>().unwrap()
}
#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
read::<String>()
.split_whitespace()
.map(|e| e.parse().ok().unwrap())
.collect()
}
#[allow(dead_code)]
fn read_mat<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
(0..n).map(|_| read_vec()).collect()
}
#[allow(dead_code)]
fn readii() -> (i64, i64) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
(
iter.next().unwrap().parse::<i64>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
)
}
fn readff() -> (f64, f64) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
(
iter.next().unwrap().parse::<f64>().unwrap(),
iter.next().unwrap().parse::<f64>().unwrap(),
)
}
#[allow(dead_code)]
fn readiii() -> (i64, i64, i64) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
(
iter.next().unwrap().parse::<i64>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
)
}
#[allow(dead_code)]
fn readuu() -> (usize, usize) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
(
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<usize>().unwrap(),
)
}
fn readcc() -> (char, char) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
(
iter.next().unwrap().parse::<char>().unwrap(),
iter.next().unwrap().parse::<char>().unwrap(),
)
}
fn readuuu() -> (usize, usize, usize) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
(
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<usize>().unwrap(),
)
}
#[allow(dead_code)]
fn readiiii() -> (i64, i64, i64, i64) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
(
iter.next().unwrap().parse::<i64>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
)
}
#[allow(dead_code)]
fn readuuuu() -> (usize, usize, usize, usize) {
let mut str = String::new();
let _ = stdin().read_line(&mut str).unwrap();
let mut iter = str.split_whitespace();
(
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<usize>().unwrap(),
)
}
pub struct Dsu {
n: usize,
// root node: -1 * component size
// otherwise: parent
parent_or_size: Vec<i32>,
}
impl Dsu {
// 0 <= size <= 10^8 is constrained.
pub fn new(size: usize) -> Self {
Self {
n: size,
parent_or_size: vec![-1; size],
}
}
pub fn merge(&mut self, a: usize, b: usize) -> usize {
assert!(a < self.n);
assert!(b < self.n);
let (mut x, mut y) = (self.leader(a), self.leader(b));
if x == y {
return x;
}
if -self.parent_or_size[x] < -self.parent_or_size[y] {
std::mem::swap(&mut x, &mut y);
}
self.parent_or_size[x] += self.parent_or_size[y];
self.parent_or_size[y] = x as i32;
x
}
pub fn same(&mut self, a: usize, b: usize) -> bool {
assert!(a < self.n);
assert!(b < self.n);
self.leader(a) == self.leader(b)
}
pub fn leader(&mut self, a: usize) -> usize {
assert!(a < self.n);
if self.parent_or_size[a] < 0 {
return a;
}
self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32;
self.parent_or_size[a] as usize
}
pub fn size(&mut self, a: usize) -> usize {
assert!(a < self.n);
let x = self.leader(a);
-self.parent_or_size[x] as usize
}
pub fn groups(&mut self) -> Vec<Vec<usize>> {
let mut leader_buf = vec![0; self.n];
let mut group_size = vec![0; self.n];
for i in 0..self.n {
leader_buf[i] = self.leader(i);
group_size[leader_buf[i]] += 1;
}
let mut result = vec![Vec::new(); self.n];
for i in 0..self.n {
result[i].reserve(group_size[i]);
}
for i in 0..self.n {
result[leader_buf[i]].push(i);
}
result
.into_iter()
.filter(|x| !x.is_empty())
.collect::<Vec<Vec<usize>>>()
}
}
macro_rules! M {
(a :expr ) => {
M::new({ a })
};
}
#[derive(Copy, Clone, Debug)]
pub struct M(i64);
impl M {
fn new(x: i64) -> Self {
M(x.rem_euclid(MOD))
}
fn pow(self, n: usize) -> Self {
match n {
0 => M::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 M {
type Output = M;
fn neg(self) -> Self::Output {
Self::new(-self.0)
}
}
impl std::ops::AddAssign<M> for M {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
self.0 %= MOD;
}
}
impl std::ops::AddAssign<i64> for M {
fn add_assign(&mut self, rhs: i64) {
*self += M::new(rhs);
}
}
impl std::ops::AddAssign<usize> for M {
fn add_assign(&mut self, rhs: usize) {
*self += M::new(rhs as i64);
}
}
impl<T> std::ops::Add<T> for M
where
M: std::ops::AddAssign<T>,
{
type Output = Self;
fn add(self, other: T) -> Self {
let mut res = self;
res += other;
res
}
}
impl std::ops::SubAssign<M> for M {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
if self.0 < 0 {
self.0 += MOD;
}
}
}
impl std::ops::SubAssign<i64> for M {
fn sub_assign(&mut self, rhs: i64) {
*self -= M::new(rhs);
if (*self).0 < 0 {
self.0 += MOD;
}
}
}
impl std::ops::SubAssign<usize> for M {
fn sub_assign(&mut self, rhs: usize) {
*self -= M::new(rhs as i64);
if (*self).0 < 0 {
self.0 += MOD;
}
}
}
impl<T> std::ops::Sub<T> for M
where
M: std::ops::SubAssign<T>,
{
type Output = Self;
fn sub(self, other: T) -> Self {
let mut res = self;
res -= other;
res
}
}
impl std::ops::MulAssign<M> for M {
fn mul_assign(&mut self, rhs: Self) {
self.0 %= MOD;
self.0 *= (rhs.0 % MOD);
self.0 %= MOD;
}
}
impl std::ops::MulAssign<i64> for M {
fn mul_assign(&mut self, rhs: i64) {
*self *= M::new(rhs);
}
}
impl std::ops::MulAssign<usize> for M {
fn mul_assign(&mut self, rhs: usize) {
*self *= M::new(rhs as i64);
}
}
impl<T> std::ops::Mul<T> for M
where
M: std::ops::MulAssign<T>,
{
type Output = Self;
fn mul(self, other: T) -> Self {
let mut res = self;
res *= other;
res
}
}
impl std::ops::DivAssign<M> for M {
fn div_assign(&mut self, rhs: Self) {
*self *= rhs.inv();
}
}
impl std::ops::DivAssign<i64> for M {
fn div_assign(&mut self, rhs: i64) {
*self /= M::new(rhs);
}
}
impl std::ops::DivAssign<usize> for M {
fn div_assign(&mut self, rhs: usize) {
*self /= M::new(rhs as i64);
}
}
impl<T> std::ops::Div<T> for M
where
M: 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 M {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::ops::Deref for M {
type Target = i64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for M {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[allow(dead_code)]
pub fn gcd(a: usize, b: usize) -> usize {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
#[allow(dead_code)]
pub fn lcm(a: usize, b: usize) -> usize {
a / gcd(a, b) * b
}
#[allow(dead_code)]
/// (gcd, x, y)
pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {
if b == 0 {
(a, 1, 0)
} else {
let (gcd, x, y) = extgcd(b, a % b);
(gcd, y, x - (a / b) * y)
}
}
#[allow(dead_code)]
/// x ^ n % m
pub fn mod_pow(x: usize, n: usize, m: usize) -> usize {
let mut res = 1;
let mut x = x % m;
let mut n = n;
while n > 0 {
if n & 1 == 1 {
res = (res * x) % m;
}
x = (x * x) % m;
n >>= 1;
}
res
}
pub struct Combination {
m: usize,
f_table: Vec<usize>,
moi: Vec<usize>,
}
impl Combination {
// 0 <= size <= 10^8 is constrained.
pub fn new(mod_num: usize, table_size: usize) -> Self {
Self {
m: mod_num,
f_table: vec![0; table_size],
moi: vec![0; 0],
}
}
pub fn build(&mut self) {
let size = self.f_table.len();
self.f_table = self.fact_table(size, self.m);
self.moi = self.fact_inv_table(size, self.m);
}
fn fact_table(&mut self, len: usize, m: usize) -> Vec<usize> {
let mut res = vec![1; len + 1];
for i in 1..len + 1 {
res[i] = (i as usize * res[i - 1]) % m;
}
res
}
fn fact_inv_table(&mut self, len: usize, m: usize) -> Vec<usize> {
let mut res = vec![1; len + 1];
for i in 1..len + 1 {
res[i] = (res[i - 1] * self.mod_inverse(i, m)) % m;
}
res
}
pub fn p(&mut self, n: usize, k: usize) -> i64 {
let p = MOD as usize;
if k == 0 {
return 1;
}
if n < k {
0
} else {
let (a1, e1) = self.mod_fact(n, p);
let (a2, e2) = self.mod_fact(k, p);
let (a3, e3) = self.mod_fact(n - k, p);
if e1 > e2 + e3 {
0
} else {
let moi = self.mod_inverse(a3 % p, p);
(a1 * self.mod_inverse(a3 % p, p) % p) as i64
}
}
}
pub fn c(&mut self, n: usize, k: usize) -> i64 {
let p = MOD as usize;
if n == 0 && k == 0 {
return 1;
}
if n == 0 {
return 0;
}
if k == 0 {
return 1;
}
if n < k {
0
} else {
let (a1, e1) = self.mod_fact(n, p);
let (a2, e2) = self.mod_fact(k, p);
let (a3, e3) = self.mod_fact(n - k, p);
if e1 > e2 + e3 {
0
} else {
(((a1 * &self.moi[k]) % p * &self.moi[n - k]) % p) as i64
}
}
}
pub fn h(&mut self, n: usize, k: usize) -> i64 {
return self.c(n + k - 1, k);
}
pub fn factorial(&mut self, n: usize) -> i64 {
return self.p(n, n);
}
fn extgcd(&mut self, a: i64, b: i64) -> (i64, i64, i64) {
if b == 0 {
(a, 1, 0)
} else {
let (gcd, x, y) = extgcd(b, a % b);
(gcd, y, x - (a / b) * y)
}
}
fn mod_inverse(&mut self, a: usize, m: usize) -> usize {
let (_, x, _) = self.extgcd(a as i64, m as i64);
((m as i64 + x) as usize % m) % m
}
fn mod_fact(&mut self, n: usize, p: usize) -> (usize, usize) {
if n == 0 {
(1, 0)
} else {
let (a, b) = self.mod_fact(n / p, p);
let pow = b + n / p;
if n / p % 2 != 0 {
(a * (p - self.f_table[(n % p) as usize]) % p, pow)
} else {
(a * self.f_table[(n % p) as usize] % p, pow)
}
}
}
}
fn solve() {
let n: usize = read();
let mut es = vec![];
for i in 0..n - 1 {
let (a, b, c) = readuuu();
es.push((a - 1, b - 1, c));
}
let mut res = M(0);
let mut c: Combination = Combination::new(MOD as usize, 201010);
c.build();
for i in 0..32 {
let mut dsu = Dsu::new(n);
for &e in es.iter() {
if e.2 & (1 << i) != 0 {
dsu.merge(e.0, e.1);
}
}
let mut set = BTreeSet::new();
for j in 0..n {
let l = dsu.leader(j);
if !set.contains(&l) {
let x = dsu.size(l);
res += M(2).pow(i) * c.c(x, 2);
}
set.insert(l);
}
// p!(res.0);
}
// p!(c.c(1, 2));
p!(res.0);
return;
}
fn main() {
solve();
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0