CodeForces 893C

这个得先了解:并查集

并查集板子:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int m=100008;
/*
规则:
    1.初始化、合并(谁当谁领导问题)、查询
注意:
    1.每棵树记录树高
    2.两棵树树高不同,树高小的连向树高大的
    3.压缩路径,走向一次根节点就把这个点改
      成直接连向根(此过程中经过的节点也会变成一样的根)
    4.为了简化:即使树高变化也不改变树高
    5.i:i是代表的某个元素
      x:x是这个元素的根
      pre[i]=x:i的根是x
*/


/*          板子          */

const int  N=1005;//指定并查集所能包含元素的个数(由题意决定)
int pre[N];  //存储每个结点的前驱结点
int rk[N]; //树的高度

//初始化
void init(int n)
{
    for(int i = 0; i < n; i++)
    {
        pre[i] = i;     //每个结点的上级都是自己
        rk[i] = 1;   	//每个结点构成的树的高度为 1
    }
}

//查找
int find(int x)
/*改进查找算法:完成路径压缩,将 x的上级
直接变为根结点,那么树的高度就会大大降低
*/
{
    if(pre[x] == x)
        return x;		//递归出口:x的上级为 x本身,即 x为根结点
    return pre[x] = find(pre[x]);   //此代码相当于先找到根结点 rootx,然后 pre[x]=rootx
}

//合并
    //join(x,y)的执行逻辑如下:
    //1、寻找 x 的代表元(即教主);
    //2、寻找 y 的代表元(即教主);
    //3、如果 x 和 y 不相等,则随便选一个人作为另一个人的上级,如此一来就完成了x和y的合并.
bool join(int x,int y)
{
    x = find(x);//寻找 x的代表元
    y = find(y);//寻找 y的代表元
    if(x == y)
        return false;			//如果 x和 y的代表元一致,说明他们共属同一集合,则不需要合并,返回 false,表示合并失败;否则,执行下面的逻辑
    if(rk[x] > rk[y])
        pre[y]=x;		//如果 x的高度大于 y,则令 y的上级为 x
    else								//否则
    {
        if(rk[x]==rk[y])
            rk[y]++;	//如果 x的高度和 y的高度相同,则令 y的高度加1
        pre[x]=y;						//让 x的上级为 y
    }
    return true;						//返回 true,表示合并成功
}

//判断
bool isSame(int x, int y)      		//判断两个结点是否连通
{
    return find(x) == find(y);  	//判断两个结点的根结点(即代表元)是否相同
}
/*          板子          */


void sol()
{
    int n,m;
    cin>>n>>m;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    sol();
    return 0;
}

题解:实际上就是不断的通过函数找到他们的祖宗并且比较他们祖宗的权值(这一点需要注意吧我是比较输入的两个索引的权值大小。。。一直wa。。。),并根据情况将其合并

最后通过for循环查找他们的祖宗进行sum+祖宗(只能加一次(题意如此))

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
#define endl "\n"
#define yes cout<<"Yes\n";
#define no cout<<"No\n";

using namespace std;
const int M=2e5+10;
int a[M];
int v[M]; //提及过
int u[M]; //父节点用过
int f[M]; //父亲
int fd(int x)
{
    if(x!=f[x]) f[x]=fd(f[x]);
    return f[x];
}
void sol()
{
    int n;  cin>>n;
    int m;  cin>>m;

    for(int i=1;i<=n;i++)
    {
        cin>>a[i]; f[i]=i;
    }
    while(m--)
    {
        int x,y;
        cin>>x>>y;
        int fx=fd(x);
        int fy=fd(y);
        if(min(a[fx],a[fy])==a[fx])
        {
            f[fy]=fx;
        }
        else
        {
            f[fx]=fy;
        }
    }
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        int fi=fd(i);
        if(!u[fi])
        {
            u[fi]=1;
            sum+=a[fi];
        }
    }
    cout<<sum<<endl;
    return ;
}
signed main()
{
    ios;
    int t=1;
//    cin>>t;
    while(t--)
    {
        sol();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

活在当下 北乔

不爱多bb,朴实一句话:感谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值