/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 320.0 KiB
#2 Accepted 2ms 368.0 KiB
#3 Accepted 18ms 3.27 MiB
#4 Accepted 18ms 3.273 MiB
#5 Accepted 18ms 3.18 MiB
#6 Accepted 18ms 3.316 MiB
#7 Accepted 18ms 3.32 MiB
#8 Accepted 18ms 3.316 MiB
#9 Accepted 18ms 3.121 MiB
#10 Accepted 18ms 3.332 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
const int MX = 2e5 + 5 ;
ll par[MX], cnt, cost;
struct heart
{
    ll u, v, w , f , s;

};
bool compare(heart a, heart b)
{

    return a.w > b.w ;

}
vector < heart > edge ;
int find_par(ll id)
{
    return (par[id] == id)? id : par[id] = find_par(par[id]) ;
}
ll mst(ll n)
{
    sort(edge.begin(), edge.end(),compare) ;
    cost = 0, cnt = 0 ;
    for(int i = 0 ; i <= n ; i++)
    {
        par[i] = i ;
    }
    for(int i = 0 ; i< edge.size() ; i++)
    {

        ll u = find_par(edge[i].u) ;
        ll v = find_par(edge[i].v) ;
        if(u != v)
        {
            par[u] = v ;
            cost +=edge[i].w ;

        }
    }
    return cost ;
}
void solve()
{
    ll n, d ;
    cin >> n >> d ;
    ll arr[n + 5][d + 5] ;
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < d ; j++)
        {
            cin >> arr[i][j] ;
        }
    }
    heart dp[40] ;
    for(int i = 0 ; i < 40 ; i++)
    {
        dp[i].f = INT_MIN ;
        dp[i].s = -1 ;
    }
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < (1 << d) ; j++)
        {
            ll cur_cost = 0 ;
            for(int k = 0 ; k < d ; k++)
            {
                ll bit = (j & (1 << k)) ;
                cur_cost +=  (bit ?  arr[i][k] : -arr[i][k]) ;
            }
            if(cur_cost > dp[j].f)
            {
                dp[j].f = cur_cost ;
                dp[j].s = i ;
            }
        }
    }
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < (1 << d) ; j++)
        {
            ll cur_cost = 0 ;
            for(int k = 0 ; k < d ; k++)
            {
                int bit = (j & (1 << k)) ;
               cur_cost +=  (bit ?  arr[i][k] : -arr[i][k]) ;
            }
            ll cur = abs(cur_cost - dp[j].f) ;
            if(cur) edge.pb({i , dp[j].s ,cur ,0,0}) ;

        }
    }
   // for(auto it : edge) cout << it.u << " " << it.v << " " << it.w << endl ;

    cout << mst(n) << endl ;

}
int main()
{
     __Heart__
     //READ("input9.txt") ;
     //WRITE("output9.txt") ;
     int t ; t = 1 ; while(t--) solve() ;
}

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-07 12:55:31
Judged At
2024-11-11 02:55:16
Judged By
Score
100
Total Time
18ms
Peak Memory
3.332 MiB