关于并查集板子

#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<<&quot;No&quot;<<endl;
else
cout<<&quot;Yes&quot;<<endl;
}
return  0;
}
全部评论

相关推荐

牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务