Submission #994157


Source Code Expand

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1.0);

void precalc() {
}

const int maxn = (int) 4e5 + 10;
int edges[maxn][3];

int n, q;

int read() {
  if (scanf("%d%d", &n, &q) < 2) {
    return 0;
  }
  for (int i = 0; i < q; ++i) {
    int s, t, c;
    scanf("%d%d%d", &s, &t, &c);
    edges[2 * i][0] = s, edges[2 * i][1] = t, edges[2 * i][2] = c;
    edges[2 * i + 1][0] = s + 1, edges[2 * i + 1][1] = t, edges[2 * i + 1][2] = c + 1;
  }
  q *= 2;
  return 1;
}


int pr[maxn], w[maxn];

int get(int v) {
  return pr[v] == v ? v : pr[v] = get(pr[v]);
}

void myUnion(int s, int t) {
  s = get(s), t = get(t);
  if (s == t) {
    return;
  }

  if (w[s] == w[t]) {
    ++w[s];
  }

  if (w[s] < w[t]) {
    swap(s, t);
  }

  pr[t] = s;
}

void solve() {
  for (int i = 0; i < n; ++i) {
    pr[i] = i;
    w[i] = 0;
  }

  set<pair<int, int> > evs;
  for (int i = 0; i < q; ++i) {
    evs.insert(mp(edges[i][2], i));
  }

  long long res = 0;
  while (sz(evs)) {
    int id = evs.begin()->second;
    evs.erase(evs.begin());
    int &s = edges[id][0], &t = edges[id][1];
    if (get(s) == get(t)) {
      continue;
    }
    myUnion(s, t);
    res += edges[id][2];
    s = (s + 1) % n;
    t = (t + 1) % n;
    edges[id][2] += 2;
    evs.insert(mp(edges[id][2], id));
  }
  printf("%lld\n", res);
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}

Submission Info

Submission Time
Task G - Zigzag MST
User XraY
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 2183 Byte
Status AC
Exec Time 276 ms
Memory 25216 KB

Compile Error

./Main.cpp: In function ‘int read()’:
./Main.cpp:47:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &s, &t, &c);
                                ^

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 3
AC × 33
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
Case Name Status Exec Time Memory
01-01.txt AC 3 ms 256 KB
01-02.txt AC 194 ms 23680 KB
01-03.txt AC 230 ms 25216 KB
01-04.txt AC 17 ms 1792 KB
01-05.txt AC 17 ms 1792 KB
01-06.txt AC 20 ms 1792 KB
01-07.txt AC 22 ms 1792 KB
01-08.txt AC 25 ms 1920 KB
01-09.txt AC 41 ms 2944 KB
01-10.txt AC 132 ms 13568 KB
01-11.txt AC 227 ms 24448 KB
01-12.txt AC 249 ms 25216 KB
01-13.txt AC 246 ms 25216 KB
01-14.txt AC 247 ms 25216 KB
01-15.txt AC 246 ms 25216 KB
01-16.txt AC 247 ms 25216 KB
01-17.txt AC 251 ms 25216 KB
01-18.txt AC 225 ms 25216 KB
01-19.txt AC 19 ms 1792 KB
01-20.txt AC 33 ms 2048 KB
01-21.txt AC 97 ms 8576 KB
01-22.txt AC 276 ms 25216 KB
01-23.txt AC 271 ms 25216 KB
01-24.txt AC 26 ms 3328 KB
01-25.txt AC 241 ms 25216 KB
01-26.txt AC 17 ms 1792 KB
01-27.txt AC 35 ms 2944 KB
01-28.txt AC 125 ms 16256 KB
01-29.txt AC 179 ms 24576 KB
01-30.txt AC 194 ms 25216 KB
sample-01.txt AC 3 ms 256 KB
sample-02.txt AC 3 ms 256 KB
sample-03.txt AC 3 ms 256 KB