/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 2ms 600.0 KiB
#3 Wrong Answer 1461ms 107.781 MiB

Code

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

int degree[2003],two=0;

bool valid(int u,int v,int lim)
{
    if(degree[u]==2 || degree[v]==2) return false;
    int c =0;
    if(degree[u]==1) ++c;
    if(degree[v]==1) ++c;
    if(two + c > lim) return false;
    two += c;
    degree[u]++;
    degree[v]++;
    return true;
}

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

int main()
{
    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_back({cost,i,j});
        }
    }
    sort(pq.begin(),pq.end(),cmp);

    int need = n-2;
    vector<int> x = pq[0];
    long long ans = x[0];
    int nodes[n+1]={0};
    nodes[x[1]]++;
    nodes[x[2]]++;
    int ind=1,total = 2;

    while(total != n)
    {
        x = pq[ind++];
        nodes[x[1]]++; if(nodes[x[1]]==1) ++total;
        nodes[x[2]]++; if(nodes[x[2]]==1) ++total;

        if(valid(x[1],x[2],total-2))
        {
            ans += x[0];
        }
        else
        {
            nodes[x[1]]--; if(nodes[x[1]]==0) --total;
            nodes[x[2]]--; if(nodes[x[2]]==0) --total;
        }
    }

    cout << ans << endl;
}

Information

Submit By
Type
Submission
Problem
P1098 KuZ the Maximum spanning tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-24 15:52:37
Judged At
2024-12-03 05:00:43
Judged By
Score
1
Total Time
1461ms
Peak Memory
107.781 MiB