/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 2ms 508.0 KiB
#3 Accepted 544ms 107.676 MiB
#4 Accepted 550ms 108.0 MiB
#5 Accepted 540ms 107.766 MiB
#6 Accepted 553ms 107.82 MiB
#7 Accepted 536ms 107.82 MiB
#8 Accepted 544ms 107.82 MiB
#9 Accepted 550ms 107.816 MiB
#10 Accepted 543ms 107.816 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;

struct DSU{
    int par[2005],sz[2005];
    void create(int u)
    {
        par[u] = u;
        sz[u]=1;
    }
    int findpar(int u)
    {
        if(par[u]==u) return u;
        return par[u] = findpar(par[u]);
    }
    void Union(int u,int v)
    {
        int a = findpar(u);
        int b = findpar(v);
        if(sz[a] < sz[b]) swap(a,b);
        par[b] = a;
        sz[a] += b;
    }
}dsu;

bool cmp(vector<int>&a,vector<int>&b) {return a>b;}

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n,k; cin >> n >> k;
    int a[n][k];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<k;j++) cin >> a[i][j];
    }
    if(n==1)
    {
        cout<<0<<endl;
        return 0;
    }

    priority_queue<vector<int>>pq;
    //vector<vector<int>> pq(0,vector<int>(3));
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            int cost = 0;
            for(int m=0;m<k;m++) cost += abs(a[i][m]-a[j][m]);
            pq.push({cost,i,j});
        }
    }
    //sort(pq.begin(),pq.end(),cmp);

    int need = n-2;
    vector<int> x = pq.top(); pq.pop();
    long long ans = x[0];
    int taken[n+1]={0};
    for(int i=1;i<=n;i++) dsu.create(i);
    dsu.Union(x[1],x[2]);
    taken[x[1]]=1;
    taken[x[2]]=1;
    int edge = 1,ind = 1;

    while(edge != n-1)
    {
        x = pq.top(); pq.pop();
        if(taken[x[1]] && taken[x[2]] && dsu.findpar(x[1])==dsu.findpar(x[2])) continue;

        taken[x[1]]=1; taken[x[2]]=1;
        dsu.Union(x[1],x[2]);
        ++edge;
        ans += x[0];
    }

    cout << ans << endl;
}

Information

Submit By
Type
Submission
Problem
P1097 KuZ the Maximum spanning tree (Easy Version)
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-25 14:19:16
Judged At
2024-12-03 05:03:02
Judged By
Score
100
Total Time
553ms
Peak Memory
108.0 MiB