結果
問題 |
No.1054 Union add query
|
ユーザー |
![]() |
提出日時 | 2021-03-13 02:30:56 |
言語 | Rust (1.83.0 + proconio) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,549 bytes |
コンパイル時間 | 23,859 ms |
コンパイル使用メモリ | 377,936 KB |
実行使用メモリ | 7,904 KB |
最終ジャッジ日時 | 2024-10-14 15:18:37 |
合計ジャッジ時間 | 15,876 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 1 RE * 7 |
ソースコード
#[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") } fn root (par:&Vec<usize>, mut x:usize) -> usize { while par[x] != x { x = par[x]; } x } fn main(){ let n:usize = read(); let query:usize = read(); let mut par:Vec<usize> = (0..n).collect(); let mut sum = vec![0i64;n]; let mut sz = vec![1;n]; for _ in 0..query { let t:usize = read(); let mut a:usize = read(); let mut b:usize = read(); if t == 1 { a -= 1; b -= 1; a = root(&par,a); b = root(&par,b); if a != b { if sz[a] < sz[b] { std::mem::swap(&mut a,&mut b); } sz[a] += sz[b]; sum[b] -= sum[a]; par[b] = a; } }else if t == 2 { a -= 1; a = root(&par,a); sum[a] += b as i64; }else { a -= 1; let mut x = a; let mut ans = sum[x]; while x != par[x] { x = par[x]; ans += sum[x]; } println!("{}",ans); } } }