/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 516.0 KiB
#2 Wrong Answer 2ms 532.0 KiB
#3 Time Exceeded ≥1099ms ≥107.332 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()
{
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    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;
    priority_queue<vector<int>> pq;
    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);

    long long ans = 0;
    int nodes[n + 1] = {0};
    int total = 0;

    while (total < n) {
        vector<int> x = pq.top();
        pq.pop();
        int cost = x[0], u = x[1], v = x[2];

        nodes[u]++; if (nodes[u] == 1) ++total;
        nodes[v]++; if (nodes[v] == 1) ++total;

        if (valid(u, v, total - 2)) {
            ans += cost;
        } else {
            nodes[u]--; if (nodes[u] == 0) --total;
            nodes[v]--; if (nodes[v] == 0) --total;
        }
    }

    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 00:24:40
Judged At
2024-11-11 02:52:59
Judged By
Score
10
Total Time
≥1099ms
Peak Memory
≥107.332 MiB