关于并查集板子
#include<bits/stdc++.h>
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"<<endl;
else
cout<<"Yes"<<endl;
}
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"<<endl;
else
cout<<"Yes"<<endl;
}
return 0;
}
全部评论
相关推荐
点赞 评论 收藏
分享
2025-11-23 20:47
中国地质大学(武汉) Java
程序员牛肉:继续沉淀吧同学,你这就是纯纯的流水线产品。
差不多的学历+两个烂大街项目。自身学历又不行,现在找啥实习呢。有点太浮躁了。多花点心思搞搞ai,开源和八股。这比你这段时间捣鼓一段小厂实习要好得多; 点赞 评论 收藏
分享
点赞 评论 收藏
分享
