結果
問題 | No.1054 Union add query |
ユーザー | ziita |
提出日時 | 2020-05-15 22:39:44 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 461 ms / 2,000 ms |
コード長 | 4,331 bytes |
コンパイル時間 | 12,430 ms |
コンパイル使用メモリ | 400,296 KB |
実行使用メモリ | 25,344 KB |
最終ジャッジ日時 | 2024-09-19 11:47:33 |
合計ジャッジ時間 | 16,400 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 341 ms
14,848 KB |
testcase_04 | AC | 345 ms
19,584 KB |
testcase_05 | AC | 339 ms
14,232 KB |
testcase_06 | AC | 307 ms
19,968 KB |
testcase_07 | AC | 289 ms
25,344 KB |
testcase_08 | AC | 203 ms
21,504 KB |
testcase_09 | AC | 461 ms
19,592 KB |
testcase_10 | AC | 76 ms
15,616 KB |
ソースコード
#![allow(unused_imports)] #![allow(non_snake_case, unused)] use std::cmp::*; use std::collections::*; use std::ops::*; // https://atcoder.jp/contests/hokudai-hitachi2019-1/submissions/10518254 macro_rules! eprint { ($($t:tt)*) => {{ use ::std::io::Write; let _ = write!(::std::io::stderr(), $($t)*); }}; } macro_rules! eprintln { () => { eprintln!(""); }; ($($t:tt)*) => {{ use ::std::io::Write; let _ = writeln!(::std::io::stderr(), $($t)*); }}; } macro_rules! dbg { ($v:expr) => {{ let val = $v; eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val); val }} } macro_rules! mat { ($($e:expr),*) => { Vec::from(vec![$($e),*]) }; ($($e:expr,)*) => { Vec::from(vec![$($e),*]) }; ($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) }; ($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) }; } macro_rules! ok { ($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => { $a$([$i])*.$f($($t),*) }; ($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { { let t = $e; ok!($a$([$i])*.$f($($es),*)$(@$t)*@t) } }; } pub fn readln() -> String { let mut line = String::new(); ::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e)); line } macro_rules! read { ($($t:tt),*; $n:expr) => {{ let stdin = ::std::io::stdin(); let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| { let line = line.unwrap(); let mut it = line.split_whitespace(); _read!(it; $($t),*) }).collect::<Vec<_>>(); ret }}; ($($t:tt),*) => {{ let line = readln(); let mut it = line.split_whitespace(); _read!(it; $($t),*) }}; } macro_rules! _read { ($it:ident; [char]) => { _read!($it; String).chars().collect::<Vec<_>>() }; ($it:ident; [u8]) => { Vec::from(_read!($it; String).into_bytes()) }; ($it:ident; usize1) => { $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1 }; ($it:ident; [usize1]) => { $it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>() }; ($it:ident; [$t:ty]) => { $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>() }; ($it:ident; $t:ty) => { $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e)) }; ($it:ident; $($t:tt),+) => { ($(_read!($it; $t)),*) }; } pub fn main() { let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join(); } const MOD: u32 = 1_000_000_007; const INF: i64 = std::i64::MAX/2; pub struct UnionFind { data: Vec<i32>, val: Vec<i64>, } impl UnionFind { pub fn new(n: usize) -> Self{ UnionFind { data: vec![-1;n], val: vec![0;n] } } pub fn root(&mut self, x: usize) -> (usize,i64){ if self.data[x]<0 { (x,self.val[x]) } else { let mut sum = 0; sum += self.val[x]; let p = self.data[x] as usize; let ret = self.root(p); sum += ret.1; self.data[x] = ret.0 as i32; self.val[x] = sum-self.val[ret.0]; (self.data[x] as usize, sum) } } pub fn unite(&mut self, x: usize, y: usize){ let rx = self.root(x).0; let ry = self.root(y).0; if rx!=ry { let (rx,ry) = if self.data[rx]>self.data[ry] { (rx,ry) } else{ (ry,rx) }; self.data[rx] += self.data[ry]; self.data[ry] = rx as i32; self.val[ry] -= self.val[rx]; } } pub fn same(&mut self, x: usize, y: usize) -> bool{ self.root(x).0 == self.root(y).0 } pub fn size(&mut self, x: usize) -> i32 { let r = self.root(x).0; - self.data[r] } pub fn add(&mut self, x: usize, v: i64) { let r = self.root(x).0; self.val[r] += v; } } fn solve() { let (n,q) = read!(usize,usize); let tab = read!(i64,i64,i64;q); let mut uf = UnionFind::new(n); for (t,a,b) in tab { if t==1 { let s = (a-1) as usize; let t = (b-1) as usize; uf.unite(s,t); } if t==2 { let s = (a-1) as usize; uf.add(s,b); } if t==3 { let s = (a-1) as usize; println!("{}", uf.root(s).1); } } } fn run() { solve(); }