Black and white -2021牛客暑期多校训练营3

经过仔细地观察题目,我们可以发现当我们取矩阵中任意一个点时,我们都会得到这一列和这一行的贡献,故能够想到把每次获得的行列加入一个集合,若集合中包含了所有的行列说明我们一定可以构建出这个矩阵。
下面是克鲁斯卡尔的代码

#include "bits/stdc++.h"
//#define int long long
using namespace std;
const int N = 6e3+10;

int fa[N<<1];
struct node{
    int x,y,v;
} f[N*N];
int find(int x)
{
    if (x == fa[x]) return x;
    return fa[x] = find(fa[x]);
}
signed main()
{
    ios::sync_with_stdio(false);
    int n,m,a,b,c,d,p;
    cin >> n >> m >> a >> b >> c >> d >> p;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            f[(i-1)*m+j] = node{i,j,(a * a * b + a * c + d)% p};
            a = (a * a * b + a * c + d)% p;
        }
    }
    sort(f+1,f+1+n*m,[&](node a1,node a2){
        return a1.v < a2.v;
    });
    for (int i = 1; i <= n+m; ++i) {
        fa[i] = i;
    }
    int res = 0;
    for(int i = 1;i <= n*m ;i++)
    {
        int x = find(f[i].x),y = find(n+f[i].y);
        if(x != y)
        {
            fa[y]  = x;
            res += f[i].v;
        }
    }
    cout << res << endl;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值