数据结构09 836. 合并集合

数据结构09 836. 合并集合,第1张

文章目录
  • 原题题目
  • 代码实现

原题题目


代码实现
#include
using namespace std;

const int N=100010;
int n,m;
int p[N];

int find(int x)
{
    if(p[x]!=x)p[x]=find(p[x]);
    return p[x];
}

int main()
{
    cin >> n>> m;
    
    for(int i=1;i<=n;i++)p[i]=i;
    while(m--)
    {
        char op[2];
        
        int a,b;
        cin >> op >> a>> b;
        if(op[0]=='M')p[find(a)] = find(b);
        else 
        {
            if(find(a)==find(b)) puts("Yes");
            else puts("No");
        }
    }
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/713677.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存