use std::collections::VecDeque; use std::mem::swap; use std::io::{self, BufRead}; //use proconio::input; struct UnionFind{ n: usize, parent: Vec, num: Vec, } impl UnionFind{ fn new(n: usize)->Self{ let mut parent = Vec::new(); for i in 0..n{ parent.push(i); } let num = vec![1; n]; UnionFind{ n, parent, num, } } fn find(&mut self, x: usize)->usize{ if x==self.parent[x]{return x;} self.parent[x] = self.find(self.parent[x]); self.parent[x] } fn union(&mut self, u: usize, v: usize){ let (mut pu, mut pv) = (self.find(u), self.find(v)); if pu==pv{return;} if self.num[pu] < self.num[pv]{ swap(&mut pu, &mut pv); } self.num[pu] += self.num[pv]; self.parent[pv] = pu; } fn same(&mut self, u: usize, v: usize)->bool{ self.find(u)==self.find(v) } } fn main(){ //input! { // n: usize, m: usize, q: usize, // e: [(usize, usize); m], // query: [(usize, usize); q] //} //ここからの入力はgptです…やりたいことはただ以上のようなものです。 let stdin = io::stdin(); let mut iterator = stdin.lock().lines(); let first_line = iterator.next().unwrap().unwrap(); let mut iter = first_line.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let m: usize = iter.next().unwrap().parse().unwrap(); let q: usize = iter.next().unwrap().parse().unwrap(); let mut e = Vec::with_capacity(m); for _ in 0..m { let line = iterator.next().unwrap().unwrap(); let mut iter = line.split_whitespace(); let u: usize = iter.next().unwrap().parse().unwrap(); let v: usize = iter.next().unwrap().parse().unwrap(); e.push((u, v)); } let mut query = Vec::with_capacity(q); for _ in 0..q { let line = iterator.next().unwrap().unwrap(); let mut iter = line.split_whitespace(); let u: usize = iter.next().unwrap().parse().unwrap(); let v: usize = iter.next().unwrap().parse().unwrap(); query.push((u, v)); } //↑ここまでの入力はgptです… let mut uf = UnionFind::new(n); let mut edge = vec![Vec::new(); n]; let mut cnt = vec![0; n]; for &(u, v) in &e{ edge[u-1].push(v-1); edge[v-1].push(u-1); cnt[u-1] += 1; cnt[v-1] += 1; } let mut stack = VecDeque::new(); let mut used = vec![false; n]; for i in 0..n{ if cnt[i] <= 1{ stack.push_back(i); } } while let Some(p) = stack.pop_front(){ if used[p]{continue} used[p] = true; for &nex in &edge[p]{ if used[nex]{continue} cnt[nex] -= 1; uf.union(p, nex); if cnt[nex] <= 1{ stack.push_back(nex); } } } for &(u, v) in &query{ if uf.same(u-1, v-1){ println!("Yes"); } else { println!("No"); } } }