#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define re register
#define ri register int
#define For(i,t,n) for(ri i = t; i <= n; i++)
#define dFor(i,t,n) for(ri i = t; i >= n; i--)
#define syncoff ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
//#define int LL
const int Inf = 0x3f3f3f3f;
const int memInf = 0x3f;
inline int read() {
ri x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x<<3)+(x<<1) + (ch^48);
ch = getchar();
}
return x * f;
}
inline void read(int &x) {
x = 0; ri f = 1, ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x<<3)+(x<<1) + (ch^48);
ch = getchar();
}
x*=f;
}
int n,m,p;
struct uset{
int fa[5005];
// int init(int n=5000){
// For(i,0,n) fa[i]=i;
// }
int get(int x){
if(fa[x]==x) return x;
return fa[x]=get(fa[x]);
}
inline bool same(const int& x,const int& y){
return (get(x)==get(y));
}
void merge(const int& x,const int& y){
ri a=get(x),b=get(y);
fa[b]=a;
}
uset(int n=5000){
//init();
For(i,0,n) fa[i]=i;
}
};
uset u;
void input(){
read(n); read(m); read(p);
For(i,1,m){
ri a=read(),b=read();
u.merge(a,b);
}
}
void solve(){
input();
while(p--){
ri a=read(),b=read();
if(u.same(a,b)) printf("Yes\n");
else printf("No\n");
}
}
signed main()
{
solve();
return 0;
}
如果使用注释掉的 init() 函数进行初始化就会 RE ,求大佬解答。