关于并查集板子
#include
using namespace std;
#define int long long
#define endl '\n'
int n,m,p;
const int N=1e5;
int fa[N];
void init(int n)
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int find(int x)
{
if(fa[x]==x)return x;
int t=find(fa[x]);
fa[x]=t;
return fa[x];
}
void merge(int a,int b)
{
int x=find(a),y=find(b);
if(x==y)return;
fa[x]=y;
}
signed main()
{
std::ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
cin>>n>>m>>p;
init(n);
while(m--)
{
int a,b;
cin>>a>>b;
merge(a,b);
}
while(p--)
{
int a,b;
cin>>a>>b;
if(find(a)!=find(b))
cout<<"No"< else
cout<<"Yes"< }
return 0;
}
using namespace std;
#define int long long
#define endl '\n'
int n,m,p;
const int N=1e5;
int fa[N];
void init(int n)
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int find(int x)
{
if(fa[x]==x)return x;
int t=find(fa[x]);
fa[x]=t;
return fa[x];
}
void merge(int a,int b)
{
int x=find(a),y=find(b);
if(x==y)return;
fa[x]=y;
}
signed main()
{
std::ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
cin>>n>>m>>p;
init(n);
while(m--)
{
int a,b;
cin>>a>>b;
merge(a,b);
}
while(p--)
{
int a,b;
cin>>a>>b;
if(find(a)!=find(b))
cout<<"No"<
cout<<"Yes"<
return 0;
}
全部评论
相关推荐