(problem)
并查集例题
可先去看 并查集
#include <bits/stdc++.h>
using namespace std ;
typedef long long LL ;
inline LL read() { LL x(0) ; register char c=getchar() ;
while(!isdigit(c)) c=getchar() ;
while(isdigit(c)) x=(x<<1)+(x<<3)+(c&15) , c=getchar() ; return x ;
}
int n , m , p ;
int fa[5000+10] ;
inline int find(int x) {
return fa[x] == x ? fa[x] : fa[x] = find(fa[x]) ;
}
inline void merge(int x,int y) {
fa[find(x)] = find(y) ;
}
signed main() {
n = read() ;
m = read() ;
p = read() ;
for(register int i=1;i<=n;i++) fa[i] = i ;
for(register int i=1;i<=m;i++) {
int x = read() , y = read() ;
merge(x,y) ;
}
for(register int i=1;i<=p;i++) {
int x = read() , y = read() ;
puts(find(x)==find(y)?"Yes":"No") ;
}
return 0 ;
}