結果

問題 No.1390 Get together
ユーザー uw_yu1rabbituw_yu1rabbit
提出日時 2021-03-14 15:03:07
言語 Rust
(1.77.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 2,072 bytes
コンパイル時間 1,520 ms
コンパイル使用メモリ 172,788 KB
実行使用メモリ 17,236 KB
最終ジャッジ日時 2024-04-23 22:43:57
合計ジャッジ時間 4,564 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 0 ms
5,376 KB
testcase_16 AC 44 ms
12,800 KB
testcase_17 AC 59 ms
17,024 KB
testcase_18 AC 41 ms
12,920 KB
testcase_19 AC 63 ms
17,152 KB
testcase_20 AC 63 ms
17,068 KB
testcase_21 AC 69 ms
17,008 KB
testcase_22 AC 57 ms
15,744 KB
testcase_23 AC 55 ms
15,616 KB
testcase_24 AC 54 ms
15,744 KB
testcase_25 AC 64 ms
17,236 KB
testcase_26 AC 62 ms
17,152 KB
testcase_27 AC 67 ms
17,152 KB
testcase_28 AC 63 ms
17,152 KB
testcase_29 AC 64 ms
17,008 KB
testcase_30 AC 60 ms
17,152 KB
testcase_31 AC 67 ms
17,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(dead_code)]
#[allow(unused_imports)]
fn read<T: std::str::FromStr>() -> T {
    use std::io::*;
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}
use std::mem::*;
struct UnionFind {
    par: Vec<i64>,
}
#[allow(dead_code)] 
impl UnionFind {

    pub fn new(n: usize) -> Self {
        let par = (0..n).map(|_| -1).collect();
        Self{par}
    }

    pub fn root(&mut self, x: usize) -> i64 {
        if self.par[x] < 0{
            x as i64
        }else{
            self.par[x] = self.root(self.par[x] as usize);
            self.par[x]
        }
    }

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

    pub fn merge(&mut self, x: usize, y: usize) -> bool {
        let mut _x: i64 = self.root(x);
        let mut _y: i64 = self.root(y);
        if _x == _y {
            return false;
        }
        if self.par[_x as usize] > self.par[_y as usize] {
            swap(&mut _x, &mut _y);
        }
        self.par[_x as usize] += self.par[_y as usize];
        self.par[_y as usize] = _x as i64;
        return true;
    }
    pub fn size(&mut self, x: usize) -> i64 {
        (self.par[x]) as i64 * -1
    }
}
fn main(){
    let n:usize = read();
    let m:usize = read();
    let bc:Vec<(usize,usize)> = (0..n).map(|_| (read(),read())).collect();
    let mut rec = vec![Vec::new();n];
    let mut uf = UnionFind::new(m);
    for i in 0..n {
        rec[bc[i].1 - 1].push(bc[i].0 - 1);
    }
    let mut ans = 0;
    for i in 0..n {
        if rec[i].len() == 0 {
            continue;
        }
        let key = rec[i][0];
        for j in 0..rec[i].len() {
            if !uf.same(key,rec[i][j]) {
                ans += 1;
                uf.merge(key,rec[i][j]);
            }
        }
    }
    println!("{}",ans);
}
0