use std::collections::HashSet; struct UnionFind { n: usize, parents: Vec, depths: Vec, chains: Vec, } 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 merge(a: usize, b: usize, uf: &mut UnionFind, size: &mut Vec) { let pa = uf.find(a); let pb = uf.find(b); if pa == pb { return; } uf.unite(pa, pb); let p = uf.find(pa); if p == pa { size[pa] += size[pb]; size[pb] = 0; } else { size[pb] += size[pa]; size[pa] = 0; } } fn main() { let mut ndw = String::new(); std::io::stdin().read_line(&mut ndw).ok(); let ndw: Vec = 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); let mut wsize = vec![1usize; n]; for _ in 0..d { let mut ab = String::new(); std::io::stdin().read_line(&mut ab).ok(); let ab: Vec = 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 = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = ab[0]-1; let b = ab[1]-1; merge(a, b, &mut wuf,&mut wsize); } let mut children = vec![vec![]; n]; for i in 0..n { children[cuf.find(i)].push(i); } let mut result_map = vec![0usize; n]; for i in 0..n { let parent = cuf.find(i); if i != parent { continue; } let mut temp = HashSet::new(); for &v in children[i].iter() { temp.insert(wuf.find(v)); } result_map[i] = temp.iter().map(|&v| wsize[v]).sum::(); } let mut result = 0usize; for i in 0..n { result += result_map[cuf.find(i)]; } println!("{}", result-n); }