結果

問題 No.1054 Union add query
ユーザー uw_yu1rabbituw_yu1rabbit
提出日時 2021-03-13 01:30:17
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 2,886 bytes
コンパイル時間 23,037 ms
コンパイル使用メモリ 400,560 KB
実行使用メモリ 60,460 KB
最終ジャッジ日時 2024-10-14 15:05:39
合計ジャッジ時間 23,242 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,636 KB
testcase_01 WA -
testcase_02 AC 1 ms
6,820 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable does not need to be mutable
  --> src/main.rs:71:13
   |
71 |         let mut a:usize = read();
   |             ----^
   |             |
   |             help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

warning: variable does not need to be mutable
  --> src/main.rs:72:13
   |
72 |         let mut b:usize = read();
   |             ----^
   |             |
   |             help: remove this `mut`

warning: variable does not need to be mutable
  --> src/main.rs:88:21
   |
88 |                 let mut r = uf.root(a - 1);
   |                     ----^
   |                     |
   |                     help: remove this `mut`

ソースコード

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
    }
}
use std::collections::HashSet;
fn main(){
    let n:usize = read();
    let query:usize = read();
    let mut uf = UnionFind::new(n);
    let mut p = vec![0;n];
    let mut q = vec![0;n];
    let mut rec = vec![HashSet::new();n];
    for i in 0..n {
        rec[i].insert(i);
    }
    for _ in 0..query {
        let t:usize = read();
        let mut a:usize = read();
        let mut b:usize = read();
        if t == 1 {
            let sz1 = uf.size(a - 1);
            let sz2 = uf.size(b - 1);
            uf.merge(a - 1,b - 1);
            if sz1 < sz2 {
                let mut nov = replace(&mut rec[b],HashSet::new());
                let r = uf.root(b - 1);
                for &e in &rec[a] {
                    nov.insert(e);
                    q[e] += p[e] - p[r as usize];
                }
                rec[b] = nov;
                rec[a] = HashSet::new();
            }else {
                let mut nov = replace(&mut rec[a - 1],HashSet::new());
                let mut r = uf.root(a - 1);
                for &e in &rec[b - 1] {
                    nov.insert(e);
                    q[e] += p[e] - p[r as usize];
                }
                rec[a - 1] = nov;
                rec[b - 1] = HashSet::new();
            }
        }else if t == 2 {
            p[uf.root(a - 1) as usize] += b as i64;
        }else{
            println!("{}",p[uf.root(a - 1) as usize] + q[a - 1]);
        }
    }
}
0