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;
}



猜你喜欢

转载自blog.csdn.net/weixin_45509601/article/details/119173350
今日推荐