/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 328.0 KiB
#2 Wrong Answer 1ms 540.0 KiB
#3 Wrong Answer 6ms 1.582 MiB

Code

#ifndef COMMON_H
#define COMMON_H 1
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <random>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define szx(x) (int)(x).size()
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
constexpr int LOG2(int x)
{
    return 32 - __builtin_clz(x) - 1;
}
#endif // COMMON_H
#ifndef DEBUG_H
#define DEBUG_H 1
#ifndef CLown1331
#define debug(...) 0
#define ASSERT(...) 0
#define dbg(...) 0
#endif
#endif // DEBUG_H
#ifndef ATCODER_DSU_HPP
#define ATCODER_DSU_HPP 1
#include <algorithm>
#include <cassert>
#include <vector>
namespace atcoder {
struct dsu {
  public:
    dsu() : _n(0) {}
    explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}
    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }
    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }
    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }
    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }
    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }
  private:
    int _n;
    std::vector<int> parent_or_size;
};
}  // namespace atcoder
#endif  // ATCODER_DSU_HPP
#ifndef solution_h
#define solution_h 1
namespace solution
{
const int sz = 2e5 + 105;
const int mod = 1e9 + 7;
const ll INF = 1e16;
int n, k;
int coord[sz][5];
struct edge {
    int u, v;
    ll w;
    edge(int u, int v, ll w) : u(u), v(v), w(w) {}
    bool operator < (const edge &e) const {
        return w > e.w;
    }
};
vector <edge> edges;
/*https://cp-algorithms.com/geometry/manhattan-distance.html#farthest-pair-of-points-in-manhattan-distance*/
void get_edges()
{
    vector <pair<int, int>> calc(1 << k);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < (1 << k); j++)
        {
            ll sum = 0;
            for (int l = 0; l < k; l++)
            {
                sum += (j & (1 << l)) ? coord[i][l] : -coord[i][l];
            }
            if (i == 0 || sum < calc[j].first)
            {
                calc[j] = {sum, i};
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < (1 << k); j++)
        {
            ll sum = 0;
            for (int l = 0; l < k; l++)
            {
                sum += (j & (1 << l)) ? coord[i][l] : -coord[i][l];
            }
            sum -= calc[j].first;
            if (sum > 0)
            {
                edges.emplace_back(i, calc[j].second, sum);
            }
        }
    }
}
void solve()
{
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < k; j++)
        {
            scanf("%d", &coord[i][j]);
        }
    }
    get_edges();
    atcoder::dsu d(n);
    ll ans = 0;
    for (auto &e : edges)
    {
        if (d.same(e.u, e.v))
        {
            continue;
        }
        d.merge(e.u, e.v);
        ans += e.w;
    }
    printf("%lld\n", ans);
}
} // namespace solution
#endif // solution_h
#define _CRT_SECURE_NO_WARNINGS
int main()
{
    solution::solve();
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1098 KuZ the Maximum spanning tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-13 17:20:07
Judged At
2024-12-03 05:00:52
Judged By
Score
1
Total Time
6ms
Peak Memory
1.582 MiB