kuangbin专题打卡活动之并查集

4266. 无线网络

4266
考点:合并和查询

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1010;

int n, m, d;
int x[N], y[N];
int p[N];
bool g[N][N], st[N];//g是判断距离,st是判断开没开机

int find(int x)  // 并查集
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

bool check(int a,int b)
{
    int dx = abs(x[a] - x[b]);
    int dy = abs(y[a] - y[b]);
    if (dx * dx + dy * dy <= d * d) return true;
    return false;
}

int main()
{
    cin>>n>>d;
    for(int i=1;i<=n;i++)
       cin>>x[i]>>y[i];
     
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= n; j ++ )
            if (check(i, j))
                g[i][j] = true;
      
    for(int i=0;i<=n;i++)p[i]=i;
    char op;
    int a, b;
    while (cin >> op)
    {
        if (op == 'O') // 最多开机 n 次,时间复杂度 O(n * n + m)
        {
            scanf("%d", &a);
            for (int i = 1; i <= n; i ++ )
                if (st[i] && g[i][a])//st[i]==true,说明开机了
                {
                    int fa = find(a), fb = find(i);//合并
                    p[fa] = fb;
                }

            st[a] = true;
        }
        else
        {
            scanf("%d%d", &a, &b);
            int fa = find(a);
            int fb = find(b);

            if (fa == fb) cout << "SUCCESS" << endl;//查询
            else cout << "FAIL" << endl;
        }
    }

    return 0;
}

4267. 可疑人员

4267
考点:合并

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N=3e5+10;

int a[N],p[N];
int n,m,k;
int sizee[N];

int find(int x)  // 并查集
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    while(cin>>n>>m,n||m)
    {
        
        for(int i=0;i<n;i++)
           p[i]=i,sizee[i]=1;
           
        for(int i=1;i<=m;i++)//合并
        {
            int k;
            cin>>k;
            if(k==0)continue;
            int x,y;
            cin>>x;//把每个社团的第一个人读进来
            for(int j=2;j<=k;j++)
            {
                cin>>y;
                int px=find(x),py=find(y);
                if(px!=py)
                {
                   
                    sizee[py] += sizee[px];
                    p[px]=py;
                }
            }
        }
        int res=0;
        cout<<sizee[find(0)]<<endl;
    }
    return 0;
}
//维护每个集合中的元素个数
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值