結果
問題 | No.483 マッチ並べ |
ユーザー |
|
提出日時 | 2023-01-12 20:11:35 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,166 bytes |
コンパイル時間 | 12,852 ms |
コンパイル使用メモリ | 403,344 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-23 13:18:59 |
合計ジャッジ時間 | 14,943 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
コンパイルメッセージ
warning: field `n` is never read --> src/main.rs:5:5 | 4 | struct UnionFind { | --------- field in this struct 5 | n: usize, | ^ | = note: `#[warn(dead_code)]` on by default
ソースコード
const SIZE: usize = 1e4 as usize;struct UnionFind {n: usize,parents: Vec<usize>,}impl UnionFind {fn new(n: usize) -> Self {UnionFind {n: n,parents: (0..n).collect(),}}fn equiv(&mut self, a: usize, b: usize) -> bool {self.find(a) == self.find(b)}fn unite(&mut self, a: usize, b: usize) {if self.equiv(a, b) { return; }let (a, b) = (a.min(b), a.max(b));let x = self.parents[a];let y = self.parents[b];self.parents[y] = self.parents[x];}fn find(&mut self, a: usize) -> usize {if self.parents[a] == a { return a; }let p = self.find(self.parents[a]);self.parents[a] = p;p}}fn main() {let mut n = String::new();std::io::stdin().read_line(&mut n).ok();let n: usize = n.trim().parse().unwrap();let items = (0..n).map(|_| {let mut temp = String::new();std::io::stdin().read_line(&mut temp).ok();let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();(temp[0].min(temp[2]), temp[1].min(temp[3]), temp[0].max(temp[2]), temp[1].max(temp[3]))}).collect::<Vec<_>>();let mut uf = UnionFind::new(SIZE);let mut size = vec![1usize; SIZE];let mut linecnt = vec![0usize; SIZE];for &(r0, c0, r1, c1) in items.iter() {let idx0 = (r0-1) * 100 + (c0-1);let idx1 = (r1-1) * 100 + (c1-1);let p0 = uf.find(idx0);let p1 = uf.find(idx1);if p0 == p1 {linecnt[p0] += 1;continue;}uf.unite(p0, p1);let p = uf.find(p0);if p == p0 {size[p0] += size[p1];size[p1] = 0;linecnt[p0] += linecnt[p1]+1;linecnt[p1] = 0;} else {size[p1] += size[p0];size[p0] = 0;linecnt[p1] += linecnt[p0]+1;linecnt[p0] = 0;}}if (0..SIZE).any(|i| size[i] < linecnt[i]) {println!("NO");} else {println!("YES");}}