結果
| 問題 |
No.1293 2種類の道路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-11-23 20:29:04 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,962 bytes |
| コンパイル時間 | 18,145 ms |
| コンパイル使用メモリ | 396,576 KB |
| 実行使用メモリ | 637,928 KB |
| 最終ジャッジ日時 | 2024-09-25 01:13:53 |
| 合計ジャッジ時間 | 21,108 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 7 MLE * 1 -- * 14 |
コンパイルメッセージ
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
warning: method `chain_cnt` is never used
--> src/main.rs:48:8
|
11 | impl UnionFind {
| -------------- method in this implementation
...
48 | fn chain_cnt(&mut self, i: usize) -> usize {
| ^^^^^^^^^
ソースコード
use std::collections::{HashSet, HashMap};
struct UnionFind {
n: usize,
parents: Vec<usize>,
depths: Vec<usize>,
chains: Vec<usize>,
}
impl UnionFind {
fn new(n: usize) -> Self {
UnionFind {
n: n,
parents: (0..n).collect(),
depths: vec![0; n],
chains: vec![1; n],
}
}
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 x = self.parents[a];
let y = self.parents[b];
if self.depths[x] > self.depths[y] {
self.parents[x] = self.parents[y];
self.chains[y] += self.chains[x];
} else {
self.parents[y] = self.parents[x];
self.chains[x] += self.chains[y];
if self.depths[x] == self.depths[y] {
self.depths[x] += 1;
}
}
}
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 chain_cnt(&mut self, i: usize) -> usize {
let idx = self.find(i);
self.chains[idx]
}
}
fn main() {
let mut ndw = String::new();
std::io::stdin().read_line(&mut ndw).ok();
let ndw: Vec<usize> = ndw.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let n = ndw[0];
let d = ndw[1];
let w = ndw[2];
let mut cuf = UnionFind::new(n);
let mut wuf = UnionFind::new(n);
for _ in 0..d {
let mut ab = String::new();
std::io::stdin().read_line(&mut ab).ok();
let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let a = ab[0]-1;
let b = ab[1]-1;
cuf.unite(a, b);
}
for _ in 0..w {
let mut ab = String::new();
std::io::stdin().read_line(&mut ab).ok();
let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let a = ab[0]-1;
let b = ab[1]-1;
wuf.unite(a, b);
}
let mut wchildren = vec![HashSet::new(); n];
for i in 0..n {
wchildren[wuf.find(i)].insert(i);
}
let mut children = vec![HashSet::new(); n];
let mut used_map: HashMap<usize, HashSet<usize>> = HashMap::new();
for i in 0..n {
let parent = cuf.find(i);
children[parent].insert(i);
let cparent = wuf.find(i);
if used_map.contains_key(&parent) && used_map.get(&parent).unwrap().contains(&cparent) {
continue;
}
used_map.entry(parent).or_insert(HashSet::new()).insert(cparent);
for &v in wchildren[cparent].iter() {
children[parent].insert(v);
}
}
let mut result = 0usize;
for i in 0..n {
result += children[cuf.find(i)].len();
}
println!("{}", result-n);
}