Submission #7487065


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

struct UnionFind {
  const int n;
  V<> t; // root ? -sz : par
  UnionFind(int n) : n(n), t(n, -1) {}
  int find(int v) { return t[v] < 0 ? v : t[v] = find(t[v]); }
  void unite(int u, int v) {
    if ((u = find(u)) == (v = find(v))) return;
    if (-t[u] < -t[v]) swap(u, v);
    t[u] += t[v];
    t[v] = u;
  }
  bool same(int u, int v) { return find(u) == find(v); }
  int size(int v) { return -t[find(v)]; }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, q; cin >> n >> q;
  struct Edge {
    int u, v, w;
    bool operator<(const Edge& r) const { return w > r.w; }
  };
  priority_queue<Edge> pq;
  while (q--) {
    int a, b, c; cin >> a >> b >> c;
    pq.emplace(Edge{a + n, b, c});
    pq.emplace(Edge{(a + 1) % n, b + n, c + 1});
  }
  UnionFind uf(n);
  lint res = 0;
  while (uf.size(0) < n) {
    auto e = pq.top(); pq.pop();
    if (!uf.same(e.u % n, e.v % n)) {
      uf.unite(e.u % n, e.v % n);
      res += e.w;
    } else {
      continue;
    }
    if (e.u >= n) {
      pq.emplace(Edge{(e.u + 1) % n, e.v + n, e.w + 1});
      pq.emplace(Edge{e.u + n, (e.v + 1) % n, e.w + 2});
    } else {
      pq.emplace(Edge{e.u + n, (e.v + 1) % n, e.w + 1});
      pq.emplace(Edge{(e.u + 1) % n, e.v + n, e.w + 2});
    }
  }
  cout << res << '\n';
}

Submission Info

Submission Time
Task G - Zigzag MST
User risujiroh
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1501 Byte
Status RE
Exec Time 221 ms
Memory 14316 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 0 / 1300
Status
AC × 3
AC × 23
RE × 13
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
all sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt AC 1 ms 256 KB
01-02.txt AC 54 ms 6640 KB
01-03.txt RE 171 ms 6896 KB
01-04.txt RE 108 ms 1024 KB
01-05.txt RE 99 ms 1024 KB
01-06.txt RE 100 ms 1024 KB
01-07.txt RE 104 ms 1024 KB
01-08.txt AC 28 ms 1152 KB
01-09.txt AC 41 ms 1404 KB
01-10.txt AC 77 ms 3444 KB
01-11.txt AC 85 ms 6768 KB
01-12.txt AC 110 ms 8432 KB
01-13.txt AC 110 ms 7408 KB
01-14.txt AC 109 ms 8176 KB
01-15.txt AC 110 ms 7024 KB
01-16.txt AC 109 ms 8048 KB
01-17.txt AC 110 ms 7152 KB
01-18.txt RE 177 ms 8304 KB
01-19.txt RE 107 ms 1024 KB
01-20.txt AC 38 ms 1280 KB
01-21.txt AC 71 ms 4212 KB
01-22.txt AC 134 ms 14316 KB
01-23.txt AC 134 ms 13548 KB
01-24.txt RE 107 ms 1404 KB
01-25.txt AC 147 ms 6512 KB
01-26.txt RE 99 ms 1024 KB
01-27.txt RE 123 ms 1404 KB
01-28.txt RE 186 ms 9072 KB
01-29.txt RE 208 ms 6896 KB
01-30.txt RE 221 ms 7792 KB
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB
sample-03.txt AC 1 ms 256 KB