結果
問題 | No.2563 色ごとのグループ |
ユーザー | naut3 |
提出日時 | 2023-12-02 20:28:37 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 59 ms / 2,000 ms |
コード長 | 4,465 bytes |
コンパイル時間 | 14,314 ms |
コンパイル使用メモリ | 377,852 KB |
実行使用メモリ | 11,136 KB |
最終ジャッジ日時 | 2024-09-26 21:38:34 |
合計ジャッジ時間 | 14,131 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | AC | 1 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 1 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 | 1 ms
5,376 KB |
testcase_16 | AC | 1 ms
5,376 KB |
testcase_17 | AC | 1 ms
5,376 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 1 ms
5,376 KB |
testcase_20 | AC | 3 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 20 ms
5,504 KB |
testcase_25 | AC | 17 ms
6,016 KB |
testcase_26 | AC | 27 ms
8,064 KB |
testcase_27 | AC | 27 ms
10,624 KB |
testcase_28 | AC | 31 ms
8,704 KB |
testcase_29 | AC | 45 ms
11,008 KB |
testcase_30 | AC | 45 ms
11,008 KB |
testcase_31 | AC | 43 ms
11,008 KB |
testcase_32 | AC | 44 ms
11,008 KB |
testcase_33 | AC | 59 ms
11,136 KB |
testcase_34 | AC | 56 ms
11,008 KB |
testcase_35 | AC | 57 ms
11,008 KB |
testcase_36 | AC | 57 ms
11,136 KB |
testcase_37 | AC | 56 ms
11,136 KB |
ソースコード
#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>() }; } let N = input!(usize); let M = input!(usize); let C = input!(usize, N); let mut uf = UnionFind::new(N); let mut roots = vec![1 << 30; N + 1]; for i in 0..N { if roots[C[i]] == 1 << 30 { roots[C[i]] = i; } } for _ in 0..M { let (u, v) = (input!(usize) - 1, input!(usize) - 1); if C[u] == C[v] { uf.unite(u, v); } } let mut ans = 0_usize; for i in 0..N { if !uf.issame(roots[C[i]], i) { uf.unite(roots[C[i]], i); ans += 1; } } writeln!(out, "{}", ans); } #[derive(Clone)] pub struct UnionFind { size: usize, par: Vec<usize>, rank: Vec<usize>, cnt: Vec<usize>, } impl UnionFind { /// self = {0}, {1}, ..., {size - 1} pub fn new(size: usize) -> Self { return Self { size: size, par: vec![size; size], rank: vec![0; size], cnt: vec![1; size], }; } /// check whether set s1 (∋ a) and set s2 (∋ b) are equal pub fn issame(&mut self, a: usize, b: usize) -> bool { assert!(a < self.size && b < self.size); return self.root(a) == self.root(b); } /// unite set s1 (∋ a) and set s2 (∋ b) pub fn unite(&mut self, mut a: usize, mut b: usize) { a = self.root(a); b = self.root(b); if a != b { if self.rank[a] < self.rank[b] { std::mem::swap(&mut a, &mut b); } self.par[b] = a; if self.rank[a] == self.rank[b] { self.rank[a] += 1; } self.cnt[a] += self.cnt[b]; } } /// get the size of set s1 (∋ a) pub fn size(&mut self, x: usize) -> usize { let r = self.root(x); return self.cnt[r]; } fn root(&mut self, x: usize) -> usize { if self.par[x] == self.size { return x; } else { self.par[x] = self.root(self.par[x]); return self.par[x]; } } } impl std::fmt::Display for UnionFind { #[allow(unused_must_use)] fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut uf = self.clone(); let roots = (0..uf.size).map(|i| uf.root(i)).collect::<Vec<_>>(); let set = { let mut s = std::collections::BTreeSet::new(); for &r in roots.iter() { s.insert(r); } s }; let cc = { let mut m = std::collections::BTreeMap::new(); for (i, &r) in set.iter().enumerate() { m.insert(r, i); } m }; let mut ret = vec![vec![]; cc.len()]; for i in 0..uf.size { ret[*cc.get(&roots[i]).unwrap()].push(i); } for r in ret { write!( f, "{{{}}} ", r.iter() .map(|x| x.to_string()) .collect::<Vec<_>>() .join(" ") ); } return Ok(()); } } struct Scanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: str::SplitWhitespace<'static>, } impl<R: BufRead> Scanner<R> { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }