結果

問題 No.556 仁義なきサルたち
ユーザー hatoohatoo
提出日時 2017-10-11 10:52:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 2,983 bytes
コンパイル時間 13,183 ms
コンパイル使用メモリ 395,356 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 15:52:25
合計ジャッジ時間 14,756 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 8 ms
6,944 KB
testcase_14 AC 8 ms
6,944 KB
testcase_15 AC 8 ms
6,940 KB
testcase_16 AC 14 ms
6,944 KB
testcase_17 AC 15 ms
6,944 KB
testcase_18 AC 15 ms
6,940 KB
testcase_19 AC 14 ms
6,940 KB
testcase_20 AC 15 ms
6,940 KB
testcase_21 AC 15 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `rank` is never read
  --> src/main.rs:67:9
   |
64 | struct UFT {
   |        --- field in this struct
...
67 |     pub rank: Vec<usize>,
   |         ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn get<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }

    #[allow(dead_code)]
    pub fn get2<T: FromStr, U: FromStr>() -> (T, U)
    where
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }
}


#[allow(unused_macros)]
macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

struct UFT {
    pub par: Vec<usize>,
    pub num: Vec<usize>,
    pub rank: Vec<usize>,
}

impl UFT {
    fn new(n: usize) -> Self {
        UFT {
            par: (0..n).collect(),
            num: vec![1; n],
            rank: vec![0; n],
        }
    }

    fn find(&mut self, x: usize) -> usize {
        if self.par[x] == x {
            x
        } else {
            let p = self.par[x];
            let pp = self.find(p);
            self.par[x] = pp;
            pp
        }
    }

    fn unite(&mut self, x: usize, y: usize) {
        let x = self.find(x);
        let y = self.find(y);
        if x == y {
            return;
        }

        if (self.num[x], y) > (self.num[y], x) {
            self.par[y] = x;
            self.num[x] += self.num[y];
            self.num[y] = 0;
        } else {
            self.par[x] = y;
            self.num[y] += self.num[x];
            self.num[x] = 0;
        }

        /*
        if self.rank[x] < self.rank[y] {
            self.par[x] = y;
        } else {
            self.par[y] = x;
            if self.rank[x] == self.rank[y] {
                self.rank[x] += 1;
            }
        }
        */
    }
}

fn main() {
    let (n, m): (usize, usize) = util::get2();
    let ab: Vec<(usize, usize)> = (0..m).map(|_| util::get2()).collect();

    let mut uft = UFT::new(n + 1);
    for &(a, b) in &ab {
        uft.unite(a, b);
    }

    for i in 1..n + 1 {
        println!("{}", uft.find(i));
    }
}
0